|
-
October 15th, 2009, 11:43 AM
#1
Simple passing pointers as an argument
Hello,
In my code, I create a 3-element integar array, using the function CreateArray(). I then want to pass this array to another function, which will inspect the values of this array. Here is what I have tried:
Code:
int* CreateArray();
void CheckValues(int* an_array);
int main()
{
int* my_array = CreateArray();
CheckValues(my_array);
return 0;
}
int* CreateArray()
{
int my_array[3];
my_array[0] = 5;
my_array[1] = 10;
my_array[2] = 25;
return my_array;
}
void CheckValues(int* an_array)
{
int a = an_array[0];
int b = an_array[1];
int c = an_array[2];
}
The problem is, in CheckValues(int* an_array), the values of a, b and c are not 5, 10 and 15 as expected, bu they are all -858993460.
I don't know why this is. I pass a pointer to my array, and then check the values which the pointer is pointing to. What's wrong?
I know that when an argument is passed to a function, a copy is made of it, so the original variable is not modified. But in this case, if I a copy is made of the pointer, then the copy will still point to the same address, and should still therefore point to the correct values.
Any help?
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|