WasabiCannon WasabiCannon - 1 year ago 73
C Question

How to concatenate an array of chars into 1 variable?

I have an array of chars that I would like to set to 1 variable. How would I go about doing this. For example I would have the following code:

char list[5] = {'B','O','B','B','Y'};

how would I have it so that I could set it to a variable to have it so that:

char *name = "BOBBY"

pulling the values from the list shown above.

Answer Source

In addition to the other answers, there is simpler solution for systems conforming to POSIX 1-2008, such as linux and OS/X:

char *name = strndup(list, sizeof(list));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download