By Gianluca Bargelli

2010-06-28 09:35:52 8 Comments

I'm trying to understand how to solve this trivial problem in C, in the cleanest/safest way. Here's my example:

#include <stdio.h>

int main(int argc, char *argv[])
    typedef struct
        char name[20];
        char surname[20];
        int unsigned age;
    } person;

    //Here i can pass strings as does it works?
    person p = {"John", "Doe",30};

    printf("Name: %s; Age: %d\n",,p.age);
    // This works as expected...
    p.age = 25;
    //...but the same approach doesn't work with a string = "Jane";

    printf("Name: %s; Age: %d\n",,p.age);

    return 1;

The compiler's error is:

main.c: In function ‘main’: main.c:18: error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’

I understand that C (not C++) has no String type and instead uses arrays of chars, so another way to do this was to alter the example struct to hold pointers of chars:

#include <stdio.h>

int main(int argc, char *argv[])
    typedef struct
        char *name;
        char *surname;
        int unsigned age;
    } person;

    person p = {"John", "Doe",30};

    printf("Name: %s; Age: %d\n",,p.age);

    p.age = 25; = "Jane";

    printf("Name: %s; Age: %d\n",,p.age);

    return 1;

This works as expected but I wonder if there a better way to do this. Thanks.


@Péter Török 2010-06-28 09:41:48

The first example doesn't work because you can't assign values to arrays - arrays work (sort of) like const pointers in this respect. What you can do though is copy a new value into the array:

strcpy(, "Jane");

Char arrays are fine to use if you know the maximum size of the string in advance, e.g. in the first example you are 100% sure that the name will fit into 19 characters (not 20 because one character is always needed to store the terminating zero value).

Conversely, pointers are better if you don't know the possible maximum size of your string, and/or you want to optimize your memory usage, e.g. avoid reserving 512 characters for the name "John". However, with pointers you need to dynamically allocate the buffer they point to, and free it when not needed anymore, to avoid memory leaks.

Update: example of dynamically allocated buffers (using the struct definition in your 2nd example):

char* firstName = "Johnnie";
char* surname = "B. Goode";
person p; = malloc(strlen(firstName) + 1);
p.surname = malloc(strlen(surname) + 1);

p.age = 25;
strcpy(, firstName);
strcpy(p.surname, surname);

printf("Name: %s; Age: %d\n",,p.age);


@Gus 2010-06-28 09:50:21

The two structs are different. When you initialize the first struct, about 40 bytes of memory are allocated. When you initialize the second struct, about 10 bytesof memory are allocated. (Actual amount is architecture dependent)

You can use the string literals (string constants) to initalize character arrays. This is why

person p = {"John", "Doe",30};

works in the first example.

You cannot assign (in the conventional sense) a string in C.

The string literals you have ("John") are loaded into memory when your code executes. When you initialize an array with one of these literals, then the string is copied into a new memory location. In your second example, you are merely copying the pointer to (location of) the string literal. Doing something like:

char* string = "Hello";
*string = 'C'

might cause compile or runtime errors (I am not sure.) It is a bad idea because you are modifying the literal string "Hello" which, for example on a microcontroler, could be located in read-only memory.

@Gianluca Bargelli 2010-06-28 10:07:34

You are correct, the assignment you wrote cause a segfault because you are trying to alter a pointer value, but referring to my example things goes like char *string = "Hello"; string = "C"; (note there's no pointer assignment on the last statement) which works as expected.

@Gus 2010-06-28 10:13:53

I would like to mention that "altering a pointer value" does not necessarily cause a segfault. The reason that the snippet in my original post causes a segfault is because you are trying to modify memory which is located in a restricted address space.

@Artelius 2010-06-28 09:45:14

Think of strings as abstract objects, and char arrays as containers. The string can be any size but the container must be at least 1 more than the string length (to hold the null terminator).

C has very little syntactical support for strings. There are no string operators (only char-array and char-pointer operators). You can't assign strings.

But you can call functions to help achieve what you want.

The strncpy() function could be used here. For maximum safety I suggest following this pattern:

strncpy(, "Jane", 19);[19] = '\0'; //add null terminator just in case

Also have a look at the strncat() and memcpy() functions.

@Gianluca Bargelli 2010-06-28 10:38:07

For now the best answer but also Péter's one is good (showing how to do it with pointers) so i'm waiting a little more to see if more people can add more tips/suggestions on the subject.

Related Questions

Sponsored Content

63 Answered Questions

[SOLVED] What is the difference between String and string in C#?

84 Answered Questions

[SOLVED] How do I make the first letter of a string uppercase in JavaScript?

76 Answered Questions

[SOLVED] How do I iterate over the words of a string?

  • 2008-10-25 08:58:21
  • Ashwin Nanjappa
  • 2118407 View
  • 2849 Score
  • 76 Answer
  • Tags:   c++ string split

3 Answered Questions

58 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

56 Answered Questions

[SOLVED] How to replace all occurrences of a string?

24 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

43 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

18 Answered Questions

[SOLVED] Why is char[] preferred over String for passwords?

Sponsored Content