【发布时间】:2011-12-17 14:16:42
【问题描述】:
我的 main.h 中有一个结构,但是当我尝试为结构中的 3D 数组分配内存时,出现以下编译器错误。
'text' has no member named 'list'
现在,我只为 3D 数组获取结构中的其他变量。
main.h
#define MAX_WORD 100
typedef struct textTag {
char name[100];
char ***list;
int words;
}text;
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
void createArray(FILE *file, text *checkTexts, int fileCount, int size){
int i, n, wordCount, sections, rest;
FILE *textFile;
text localText;
char fileName[MAX_WORD + 30];
readFileNames(file, checkTexts);
for(i = 0; i < fileCount; i++){
localText = checkTexts[i];
strcpy(fileName, "./testFolder/");
strcat(fileName, checkTexts[i].name);
openFile(&textFile, fileName);
checkTexts[i].words = countWords(textFile);
sections = (wordCount / size);
rest = wordCount % size;
checkTexts[i].list = malloc(sections * sizeof(char **)); //Compile error here
for(n = 0; n < sections; n++){
checkTexts[i].list[n] = malloc(size * sizeof(char *)); //Compile error here
}
checkTexts[i].list[sections] = malloc(rest * sizeof(char*)); //Compile error here
readFileContent(textFile,checkTexts[i].list, size); //Compile error here
}
}
【问题讨论】:
-
请包含编译错误
-
您只提到了编译器错误,但没有提到您收到的确切错误。你需要提到它。
-
编译器说 main.c:170:20: error: 'text' has no member named 'list'
-
用于定义结构的 sn-p 是否都在 main.h 文件中?如果没有,你能显示整个文件吗?您的代码在 gcc 4.5 下编译得很好,但我在源文件中定义了内联文本。
-
无法复制。注释掉带有未定义函数的行并定义 MAX_WORD,
gcc -W -c main.c仅警告可能未初始化使用wordCount。
标签: c multidimensional-array struct