帮助了解 C 中的结构和循环

发布于 2024-12-03 08:23:06 字数 519 浏览 0 评论 0原文

这里是 C 初学者。这在这里合法吗?当我运行此代码时,我不断收到状态访问冲突:

struct point {
    char *x;
    char *y;
}

int main()
{
    ....
    struct point *pt;

    for (;;)
    {
        ....        
        pt = process_message(message);
        printf("%s",pt->x);
        ...
    }
}

struct point* process_message(char* message)
{
    struct point *pt;
    pt = malloc(1*sizeof(struct point))
    strncpy(pt->x, message, 4);
    return pt;
}

编辑

大家好,我做了一些更改...但仍然收到访问冲突。请指教!

beginner in C here. Is this legal here? I keep getting status access violation when i run this code:

struct point {
    char *x;
    char *y;
}

int main()
{
    ....
    struct point *pt;

    for (;;)
    {
        ....        
        pt = process_message(message);
        printf("%s",pt->x);
        ...
    }
}

struct point* process_message(char* message)
{
    struct point *pt;
    pt = malloc(1*sizeof(struct point))
    strncpy(pt->x, message, 4);
    return pt;
}

EDIT

Hi guys i made some changes...but still getting the access violation. pls advise!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

山川志 2024-12-10 08:23:06

您需要为process_message中的struct point分配内存。

像这样的事情:

struct point* process_message(char* message)
{
    struct point *pt;
    pt = malloc(1*sizeof(struct point));
    // ... do some processing ...
    return pt;
}

当然,您应该在之后的某个时刻释放指针。

此外,您还应该在 process_message 中返回一个结构点或检查您的类型一致性。如果您仔细观察,您会发现您正在返回一个struct Price* 但您期望(在调用方)一个 struct point *

更新

现在问题已更新,您还需要为 char *xchar *y if 分配内存如果您想这样做:

strncpy(pt->x, message, 4);

我建议您阅读一点(或大量)有关 C++ 中的内存管理和指针的内容。指针并不是C 编程语言中处理数据的唯一方式。

(为了代码简单性,没有进行错误检查)

You need to allocate memory for struct point in process_message.

Something like this:

struct point* process_message(char* message)
{
    struct point *pt;
    pt = malloc(1*sizeof(struct point));
    // ... do some processing ...
    return pt;
}

Of course, you should free the pointer at some point afterwards.

Also you should return a struct point in process_message or check your type concordance. If you look carefully, you will see you are returning a struct price* but you are expecting (on the calling side) a struct point *.

UPDATE:

Now that question was updated, you will also need to allocate memory for char *x and char *y if you want to do this:

strncpy(pt->x, message, 4);

I would recommend to read a little bit (or a lot) about memory management and pointers in C++. Pointers are not the only way of dealing with data in C Programming Language.

(NO ERROR CHECKING DONE FOR CODE SIMPLICITY)

扬花落满肩 2024-12-10 08:23:06

您的 main 功能正常。您的问题出在 process_message 中,您在其中使用指向结构的指针,但没有为其分配任何内存,访问它,然后返回它。单独声明指针不会创建用于保存结构的内存。因此,您应该为结构体分配(例如使用malloc)内存,以便该内存在函数结束后“存在”。然后你可以像你正在做的那样返回指向它的指针,然后当你完成它时,你可以在 main 中释放它。

鉴于您正在执行的简单操作,可能有更好的方法来实现此处的目标。一种是在 main 函数中保留一个本地“临时”结构,并将其地址传递给处理消息:

struct point pt;
process_message(message, &pt);
printf("%s", pt.x);

这将“重用”本地内存,并且不需要分配/释放。请注意,这里您不返回任何内容,您只需在 process 函数中“填充”结构即可。

最后,如果您使用的是现代版本的 C,您实际上可以根据需要从函数中返回完整的结构:

struct point process_message(char* message)
{
    struct point pt;
    ... do some processing ...
    return pt;
}

并像这样调用它:

struct point pt = process_message(message);

Your main function is OK. Your problem is in process_message, where you're using a pointer-to-a-struct but not allocating any memory for it, accessing it, then returning it. Declaring a pointer alone does not create the memory to hold the struct. So you should allocate (use malloc for example) the memory for the struct, so that the memory will "exist" after the end of the function. Then you can return the pointer to it as you're doing, and then you would free it in main when you were done with it.

There are possibly better ways to accomplish the goal here given the simple operation you're doing. One is to keep one "scratch" struct local in your main function, and pass its address to process message:

struct point pt;
process_message(message, &pt);
printf("%s", pt.x);

This will "Reuse" the local memory and not require the alloc/free. Notice here that you don't return anything, you just "fill in" the struct in the process function.

Finally, if you're using a modern version of C, you can actually just return the full structure from the function if you want:

struct point process_message(char* message)
{
    struct point pt;
    ... do some processing ...
    return pt;
}

And call it like this:

struct point pt = process_message(message);
亽野灬性zι浪 2024-12-10 08:23:06

在函数 process_message 中分配内存

pt = malloc (sizeof (struct point));
/* do processing */
return pt;

请记住在使用 pt 后,

,并记住释放它 free (pt)编辑

此外,在分配 pt 后,您还需要分配内存块来将字符串存储到函数中的 xy。完成工作后,您需要首先释放字符串(内存块),然后释放结构。

Remember to allocate memory in the function process_message

pt = malloc (sizeof (struct point));
/* do processing */
return pt;

also after you have finished working with pt remember to free it free (pt).

EDIT

Also you need to allocate the memory blocks to store the strings to x and y in the function, after allocation of the pt. When you have done working, you need to free the strings first (memory blocks) and then the structure.

守望孤独 2024-12-10 08:23:06

每当您创建指针时,它只是一个 sizeof(ptr) 内存,它指向一些数据。因此,您必须有一些内存来存储数据。
因此,要么在函数(process_message)中分配内存,要么在调用函数中分配内存。
它应该像

struct point *pt = (struct point*)malloc(sizeof(struct point));

Whenever you create pointer, it's just a sizeof(ptr) memory , which points to some data. So you must have some memory where your data is stored.
So either allocate memory in your function ( process_message), or in calling function.
it should go like

struct point *pt = (struct point*)malloc(sizeof(struct point));
鲜血染红嫁衣 2024-12-10 08:23:06

正如现在所写,您正在分配结构点,但该结构包含指向字符串的指针,而不是字符串本身的存储空间。在复制到字符串之前,您需要为字符串分配空间:

pt = malloc(1*sizeof(struct point));
pt->x = (char *)malloc( strlen(message) + 1);

在使用 pt->y 之前,不要忘记对其执行相同的操作,并且不要忘记单独释放为字符串分配的存储空间,然后为结构分配的。

As written now, you're allocating the struct point, but that structure contains pointers to strings, and not storage for the strings themselves. You need to allocate space for the strings before you copy into them:

pt = malloc(1*sizeof(struct point));
pt->x = (char *)malloc( strlen(message) + 1);

Don't forget to do the same for pt->y before you use it, and don't forget to separately free the storage allocated for the strings, and then that allocated for the struct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文