I think I'm going to test you a little, as well.
Can you tell me the output of this program?
Code:
typedef struct
{
int a, b, c;
} MyStruct;
void foo1(MyStruct* pStruct)
{
MyStruct* pLocalStruct = pStruct;
pLocalStruct ->a = 1;
printf("%i", pStruct->a);
}
void foo2(MyStruct* pStruct)
{
MyStruct* pLocalStruct;
pLocalStruct = malloc( sizeof(MyStruct) );
*pLocalStruct = *pStruct;
pLocalStruct->a = 1;
printf("%i", pStruct->a);
}
int main()
{
MyStruct s;
s.a = s.b = s.c = 50;
printf("Before foo1, a = %i\n", s.a);
foo1(&s);
printf("After foo1, a = %i\n", s.a);
s.a = s.b = s.c = 50;
printf("Before foo2, a = %i\n", s.a);
foo2(&s);
printf("After foo2, a = %i\n", s.a);
return 0;
}
Originally Posted by
andrew.bolster
if that is the incorrect way to handle such pointers, what is the correct way?
copy the pointers in the main()?
Do not copy FILE* pointers.