【发布时间】:2016-01-06 13:25:52
【问题描述】:
是的,我确实阅读了这两篇文章。
C compile error: "Variable-sized object may not be initialized" C error "variable-sized object may not be initialized"
我的情况有点不同,因为我使用的是char * ptr[buflen]。这是我尝试过的:
char *ptr[buflen] = {0}; //This gave me the variable sized object error.
char *ptr[buflen];
memset( ptr, 0, buflen*buflen*sizeof(char)); //I figured this would work with looking at the previous examples.
//This seemed to work but I am curious if I need to use free or malloc
after looking at the previous examples. I don't want this to seg fault later
in the program and have no clue what is causing it.
char *ptr[buflen];
memset(ptr, 0, sizeof ptr);
char *strings_line_tokens[503] = {0}; //Why does this work but the above won't work?
【问题讨论】:
-
memset(ptr, 0, buflen*sizeof(char *))。虽然技术上 NULL-ptr 不必为零,所以您应该可能 只需使用循环来初始化所有指针。 -
char *ptr[buflen] = {0}应该是char *ptr[buflen] = {NULL} -
@DavidC.Rankin:为什么自行车脱落?在这种情况下也是一样的。
-
@DavidC.Rankin:听起来你使用了损坏的编译器。
-
是的,但无论如何你都不能初始化可变长度数组!
标签: c compiler-errors initializer-list variable-length-array