简体   繁体   English

将 strcpy 与字符串文字数组一起使用

[英]Using strcpy with a string literal array

I currently have an array of char* 's.我目前有一个char*数组。 I am trying to use strcpy to assign each element in this array the current string being processed.我正在尝试使用strcpy将此数组中的每个元素分配给正在处理的当前字符串。 I have managed to do this with a 2D array of chars ( char[][] ).我已经设法用一个二维字符数组( char[][] )做到了这一点。 However, I want to accomplish this with an array of character pointers because I don't want to define the size of the array at compile time.但是,我想用字符指针数组来完成此操作,因为我不想在编译时定义数组的大小。 Is there anyway to have something like the following code?反正有没有像下面的代码?

n = 5;
char* test[n];
strcpy(test[0], "random string");

I understand that strcpy does not work with character pointers which are uninitialised and have looked into using memset .我知道 strcpy 不适用于未初始化且已研究使用memset的字符指针。 However, I'm hoping for a more elegant solution.但是,我希望有一个更优雅的解决方案。

You say you don't want to define the size of the array at compile time.你说你不想在编译时定义数组的大小。 Well, at some point you you will have to do it.好吧,在某些时候你将不得不这样做。 If it's not at compile time, it's at runtime.如果不是在编译时,就是在运行时。 And during run time, you can do it with malloc (found in <malloc.h> ):在运行时,您可以使用malloc (在<malloc.h>中找到):

n = 5;
char* test[n];

test[0] = malloc(sizeof(char) * 14); // 14 is the length of the string bellow, plus a terminator
strcpy(test[0], "random string");

If you want it to be cleaner, you can encapsulate it in a function (so you don't have to write malloc many times):如果你想让它更干净,你可以把它封装在一个function中(这样你就不用多次写malloc ):

#include <string.h>
#include <malloc.h>

void store(char * storage[], int pos, char const * sentence)
{
  int len = strlen(sentence);
  storage[pos] = malloc(sizeof(char) * (len + 1));
  strcpy(storage[pos], sentence);
}

int main()
{
  int n = 5;
  char * test[n];
  store(test, 0, "random string");
  store(test, 1, "another string");

  return 0;
}

Please consider using strncpy() instead of strcpy() to avoid writing data outside destination buffer.请考虑使用 strncpy() 而不是 strcpy() 以避免将数据写入目标缓冲区之外。 https://linux.die.net/man/3/strncpy https://linux.die.net/man/3/strncpy

I currently have an array of char* 's.我目前有一个char*数组。 I am trying to use strcpy to assign each element in this array the current string being processed.我正在尝试使用strcpy将此数组中的每个元素分配给当前正在处理的字符串。 I have managed to do this with a 2D array of chars ( char[][] ).我已经设法用一个二维字符数组( char[][] )来做到这一点。 However, I want to accomplish this with an array of character pointers because I don't want to define the size of the array at compile time.但是,我想用一个字符指针数组来完成这个,因为我不想在编译时定义数组的大小。 Is there anyway to have something like the following code?反正有类似下面的代码吗?

n = 5;
char* test[n];
strcpy(test[0], "random string");

I understand that strcpy does not work with character pointers which are uninitialised and have looked into using memset .我知道 strcpy 不适用于未初始化的字符指针,并且已经研究过使用memset However, I'm hoping for a more elegant solution.但是,我希望有一个更优雅的解决方案。

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

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