Original Post
//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