issue with struct assignment.....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanddune008
    New Member
    • Nov 2006
    • 4

    issue with struct assignment.....

    Hi All,

    1. How to acess the member name.
    2. I am trying to fill the name with "Sanny". Vc++ IDE generates exception
    on \return.

    3. what is wrong with following assignment.


    struct profile
    {
    int Rid;
    char *name;
    }emp;

    int main()
    {
    printf("\n Enter the Rid and name");
    scanf("%d",&emp .Rid);
    scanf("%s",emp. name);
    //puts(Danoop->name);
    //gets(Danoop.nam e);
    //printf("\n%s",D anoop.name);
    return 0;
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Originally posted by sanddune008
    Code:
    struct profile
    {
        int Rid;
        char *name;
    }emp;
    The 'name' field of the structure is large enough to hold a pointer to a string -- not the string itself. You need to allocate a buffer, copy the string into the buffer, and then put the address of the buffer into the 'name' field.

    Comment

    Working...