Thread: About Variable assignment

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    26

    About Variable assignment

    Hi to all,


    I have this declaration:

    Code:
    struct node 
    { 
       char info[BUFLEN] ; 
       struct node *next ; 
    } ;
    and in the main
    Code:
    char msg[BUFLEN];
    After the initialition of string with (for example)
    Code:
    i=0; 
    char_recv = recv(newsocketfd, &c, 1, 0); 
    while (c !='\n') 
    { 
     msg[i++]=c; 
     char_recv = recv(newsocketfd, &c, 1, 0); 
     
    } /* End while (c !='\n') */
    I need to insert msg into my struct; if I write
    Code:
    p = malloc(sizeof(struct node)); 
      p->info = msg; 
      p->next = first; 
      first = p;
    the compiler return to me the message:
    error: incompatible type in assignement
    How can to correct the error ? I hope in Your help.

    Best Regards
    Nick

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    Could you also show us the line of code where you initialize "p"?

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You can't assign an array to another array. Perform a strcpy() if you wish to copy from one string to one that is inside your struct.

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You forgot that info and msg are actually pointers, not string objects. You have to use the strcpy() function:

    Code:
    strcpy(p->info, msg);

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    26
    Quote Originally Posted by KONI View Post
    You forgot that info and msg are actually pointers, not string objects. You have to use the strcpy() function:

    Code:
    strcpy(p->info, msg);
    Thank You,
    It's work.
    Bye

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by nick048 View Post
    Thank You,
    It's work.
    Bye
    It's work but it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C variable
    By webstarsandeep in forum C Programming
    Replies: 1
    Last Post: 10-23-2008, 01:26 AM
  2. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  5. Replies: 2
    Last Post: 04-12-2004, 01:37 AM