Thread: typedef

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    32

    Lightbulb typedef

    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?

  2. #2
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49
    Code:
    #include <stdio.h>
    
    int main()
    {
    	typedef char arr2[5];
    	arr2 name="name";
    	printf("%s\n",name);
    	return 0;
    }
    doesn't give me errors... spells out: "name"...

    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

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by siperi
    In above code, if I declare arr2[5] name="name"; then it throws error. why?
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    32

    Lightbulb

    Hi I used (arr2[5] name="name" instead of (arr2 name="name" in the above program. That time I got error.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right. So do you now understand why that is wrong?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    I'm not quite clear. Please explain briefly.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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:
    Code:
    arr2[5] name = "name;
    it would be syntactically wrong regardless of the typedef. The [5] cannot appear at that place. That is, this is also wrong:
    Code:
    char[5] name = "name";
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM