Hello, I was just wondering, is it possible to lose data when usingpointers? I w
ID: 3618145 • Letter: H
Question
Hello,I was just wondering, is it possible to lose data when usingpointers?
I was using a value returning function with this header:
int *Mode(int *, int*)
basically, what happened is this.
I had the function return an array with the following numbers= {8,19,21,37,40}
Now, the problem is when I print it out from my main int( )the numbers changed, it became different.
The array showed the value = {8,1,0,7,3}
Not sure if this makes sense =/
Is it memory allocation issues? right now we're learningsomething called dynamic allocations...
I was just wondering, is it possible to lose data when usingpointers?
I was using a value returning function with this header:
int *Mode(int *, int*)
basically, what happened is this.
I had the function return an array with the following numbers= {8,19,21,37,40}
Now, the problem is when I print it out from my main int( )the numbers changed, it became different.
The array showed the value = {8,1,0,7,3}
Not sure if this makes sense =/
Is it memory allocation issues? right now we're learningsomething called dynamic allocations...
Explanation / Answer
#include<stdio.h> #define MAX_SIZE 1000 int main() { int* arr; int i; //Calloc intializes all values to 0 //and the 5 represents the size. arr = calloc(5,sizeof(int*)); arr[0] = 1; arr[1] = 9; arr[2] = 10; arr[3] = 11; arr[4] = 19; for(i = 0; i < 5; i++) printf("%d ",arr[i]); system("pause"); return 0; }
#include<stdio.h> #define MAX_SIZE 1000 int main() { int* arr; int i; //Calloc intializes all values to 0 //and the 5 represents the size. arr = calloc(5,sizeof(int*)); arr[0] = 1; arr[1] = 9; arr[2] = 10; arr[3] = 11; arr[4] = 19; for(i = 0; i < 5; i++) printf("%d ",arr[i]); system("pause"); return 0; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.