【发布时间】:2018-07-31 23:14:22
【问题描述】:
所以我一直遇到这个错误,到目前为止我似乎无法找到答案,如果之前有人问过这个问题,我深表歉意。 以下是受影响的代码:
char **List_Of_Words;
int List_Index=0;
List_Of_Words = malloc(Number_of_Words *sizeof(char*));
//Processed_Word word is a word that has been read from a file, its in a loop.
strcpy(*List_Of_Words[List_Index], Processed_Word);
List_Index++;
//im looping through the array to print each word thats stored there
for(int i = 0;i < List_Index; i++)
{
printf("%s\n", *List_Of_Words[List_Index]);
}
当我使用 Visual Studio 进行调试时,我收到此错误:
Unhandled exception at 0x01229240 in Wordcount.exe: 0xC0000005: Access violation writing location 0x00000030.
所以,我假设我的程序正在尝试访问它无法访问的内存。 C 对我来说是一门新语言,所以我真的不知道如何处理这个问题。
【问题讨论】:
-
List_Of_Words是一个指针。但你似乎从来没有真正指出任何地方? -
忘记放 malloc 行了
-
List_Of_Words[0]未初始化,但您取消引用它 -
另外你给
printf("%s"提供了错误的类型,好的编译器会警告这一点 -
为什么问题标题中有“2D”?对我来说,这看起来像是您尝试拥有一个一维字符串数组。
标签: c multidimensional-array c-strings