Archived

This topic is now archived and is closed to further replies.

Passing pointer problem

This topic is 5710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

//Exercise in futility// #include <stdio.h> #define MAX 3 //Variables// int array1[MAX]; int array2[MAX]; int counter = 0; int *ptr; //Prototypes// int *addarray(int *ptr1, int *ptr2); main() { for(counter = 0; counter < MAX; counter++) { printf("\nEnter a value for array1[%d]", (counter+1)); scanf("%d", &array1[counter]); printf("\nEnter a value for array2[%d]", (counter+1)); scanf("%d", &array2[counter]); } addarray(array1,array2); for(counter = 0; counter < MAX; counter++) { printf("\nArray1[%d] + Array2[%d] = %d", counter, counter, *ptr ); ptr++; } return 0; } int *addarray(int *ptr1, int *ptr2) { int total = 0; int holdarray[MAX]; for(counter = 0; counter < MAX; counter++) { total = *ptr1 + *ptr2; ptr1++; ptr2++; holdarray[counter] = total; total = 0; } for(counter = 0; counter < MAX; counter++) { printf("\n Holdarray[%d] = %d", counter, holdarray[counter]); } ptr = holdarray; return ptr; } when compiled and ran...i get a valid total in holdarrays printf portion, but the pointer sent back to main which holds the starting address of holdarray when incremented is not working past its first intial use. I have verified that ptr is incrementing correctly, which almost makes me think that holdarray''s mem is being released be4 i access it with my pointer? why if i can get the first value from pointer correct can i not increment by ptr++ and get the next value from holdarray like i should? i appreciate any help..i am just starting to try to grasp pointers in c

Share this post


Link to post
Share on other sites
In function addarray you declare holdarray[MAX]. But once function addarray returns control to main, holdarray goes out of scope. Therefore ptr is pointing to memory that has been released and could possibly contain garbage data.

Perhaps declare holdarray as global, or you could declare it as static in function addarray:


int *addarray(int *ptr1, int *ptr2)
{
static int holdArray[MAX];
|
|
}

------

Shop for the Lowest Price!
Then, Check a Reseller's Rating Before You Purchase!

[edited by - aNonamuss on May 30, 2002 10:32:33 PM]

Share this post


Link to post
Share on other sites
thanks for the help

i cant believe i missed that, i thought i had declared holdarray as globlal on a previous build and that it hadnt worked, but i think i may have had it declared in two places or some other problem...
going global fixed it, i am going to take a closer look at the static solution
thanks again

Share this post


Link to post
Share on other sites
You could even do away with a the globals and optimize your code a bit by doing the following. Just a thought:



      
//Exercise in futility//

#include <stdio.h>

#define MAX 3


//Prototypes//

int *addarray(int *ptr1, int *ptr2);

main()
{
int counter;
int array1[MAX];
int array2[MAX];

for(counter = 0; counter < MAX; counter++)
{
printf("\nEnter a value for array1[%d]", (counter+1));
scanf("%d", &array1[counter]);

printf("\nEnter a value for array2[%d]", (counter+1));
scanf("%d", &array2[counter]);
}

int *ptr = addarray(array1,array2);

for(counter = 0; counter < MAX; counter++)
{
printf("\nArray1[%d] + Array2[%d] = %d", counter, counter, *ptr );
ptr++;
}

return 0;
}


int *addarray(int *ptr1, int *ptr2)
{
int counter;
static int holdarray[MAX];

for(counter = 0; counter < MAX; counter++)
{
holdarray[counter] = *ptr1 + *ptr2;
ptr1++;
ptr2++;
printf("\n Holdarray[%d] = %d", counter, holdarray[counter]);

}

return holdarray;
}



[edited by - aNonamuss on May 30, 2002 10:50:01 PM]

Share this post


Link to post
Share on other sites
interesting...

I was wondering about passing holdarray back directly without going vi a pointer, but i guess thats impossible. Instead i see u assigned holdarray[0]''s address to *ptr.
Thanks for the cleaning the code a little.

I was trying to pass holdarray back directly for awhile
but i guess return doesnt like to pass along more than one value...go figure.

But, i see that u can still pass its address to another pointer and reference from there.

thanks for all the info..i actually learned something

Share this post


Link to post
Share on other sites