写入作为结构体成员的字符数组
这是我第一次在这里发布问题 - 我搜索过类似的问题,但没有找到。
这是我的标题中的片段:
#define LINE_LEN_MAX 256
typedef struct line_description {
char buffer[LINE_LEN_MAX + 1];
[...]
} line;
这是我的主要函数中的片段:
int main(int argc, char *argv[]) {
line *lineRead;
//input: valid FILE *, read from cmdline
//char buffer[LINE_LEN_MAX + 1];
while(fgets(lineRead->buffer, LINE_LEN_MAX + 1, input) != NULL) {
[...]
memset(lineRead->buffer, 0, LINE_LEN_MAX + 1);
}
}
我不断收到段错误。如果我注释掉“memset()”行,我可以在出现段错误之前从输入文件中准确读取 3 行。
但是,如果我用本地 char[] 替换 'lineRead->buffer',我就可以完美地读取我的输入文件。
我对这里的结构有什么不理解的地方?我想我想要的是一个指向结构内 char[] 开头的指针,但显然这不是正在发生的事情。
编辑:抱歉,忘记指定:我在这里没有使用动态内存。
this is my first time posting a question here - I've searched for ones that are similar, but none came up that I found.
Here is the snippet from my header:
#define LINE_LEN_MAX 256
typedef struct line_description {
char buffer[LINE_LEN_MAX + 1];
[...]
} line;
And here is the snippet from my main function:
int main(int argc, char *argv[]) {
line *lineRead;
//input: valid FILE *, read from cmdline
//char buffer[LINE_LEN_MAX + 1];
while(fgets(lineRead->buffer, LINE_LEN_MAX + 1, input) != NULL) {
[...]
memset(lineRead->buffer, 0, LINE_LEN_MAX + 1);
}
}
I keep getting a segfault. If I comment out the 'memset()' line I can read exactly 3 lines from my input file before getting a segfault.
However, if I replace 'lineRead->buffer' with a local char[] I am able to read my input file perfectly.
What am I not understanding about structs here? What I think I want is a pointer to the beginning of the char[] inside the struct, but obviously this is not what is happening.
EDIT: Sorry, forgot to specify: I am not using dynamic memory here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
程序中的
lineRead
是一个未初始化的指针,这可能不是您想要的。您应该通过编写例如
line lineRead
来为行分配一些存储空间,这将在堆栈上分配一个line
结构。然后使用.
而不是->
来访问其成员。lineRead
in your program is an uninitialized pointer, which is probably not what you wanted.You should allocate some storage space for a line by writing e.g.
line lineRead
instead, which will allocate aline
struct on the stack. Then use.
rather than->
to access its members.也许您只是在代码片段中遗漏了一些内容,但没有显示正在分配的结构。
或者,如果您不需要它位于堆上(特别是考虑到
main
中最外层作用域中的堆栈对象无论如何都具有整个程序的生命周期),Maybe you just left something out of your snippet, but you didn't show the structure being allocated.
Or, if you don't need it to be on the heap (especially considering that a stack object in outermost scope in
main
has lifetime of the entire program anyway),您将
lineRead
声明为指向line
的指针,但实际上并未将其设置为指向任何内容。当您尝试访问未初始化指针恰好指向的随机位置时,您会遇到分段错误。如果您只需要在本地范围内使用
lineRead
,那么它不应该是一个指针,只需将其声明为If
lineRead
需要比该函数生存得更久,因此确实需要动态分配,使用指针,但也为其应指向的结构保留内存:You declare
lineRead
to be a pointer to aline
, but you don't actually set it to point to anything. When you then try to access the random location that the uninitialized pointer happens to point to, you get a segmentation fault.If you only need
lineRead
in the local scope it shouldn't be a pointer, just declare it asIf
lineRead
needs to live longer that the function and so really needs to be dynamically allocated, use a pointer but also reserve memory for the struct it should be pointing to: