Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 自动定义数组大小_C_Arrays_String_Size - Fatal编程技术网

C 自动定义数组大小

C 自动定义数组大小,c,arrays,string,size,C,Arrays,String,Size,我正在尝试将常量字符*字符串复制到字符数组[?] 在C中有没有一种方法可以自动设置数组大小字符字[255]而不是使用255我的程序将在我的案例10中自动使用正确的大小。如果有更好的方法,我愿意接受任何建议 const char* test_str = "my string."; int lenght = strlen(test_str), x=0; char word[255] = {'\0'}; //memset(word, '\0', sizeof

我正在尝试将
常量字符*字符串
复制到
字符数组[?]

在C中有没有一种方法可以自动设置数组大小<代码>字符字[255]而不是使用
255
我的程序将在我的案例10中自动使用正确的大小。如果有更好的方法,我愿意接受任何建议

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);
const char*test\u str=“我的字符串。”;
内部长度=strlen(测试长度),x=0;
字符字[255]={'\0'};
//memset(单词'\0',sizeof(单词));
while(x

编辑:删除
memset(word,'\0',sizeof(word))并替换为
字[255]={'\0'}

这里有两个选项

第一种是使用
malloc
。基本上是这样使用的:

char *word = malloc(sizeof *word * (strlen(test_str)+1));
完成后,你就可以释放内存了

free(word);
另一种选择是使用VLA(可变长度数组):

我建议使用
malloc
。这有点混乱,但在我看来,VLA:s有相当多的缺点,如果你要学习C,你迟早要学习
malloc
,但是没有VLA:s你可以做得很好


我写了一个关于为什么我认为VLA在这里不好的答案:

这里有两个选项。

第一种是使用
malloc
。基本上是这样使用的:

char *word = malloc(sizeof *word * (strlen(test_str)+1));
完成后,你就可以释放内存了

free(word);
另一种选择是使用VLA(可变长度数组):

我建议使用
malloc
。这有点混乱,但在我看来,VLA:s有相当多的缺点,如果你要学习C,你迟早要学习
malloc
,但是没有VLA:s你可以做得很好


我已经写了一个关于为什么我认为VLA在这里不好的答案:

在C99中阅读关于<代码> MALLC/<代码>,你可以使用这样的VLA(可变长度数组):<代码> char Word [长度+1 ];<代码>(
\0
终止符需要+1)@FelixG True。不过我不推荐。最好看看malloc。阅读一本关于C编程的好书,例如,然后阅读C编译器(可能…)和调试器(可能…<代码>“我的字符串”。
不适合10个字符的数组。阅读关于
malloc
在C99中,你可以使用如下的VLA(可变长度数组):
char word[length+1]
\0
终止符需要+1)@FelixG True。不过我不推荐。最好看看malloc。阅读一本关于C编程的好书,例如,然后阅读C编译器(可能…)和调试器(可能…)的文档。
“我的字符串”。
不适合10个字符的数组。我可能忘记了我的操作顺序,但这不会分配40个字节而不是11个字节吗?为什么要乘以指针大小?*word是字符指针。在现代系统上,根据体系结构,可以是4或8字节。
sizeof*word==sizeof(char)
sizeof word==到ptr大小(4或8字节)
是的,它工作了!,我以前没有加+1,但我自己想出来了。谢谢大家。我以前上过一些编程课程,我的老师告诉我,我应该总是像这样使用malloc
char*word=(char*)malloc(sizeof*word*strlen(text)+1)*
运算符用于取消对
字符
word
的指针的引用,而不是两次将
word
声明为指针
sizeof*word
等于
sizeof(char)
。我可能忘记了我的操作顺序,但这不是分配了40字节而不是11字节吗?为什么要乘以指针大小?*word是字符指针。在现代系统上,根据体系结构,可以是4或8字节。
sizeof*word==sizeof(char)
sizeof word==到ptr大小(4或8字节)
是的,它工作了!,我以前没有加+1,但我自己想出来了。谢谢大家。我以前上过一些编程课程,我的老师告诉我,我应该总是像这样使用malloc
char*word=(char*)malloc(sizeof*word*strlen(text)+1)*
运算符用于取消对
字符
word
的指针的引用,而不是两次将
word
声明为指针
sizeof*word
等于
sizeof(char)