1

I'm trying to copy a const char* string to a char array[?]

is there a way in C to set my array size automatically? char word[255]; instead of using 255 my program will automatically use the correct size in my case 10. If there's a better way to do it I'm open to any suggestions thx a lot.

const char* test_str = "my string.";

   int lenght = strlen(test_str), x=0;
    char word[255] = {'\0'};
    //memset(word, '\0', sizeof(word));
    while (x < lenght) {
        word[x] = test_str[x];
        x++;
    }

    printf("%s", word);

edit: Removed memset(word, '\0', sizeof(word)); and replaced with word[255]= {'\0'};

FatSumo
  • 79
  • 7
  • 1
    Read about `malloc` – qrdl Jul 03 '20 at 11:37
  • In C99, you can use a VLA (variable length array) like this: `char word[length + 1];` (+1 is necessary for the `\0` terminator) – Felix G Jul 03 '20 at 11:38
  • 2
    @FelixG True. Would not recommend it though. Better to look at malloc. – klutt Jul 03 '20 at 11:38
  • Read a good book about C programming, e.g. [*Modern C*](https://modernc.gforge.inria.fr/), then read [this C reference](https://en.cppreference.com/w/c) and the documentation of your C compiler (perhaps [GCC](http://gcc.gnu.org/)...) and debugger (perhaps [GDB](https://www.gnu.org/software/gdb/)...) – Basile Starynkevitch Jul 03 '20 at 11:46
  • @klutt depends on the application... if the length is limited to some sane(!) value, and the array is only needed within that function, a VLA is faster and a little more convenient because you don't have to free it afterwards. But i agree that malloc is usually the better option, because it's more portable (VLA support is optional since C11) and won't blow up your stack. Can't hurt to know about VLAs though – Felix G Jul 03 '20 at 11:46
  • @FelixG My objection to VLA:s is that they are too easy to use. I have a rant about them here https://stackoverflow.com/a/58163652/6699433 – klutt Jul 03 '20 at 11:49
  • 2
    `"my string."` does not fit in a 10 char array. – pmg Jul 03 '20 at 11:57
  • The assumption that 10 `char`s are required to store a string of 10 characters into an array is wrong and popular among newbies. You need another 11th element to store the string-terminating null character. Else if you attempt to use that string, your program has [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). Please update your source of information. – RobertS supports Monica Cellio Jul 03 '20 at 12:41

1 Answers1

3

You have two options here.

The first is using malloc. It's basically used like this:

char *word = malloc(sizeof *word * (strlen(test_str)+1));

and when you're done, you free the memory.

free(word);

The other alternative is using a VLA (variable length array):

char word[strlen(test_str) + 1];

I would recommend using malloc. It's a little bit messier, but in my opinion, VLA:s have considerable drawbacks, and if you're about to learn C, you will have to learn malloc sooner or later anyway, but you can do perfectly fine without VLA:s.

I have written an answer about why I consider VLA:s bad here: https://stackoverflow.com/a/58163652/6699433

klutt
  • 30,332
  • 17
  • 55
  • 95
  • I’m probably forgetting my order of operations but wouldn’t that allocate 40 bytes instead of 11? Why multiply by the pointer size? – mreff555 Jul 03 '20 at 11:51
  • *word is a char pointer. On modern systems that would be either 4 or 8 bytes depending on the architecture. – mreff555 Jul 03 '20 at 12:06
  • `sizeof *word == sizeof(char)` and `sizeof word == to the ptr size (4 or 8 bytes)` – Nick S Jul 03 '20 at 12:10
  • yes it worked!, I did without adding the +1 before but I figured it out and aded myself. thx a lot everybody. I did some programming courses before and my teacher told the I should always cast my malloc like this `char* word =(char*) malloc(sizeof *word *strlen(text)+1);`, can anyone tell me why ?. thx a lot. – FatSumo Jul 03 '20 at 12:25
  • 2
    @mreff555 In this case, the `*` operator is used to *dereference* the pointer to `char` `word`, not to *declare* `word` as pointer twice. `sizeof *word` is equal to `sizeof (char)`. – RobertS supports Monica Cellio Jul 03 '20 at 12:27
  • Ahh. I thought it might have been an order of operations thing. Thanks for clarifying. – mreff555 Jul 03 '20 at 12:31