Thread: need some help with structures !

  1. #1
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55

    need some help with structures !

    why can't i pass an already declared string through to a structure?

    Code:
    struct personInfo
    {
           char theName[21];
           int age;
    };
    
    int main()
    {
        int theAge = 28;
        char aName[] = "John Smith";
        personInfo p = {aName,theAge};
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    my initial thought is why use char? I'm looking at the code now...

    I should have added (unless you are using the size restriction of a char)...which you are. I just find string easier to work with than char but then again, I'm just a n00b.
    Last edited by FoodDude; 09-27-2005 at 02:59 PM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    See this link.
    http://www.cplusplus.com/doc/tutorial/tut3-5.html

    Code:
    int main()
    {
        int theAge = 28;
        char aName[] = "John Smith          ";
    	personInfo p;
    	p.age = theAge;
    	strcpy (p.theName, aName);
        //personInfo p = {aName,theAge};
        return 0;
    }

  4. #4
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    Quote Originally Posted by FoodDude
    why use char?
    why not?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Because the C++ string class is more intuitive and safer to use. Of course, in this case I don't know if you can use that struct initialization technique with the non-POD string class. That then brings up the question, why not use a constructor?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    You are trying to pass address of the string to a char array.
    Code:
    personInfo p = {aName,theAge};
    When array name is given it degenerates and gives base address of that array.In this you are trying to pass char pointer to a char and hence error.

    Solution-Declare a char pointer instead.
    Code:
    struct personInfo
    {
           char *theName;
           int age;
    };
    or pass the string instead of variable.
    Code:
    struct personInfo
    {
           char theName[21];
           int age;
    };
    
    int main()
    {
        int theAge = 28;
        char aName[] = "John Smith";
        personInfo p = {"John Smith",theAge};
        return 0;
    }
    Third way is to let user enter it
    Code:
    int main()
    {
    personal Info p;
    cin>>p.thename;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM