struct problem

This is a discussion on struct problem within the C Programming forums, part of the General Programming Boards category; hi if i have a struct as given below- Code: struct a { char *name; int i; }ob; main() { ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    30

    struct problem

    hi if i have a struct as given below-
    Code:
    struct a
    {
       char *name;
        int i;
    }ob;
    
    main()
    {
    ob.name="xyz";
    }
    Now this ob.name assignment gives an LValue required error.why?
    now a simple char ptr when assigned a string wont flag this right.
    why then does it happen for a ptr inside a struct?

    cheers!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by eklavya8 View Post
    hi if i have a struct as given below-
    Code:
    struct a
    {
       char *name;
        int i;
    }ob;
    
    main()
    {
    ob.name="xyz";
    }
    Now this ob.name assignment gives an LValue required error.why?
    now a simple char ptr when assigned a string wont flag this right.
    why then does it happen for a ptr inside a struct?

    cheers!
    Since this code does not give any errors whatsoever (other than "main()" instead of "int main()"), you should try asking your question again, either using the appropriate code or changing the question to match.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    39
    All thought I am brushing back up on C/C++ I would try to solve the problem without using pointers- then make change to pointers

    Code:
    struct a
    {
       char name[50];
        int i;
    }
    
    int main(void)
    {
      struct a ob;
    
      ob.name="xyz";
    
      return 0;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ninety3gd View Post
    All thought I am brushing back up on C/C++ I would try to solve the problem without using pointers- then make change to pointers

    Code:
    struct a
    {
       char name[50];
        int i;
    }
    
    int main(void)
    {
      struct a ob;
    
      ob.name="xyz";
    
      return 0;
    }
    Now see, this is the one that is broken -- arrays are not assignable in C. You would need to strcpy the string into ob.name.

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    AND you should put a semicolon after
    Code:
    struct a {...}

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    39

    Cool

    Sorry got my C/C++ confused- as mention needed to fix

    Code:
    struct a
    {
       char name[50];
        int i;
    };
    
    int main(void)
    {
      struct a ob;
    
      strcpy(ob.name,"xyz");
    
      return 0;
    }
    Hope this is better

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Yes, it's better. Was there something else?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  4. Replies: 22
    Last Post: 12-23-2008, 12:53 PM
  5. Replies: 16
    Last Post: 10-29-2006, 04:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21