Hi People,
I am trying to understand of how pointer to struct works. And I am running into a problem.
Not sure why the last line "printf("Struct pointer with Name: %s\n",e2[0]->Name);" is showing :-
Struct pointer with Name: This is a second struct.
I thought that it would show:-
Struct pointer with Name: This is the first struct.
However, it does not. Would any one pls help me out.
Thanks,
zbonzbon
Code:**********************************************************
* Array.c - Learning how array works.
**********************************************************/
#include <stdio.h>
#include <string.h>
int main(){
struct example{
char Name[40];
};
char *pt_Name[2]= {
"This is the first struct",
"This is a second struct"
};
struct example *e2[2];
int i;
for(i=0; i<2; i++){
struct example e;
strcpy(e.Name,pt_Name[i]);
e2[i]=&e;
}
printf(" %s\n",e2[0]->Name);
return 0;
}