How to copy char *str to char c[] in C?

前端 未结 6 2166
栀梦
栀梦 2020-12-14 04:52

Trying to copy a char *str to char c[] but getting segmentation fault or invalid initializer error.

Why is this code is giving me a

6条回答
  •  旧时难觅i
    2020-12-14 04:52

    char c[] must have some size;

    for example

    char c[]= "example init string";
    

    // that set up table c to c[19]; You can allocate it directly at the begginign of Your program;

    char c[19] = {0}; // null filled table
    

    char c[i] is the pointer so You don't need to copy anything; char c[19] ; c = "example init string"; // now &c[0] points the same address;

    Copy can be done wiht

     strcpy(dst, src);
    

    but MS force You to use secure function:

    strcpy_s(dst,buffsize,src);
    

提交回复
热议问题