What's wrong with this code?
Code:
/*
#include<stdio.h>
struct msg{
char *p1;
char *p2;
char *p3;
}mymsg;
mymsg.p1="Teach yourself C in 21 days!";
mymsg.p2="By SAMS publishing.";
int main(void)
{
printf("mymsg.p1 points to %s\n",mymsg.p1);
printf("mymsg.p2 points to %s\n",mymsg.p2);
/* p3 points to the same string p1 points to */
mymsg.p3=mymsg.p1;
printf("mymsg.p3 points to the string p1 points to %s\n",mymsg.p3);
/* p3 points to the same string p2 points to */
mymsg.p3=mymsg.p2;
printf("mymsg.p3 points to the string p2 points to %s\n",mymsg.p3);
return 0;
}
*/
My compiler tells me:
"stru_array3.c", line 9: warning: old-style declaration or incorrect type for: mymsg
"stru_array3.c", line 9: identifier redeclared: mymsg
current : int
previous: struct msg {pointer to char p1, pointer to char p1, pointer to char p2, pointer to char p3} : "stru_array3.c", line 7
Re: What's wrong with this code?
m not sure but dont you think it would be better if you declared your structure in the main function ??
Secondly
char *p1 is only one byte of memory space. This can store just one alphabet! therefore i dont think that it is possible to have a whole string like "teach your self.....21 days"
Quote:
Originally posted by ylzhang
Code:
/*
#include<stdio.h>
struct msg{
char *p1;
char *p2;
char *p3;
}mymsg;
mymsg.p1="Teach yourself C in 21 days!";
mymsg.p2="By SAMS publishing.";
int main(void)
{
printf("mymsg.p1 points to %s\n",mymsg.p1);
printf("mymsg.p2 points to %s\n",mymsg.p2);
/* p3 points to the same string p1 points to */
mymsg.p3=mymsg.p1;
printf("mymsg.p3 points to the string p1 points to %s\n",mymsg.p3);
/* p3 points to the same string p2 points to */
mymsg.p3=mymsg.p2;
printf("mymsg.p3 points to the string p2 points to %s\n",mymsg.p3);
return 0;
}
*/
My compiler tells me:
"stru_array3.c", line 9: warning: old-style declaration or incorrect type for: mymsg
"stru_array3.c", line 9: identifier redeclared: mymsg
current : int
previous: struct msg {pointer to char p1, pointer to char p1, pointer to char p2, pointer to char p3} : "stru_array3.c", line 7