0

I want to convert my char array to a string so I can pass it into a function. Say if I have this:

char array[3] = {'1', 'a', '/'};

and I want to convert it to

char *string = "1a/";

Do I just add a NULL terminator on the end?

exsk
  • 11
  • 1
  • 1
  • 4
  • 8
    If you increase the array size to 4 and add a NUL character, it already is a string. – StoryTeller - Unslander Monica Sep 13 '17 at 07:04
  • 4
    In C a "string" is simply an array of characters terminated by a zero. – Some programmer dude Sep 13 '17 at 07:04
  • like [this](https://ideone.com/c7ezOI) – BLUEPIXY Sep 13 '17 at 07:12
  • @StoryTeller I believe there is one more tiny difference in the question, as asked, being that in the first, it is a fixed length of C type char[], and the second is a unknown length of C type char* I would advise against using the second allocation, as it is a STRING LITERAL (automatically null-terminated, and supposed to be const in nature) Rather declare char* variable and malloc for the size needed. And note these are two distinct data types. They can under certain circumstances be used interchangeably, but they are not synonym. – Louis Parkin Sep 13 '17 at 07:14

3 Answers3

4

Just add a zero-delimiter

char array[4] = {'1', 'a', '/', '\0'};
grek40
  • 13,113
  • 1
  • 24
  • 50
  • So as long as I add the null terminator at the end of my array, I can treat is as a string and pass it into functions such as printf("%s", array); ??? – exsk Sep 13 '17 at 07:08
  • 2
    yes exactly, thats it – grek40 Sep 13 '17 at 07:08
  • 4
    @declanpearson - It's not that you *can treat it* as a string. **It is** a string as defined by C. – StoryTeller - Unslander Monica Sep 13 '17 at 07:11
  • 1
    @StoryTeller yes, thats more accurate. More details about this topic might be found here https://stackoverflow.com/questions/12203339/c-string-definition-in-c-c for anyone who wants to understand and not just make it work somehow – grek40 Sep 13 '17 at 07:14
4

Declare your array like this

char array[] = {'1', 'a', '/', '\0'};  

or

char array[] = "1a/";
haccks
  • 104,019
  • 25
  • 176
  • 264
0

First of all, a char array and a char pointer are mostly the same, can at times be used interchangeably, but ultimately, a char[] has a known size, making it possible to use the sizeof() function, whereas char* just points to the first address of contiguous memory of unknown length, so sizeof will return whatever your default size for integers are (4 on 32-bit systems, 8 on 64-bit systems) to indicate the size required to store an address.

But generally the rule with strings are that they have to be null-terminated, so it doesn't matter if you use char[] or char*. See examples below.

char array[4] = {'1', 'a', '/', 0};

OR

char string[4];
memset(&string[0], 0x00, sizeof(string));
memcpy(&string[0], &array[0], 3);

OR

char* string;
string = malloc(sizeof(array)+1);
memset(&string[0], 0x00, sizeof(array)+1);
memcpy(&string[0], &array[0], sizeof(array));

and then passing the string as a char* is as simple as:

void foo (char* bar, int n)
{
  // do something with bar
}

and call it as

foo(&string[0], strlen(string));

Important to note, strlen can only be used on null-terminated char* strings.

If you have any questions, feel free to ask.

Louis Parkin
  • 800
  • 7
  • 15
  • 1
    I see what you are saying, but you really need to reword "*a char array and a char pointer are mostly the same*". Even qualified with *mostly*, that is more not-true than true. (the `memset`, `memcpy` example may be a bit over the OP's head if they are just now asking about *char array/string* differences `:)` – David C. Rankin Sep 13 '17 at 07:16
  • @DavidC.Rankin please give it another once-over (I made an edit to the part you found to be lacking) – Louis Parkin Sep 13 '17 at 07:23
  • Also, @DavidC.Rankin, I find that, the more information one provides in an answer, the more you enable a person to figure things out on their own. So yes, it is probably a little overkill, but I would find an answer like this much more useful in the long run than other answers that just tell you what to do without telling you why you are doing it. – Louis Parkin Sep 13 '17 at 07:29
  • 1
    The only problem is that a *character array* and a *pointer* are two completely separate and distinct variable types in C. An array is a distinct object with all elements guaranteed sequential in memory. An array will be converted to a pointer to its first element when passed as a parameter to a function (and technically on access). While a pointer can point to any element of an array, there is no guarantee that any two pointers are sequential in memory. You can use pointer arithmetic to access sequential values in memory, but that is as far as it goes. – David C. Rankin Sep 13 '17 at 07:43
  • 1
    1) `char[]` and `char *` are not at all the same; arrays _decay_ to pointers to their first elements (in most expressions), which makes arrays _seem_ to behave as pointers at times, but the distinction is important. 2) Strings are null-terminated character arrays, period. This is true even for string literals. A pointer may point to a string (more precisely, to its first element), but a pointer is not a string. There is a loose way in which people sometimes refer to such a pointer as a string, but for learners we must be very careful. – ad absurdum Sep 13 '17 at 09:25
  • 3) Suggest changing: "strlen can only be used on null-terminated char* strings" --> "`strlen()` can only be used on strings", since strings are _always_ null-terminated, and "`char*` strings" has no meaning, unless you mean that `strlen()` can only be used on pointers to strings (which is false, since a string, i.e., a null-terminated character array which will decay to a pointer to the first element of the string in the function call, can be provided as an argument to `strlen()`). – ad absurdum Sep 13 '17 at 09:26