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'};

8
  • 1
    Read about malloc Commented Jul 3, 2020 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) Commented Jul 3, 2020 at 11:38
  • 2
    @FelixG True. Would not recommend it though. Better to look at malloc. Commented Jul 3, 2020 at 11:38
  • Read a good book about C programming, e.g. Modern C, then read this C reference and the documentation of your C compiler (perhaps GCC...) and debugger (perhaps GDB...) Commented Jul 3, 2020 at 11:46
  • 2
    "my string." does not fit in a 10 char array. Commented Jul 3, 2020 at 11:57

1 Answer 1

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

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

6 Comments

I’m probably forgetting my order of operations but wouldn’t that allocate 40 bytes instead of 11? Why multiply by the pointer size?
*word is a char pointer. On modern systems that would be either 4 or 8 bytes depending on the architecture.
sizeof *word == sizeof(char) and sizeof word == to the ptr size (4 or 8 bytes)
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.
@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).
|

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.