写入作为结构体成员的字符数组

发布于 2024-08-21 13:25:28 字数 771 浏览 2 评论 0原文

这是我第一次在这里发布问题 - 我搜索过类似的问题,但没有找到。

这是我的标题中的片段:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以酷 2024-08-28 13:25:28

程序中的 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 a line struct on the stack. Then use . rather than -> to access its members.

小伙你站住 2024-08-28 13:25:28

也许您只是在代码片段中遗漏了一些内容,但没有显示正在分配的结构。

line *lineRead; // uninitialized pointer: access to any field crashes
lineRead = (line*) malloc( sizeof( line_description ) );

或者,如果您不需要它位于堆上(特别是考虑到 main 中最外层作用域中的堆栈对象无论如何都具有整个程序的生命周期),

line lineRead; // don't need to use a pointer!

Maybe you just left something out of your snippet, but you didn't show the structure being allocated.

line *lineRead; // uninitialized pointer: access to any field crashes
lineRead = (line*) malloc( sizeof( line_description ) );

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),

line lineRead; // don't need to use a pointer!
枯叶蝶 2024-08-28 13:25:28

您将 lineRead 声明为指向 line 的指针,但实际上并未将其设置为指向任何内容。当您尝试访问未初始化指针恰好指向的随机位置时,您会遇到分段错误。

如果您只需要在本地范围内使用 lineRead ,那么它不应该是一个指针,只需将其声明为

line lineRead;

If lineRead 需要比该函数生存得更久,因此确实需要动态分配,使用指针,但也为其应指向的结构保留内存:

line *lineRead = malloc(sizeof(line));
lineRead->buffer[0] = '\0'; // or any other initializations...

You declare lineRead to be a pointer to a line, 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 as

line lineRead;

If 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:

line *lineRead = malloc(sizeof(line));
lineRead->buffer[0] = '\0'; // or any other initializations...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文