In above code, if I declare arr2[5] name="name"; then it throws error. why?Code:int main() { typedef char arr2[5]; arr2 name="name"; printf("%s",name); return 0; }
In above code, if I declare arr2[5] name="name"; then it throws error. why?Code:int main() { typedef char arr2[5]; arr2 name="name"; printf("%s",name); return 0; }
doesn't give me errors... spells out: "name"...Code:#include <stdio.h> int main() { typedef char arr2[5]; arr2 name="name"; printf("%s\n",name); return 0; }
gave me an error because there wasn't any I/O examples being included.... so I put in "#include <stdio.h>" hopefully this was the case
Last edited by Bennie98; 11-15-2010 at 06:25 AM.
Because Tetris Is Unrealistic.
"The fear of death is the most unjustified of all fears, for there's no risk of accident for someone who's dead." - Albert Einstein
"The Edge... there is no honest way to explain it because the only people who really know where it is are the ones who have gone over." - Hunter S. Thompson
"I never think of the future. It comes soon enough." - Albert Einstein
For starters, the syntax is wrong, i.e., it would be arr2 name[5] = "name"; But if you did that, then it would be tantamount to writing char name[5][5] = "name";. That does not make sense since "name" is a const char[5]. Basically, the code snippet you posted is okay (assuming you #include <stdio.h> earlier), changing it in the way you described is not okay.Originally Posted by siperi
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hi I used (arr2[5] name="name" instead of (arr2 name="name" in the above program. That time I got error.
Right. So do you now understand why that is wrong?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I'm not quite clear. Please explain briefly.
You used the typedef to make arr2 an alias for the type array of 5 char. Therefore, arr2[5] is the type array of 5 array of 5 char. As such, you cannot assign "name" to the array.
But besides this, if you actually wrote:
it would be syntactically wrong regardless of the typedef. The [5] cannot appear at that place. That is, this is also wrong:Code:arr2[5] name = "name;
Code:char[5] name = "name";
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)