5

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

2
  • You cannot assign strings to char arrays. Commented Aug 29, 2012 at 14:10
  • 1
    You can, but it has to be all at the same time, i.e. char name[SM_NAME_SIZE] = "Alpha"; Commented Jan 5, 2016 at 20:27

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

As far as I've known, we cannot allocate memory to an array. Isn't that right? But still your answer showed me a way.
6

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

You have to use strcpy() function.

Comments

2

Use strcpy or strncpy to assign strings in C.

2 Comments

Or if you can use them, strcpy_s or strncpy_s.
Or, if you know the length of the source string, use memmove() or memcpy() — and if you don't know the length of the source string, how do you know that it's safe to copy the string into the target.
2

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.

Comments

1

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");
}

5 Comments

You don't need the '\0' in. String constants implicitly have a null zero at the end. "Alpha\0" creates a string with two trailing null zeros.
Don't you think the sizeof(char) is a bit redundant since a char is only one byte? Unless I'm thinking of this incorrectly isn't that just saying malloc(1 * strlen("Alpha\0")) ?
@KeithMiller Just adds to the readability (especially given that readers can vary significantly based on their expertise)
And when you removed the \0, the strlen value will be one byte too short for malloc...
@BoPersson Really silly mistakes I make. Thanks for following up. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.