简体   繁体   English

如何将char *复制到char [] []

[英]How to copy a char* into a char[][]

Before start, let me show you my code: 在开始之前,让我向您展示我的代码:

int     main(int argc, char *argv[])
{                  
  t_arbre       *node;
  char          str[3][1];

  &(*(str[0])) = strdup("1"); //lvalue required as left operand of assignment
  str[1] = strdup("2"); //incompatible types when assigning to type ‘char[1]’ from type ‘char *’
  str[2] = strdup("3");
  if ((node = malloc(sizeof(t_arbre))) == NULL)
    {
      printf("Error: malloc in the main function failed\n");
      return (-1);
    }
  create_node(node, str);
  return (0);
}

So, as you can see, I'm currently working in C. And I'm trying to assign a (char*) to a (char[3][1]). 因此,如您所见,我目前正在C语言中工作。我正在尝试将(char *)分配给(char [3] [1])。 Otherwise the compiler throw me for error: 否则,编译器会向我抛出错误:

incompatible types when assigning to type 'char[1]' from type 'char *' 从'char *'类型分配给'char [1]'类型时不兼容的类型

when I'm trying to assign by the following way: 当我尝试通过以下方式分配时:

str[2] = strdup("2"); 

I'm know that if I declare a string like this 我知道如果我声明这样的字符串

char str[5];

and then try to assign it a string using for example strdup() function, it could trow me the same error. 然后尝试使用例如strdup()函数为它分配一个字符串,它可能会向我抛出相同的错误。 However in this case I know that I can "dereference" it like: 但是在这种情况下,我知道我可以像这样“取消引用”它:

str[0] 

Which will give me a (char) variable. 这将给我一个(char)变量。 And given I want it to store a string. 鉴于我希望它存储一个字符串。 I have to "transform" it into a (char*) by using the '&' charactere. 我必须使用'&'字符将其“转换”为(char *)。 Which will give me 哪个会给我

&str[0]

In this case, it will work. 在这种情况下,它将起作用。 because I have a pointer to the first character. 因为我有一个指向第一个字符的指针。 By doing so, I'll be able to copy a (char*) into a (char[]). 这样,就可以将(char *)复制到(char [])中。

Otherwise, in the upper code I provide you, I want to copy a (char*) into a (char[3][1]) (Don't ask me why, it's a bit long to explain haha) So as you can see, I tried my way to solve the error like 否则,在我提供给您的上层代码中,我想将(char *)复制到(char [3] [1])中(不要问我为什么,解释哈哈会有点长)所以您可以看到,我尝试了解决类似的错误的方法

str[2] = strdup("2");

which throw me this error message 这让我看到此错误消息

incompatible types when assigning to type 'char[1]' from type 'char *' 从'char *'类型分配给'char [1]'类型时不兼容的类型

And by this way 通过这种方式

&(*(str[0])) = strdup("1"); //or &(*(str[0]))

which throw me this error message 这让我看到此错误消息

lvalue required as left operand of assignment 需要左值作为赋值左操作数

Does anyone can tell me how can I copy a (char*) into a (char[][]) ? 有人能告诉我如何将(char *)复制到(char [] [])吗? Or is it just imposible to do what I want to do ? 还是做我想做的事只是不可能? I don't speak english very well, hopeing I used the right term. 我英语说得不太好,希望我使用正确的术语。

Thank you :) 谢谢 :)

  • You can't assign to things that aren't intended for assignment, that's what "lvalue required as left operand of assignment" is telling you, because you're taking the address of something with & , which is essentially just a value, not somewhere you can store something (eg a variable), and then trying to assign to it. 您不能分配给非分配对象,这就是“作为分配的左操作数所需的左值”告诉您的内容,因为您要使用&取得某物的地址,这实际上只是一个值,而不是您可以在某个地方存储某些内容(例如变量),然后尝试分配给它。
  • A char* is not a char[] , that's what the incompatible types error is about. char*不是char[] ,这就是不兼容类型错误的原因。
  • C arrays do not support assignment. C数组不支持分配。
  • Don't forget that in C, arrays start at index 0, so the last valid index in an array of length 3 is 2 (eg arr[2] ), not 3. 不要忘记,在C中,数组从索引0开始,因此长度为3的数组中的最后一个有效索引为2(例如arr[2] ),而不是3。

Use strcpy() instead to copy a string into an array: 使用strcpy()代替将字符串复制到数组中:

char str[3][2]; // must allow space for the null terminator
strcpy(str[0], "1");
strcpy(str[1], "2");
strcpy(str[2], "3");

Or use an array of pointers and strdup() : 或者使用指针数组和strdup()

char *str[3];
str[0] = strdup("1");
...

If you do that, don't forget to free them when you're done, because strdup() allocates memory, eg 如果这样做,别忘了在完成后释放它们,因为strdup()分配内存,例如

// free one pointer from the array:
free(str[0]);
// or if you have allocated to every value in the array:
size_t arr_len = sizeof(str) / sizeof(*str);
for (size_t n = 0; n < arr_len; ++n)
    free(str[n]);

strdup returns a pointer-to-a-char. strdup返回指向一个字符的指针。 If you want to store that pointer in an array, then you need an array of pointers, like this 如果要将指针存储在数组中,则需要一个指针数组,如下所示

char *str[3];           // an array of three pointer-to-char
str[0] = strdup("1");
str[1] = strdup("2");
str[2] = strdup("3");

for ( int i = 0; i < 3; i++ )
    printf( "%s\n", str[i] );

If you want to copy the entire string content you can use strcpy , however note that it copies null terminated strings. 如果要复制整个字符串内容,可以使用strcpy ,但是请注意,它会复制以null终止的字符串。 It is needed to have place for the last null symbol. 需要在最后一个空符号处留出位置。 So, the length of string arrays must be at least 2 ( char str[3][2] ) to copy strings like "1". 因此,字符串数组的长度必须至少为2( char str[3][2] ),才能复制类似“ 1”的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM