Kreuzade
Kreuzade

Reputation: 777

Assign string to element in structure in C

I have this structure:

typedef struct SM_DB
{
    LIST_TYPE           link;
    char                name[SM_NAME_SIZE];
} SM_DB_TYPE;

And I would like to assign a string to its 'name'. I am doing so like this:

SM_DB_TYPE one;
one.name = "Alpha";

However, after compiling I get an error: "error C2106: '=' : left operand must be l-value". I am hoping this is fairly obvious. Does anyone know what I am doing wrong?

Thanks

Upvotes: 5

Views: 40211

Answers (5)

Keith Miller
Keith Miller

Reputation: 1347

Assuming SM_NAME_SIZE is large enough you could just use strcpy like so:

strcpy(one.name, "Alpha");

Just make sure your destination has enough space to hold the string before doing strcpy your you will get a buffer overflow.

If you want to play it safe you could do

if(!(one.name = malloc(strlen("Alpha") + 1))) //+1 is to make room for the NULL char that terminates C strings
{
      //allocation failed
}
strcpy(one.name, "Alpha");  //note that '\0' is not included with Alpha, it is handled by strcpy
//do whatever with one.name
free(one.name) //release space previously allocated

Make sure you free one.name if using malloc so that you don't waste memory.

Upvotes: 6

check123
check123

Reputation: 2009

Use:

strcpy(one.name, "Alpha"); //Removed null byte (Read first comment by shf301)

Alternative:

typedef struct SM_DB  
{
    LIST_TYPE           link;
    char*               name;   
} SM_DB_TYPE;

SM_DB_TYPE one;
one.name = malloc(sizeof(char) * (strlen("Alpha") + 1); //Allocate memory
if (!one.name) {
   /* Error handling */
} else {
    strcpy(one.name, "Alpha");
}

Upvotes: 1

Richard Chambers
Richard Chambers

Reputation: 17593

C does not have a built in string type. You must use an array of characters to hold the string.

Since C also does not allow the assignment of one array to another, you have to use the various functions in the Standard C Library to copy array elements from one array to another or you have to write a loop to do it yourself. Using the Standard C Library functions is much preferred though there are sometimes reasons to write your own loop.

For standard ANSI type strings used with the char type there are a large number of functions most of which begin with str such as functions to copy or compare strings strcpy(), strcmp(). There are also another set which you specify the maximum number of characters to copy or compare such as strncpy() or strncmp().

A string in C is an array of characters that is terminated by a binary zero character. So if you use a constant string such as "Constant" this will create an array of characters that has one element per character plus an additional element for the zero terminator.

This means that when sizing char arrays you must also remember to add one more extra array element to hold the zero terminator.

The strncpy() function will copy one char array to another up to either the maximum number of characters specified or when the zero terminator is found. If the maximum number of characters is reached then the destination array will not be terminated by a zero terminator so this is something to watch out for.

char  one[10];
char  two[20];
strncpy (one, "1234567", 10);  // copy constant to the char buffer max of 10 chars
one[9] = 0;   // make sure the string is zero terminated, it will be this is demo
strcpy (two, one);
strcat (two, " suffix");    // add some more text to the end

There are also functions to work with wide characters used with UNICODE.

Upvotes: 2

Sachin Mhetre
Sachin Mhetre

Reputation: 4543

You can assign value to string only while declaring it. You can not assign it later by using =.

You have to use strcpy() function.

Upvotes: 6

P.P
P.P

Reputation: 121397

Use strcpy or strncpy to assign strings in C.

Upvotes: 2

Related Questions