Thread: How can I assign string Hello to structure member?

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    22

    How can I assign string Hello to structure member?

    See the simple code below. The compiler gives message:

    assigning to type ‘char[20]’ from type ‘char *’.

    I've tried everything else that seems reasonable but none works. How can I assign string Hello to the structure member? TIA.

    Code:
    int main()
    {
    
    struct strc1
    {
    char msg1[20];
    char msg2[5];
    }talk;
    
    talk.msg1 = "Hello";
    }

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    please use strcpy function then you will be able to copy it.

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    22
    Quote Originally Posted by Satya View Post
    please use strcpy function then you will be able to copy it.
    I tried a few things using strcpy() but I havent got it. The code below makes good sense to me. But it says:

    assigning to type ‘char[20]’ from type ‘char *’

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    char string1[20];
    char string2[20] = "Hello";
    struct strc1
    {
    char msg1[20];
    char msg2[5];
    }talk;
    
    talk.msg1 = strcpy(string1,string2);
    }

  4. #4
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Seriously ..use

    Code:
    strcpy(talk.msg1, strcpy(string1, string));

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Outgoing from your first example and the advice from Satya, i came up with this code.

    Additions:
    Don't leave the parentheses of a function empty. If you write a function that take no argument, then write 'void' in it.
    Empty parentheses means nothing or more arguments.

    The main-function returns an integer, so make sure that you have a return statement with an integer value.
    If all goes right, it will mostly be 0.

    Indent your code correctly.

    Code:
    int main(void)
    {
    
        struct strc1
        {
            char msg1[20];
            char msg2[5];
        } talk;
    
        strcpy(talk.msg1, "Hello");
    
        return 0;
    }
    Last edited by WoodSTokk; 02-17-2015 at 06:52 AM.
    Other have classes, we are class

  6. #6
    Registered User clarasoft's Avatar
    Join Date
    Feb 2015
    Posts
    2
    The question has been well answered. What follows is just additional info on using strcpy and an alternative solution; You might have sometihing like the following:

    Code:
    int main(int argc, char** argv)
    {
     
    struct strc1
    {
    char msg1[20];
    char msg2[5];
    }talk;
     
    strcpy(talk.msg1, argv[2]);
     
    strcpy(talk.msg1, argv[1]);
    
    printf("\nmsg1: %s\nmsg2: %s\n", talk.msg1, talk.msg2);
    
    return 0;
    }
    The above will copy argv[1] to talk.msg1 and argv[2] to talk.msg2. talk.msg2 is initialised first to show the point while keeping your original structure.
    If the first argument is over 20 characters, the program may or may not crash but there will
    certainly be a buffer overrun in talk.msg2 (this means that the excess characters will be copied into char msg2 and overwrite part or all of what was initialised there).


    Let's see an example:


    compile the above code (name the program myprog) and call it like this:

    ./myprog 1234567890ABCDEFGHIJKL Hi!


    The output is:


    msg1: 1234567890ABCDEFGHIJKL
    msg2: KL

    The msg2 field should have held the string "Hi!"; this shows that the msg2 field has actually been corrupted from assigning to msg1. Also, the msg1 string is actually 22 characters long when used in so-called string functions.
    One solution is to use strncpy and memset. strncpy will copy up to a specified number of characters but will not necessarily append a null terminator.
    Use memset to initialise your buffers with nulls and specifiy a string lenth one less than the target buffer to insure that your buffer is null terminated:


    Code:
    int main(int argc, char** argv)
    {
     
    struct strc1
    {
    char msg1[20];
    char msg2[5];
    }talk;
     
    
    memset(talk.msg1, 0, sizeof(talk.msg1));
    memset(talk.msg2, 0, sizeof(talk.msg2));
    
    strncpy(talk.msg2, argv[2], sizeof(talk.msg2)-1);
    strncpy(talk.msg1, argv[1], sizeof(talk.msg1)-1);
    
    printf("\nmsg1: %s\nmsg2: %s\n", talk.msg1, talk.msg2);
    
    return 0;
    }
    You could of course use memset on the entire struct (use the address of talk):

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cannot assign struct member values
    By snow365 in forum C Programming
    Replies: 5
    Last Post: 02-14-2015, 12:51 PM
  2. Replies: 10
    Last Post: 03-18-2014, 10:43 AM
  3. assign string to the string variable inside structure!
    By king_zart in forum C++ Programming
    Replies: 20
    Last Post: 12-09-2012, 11:37 AM
  4. Replies: 13
    Last Post: 09-27-2010, 12:36 PM
  5. Replies: 12
    Last Post: 10-14-2003, 10:17 AM