帮助了解 C 中的结构和循环
这里是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要为
process_message
中的struct point
分配内存。像这样的事情:
当然,您应该在之后的某个时刻
释放
指针。此外,您还应该在
process_message
中返回一个结构点
或检查您的类型一致性。如果您仔细观察,您会发现您正在返回一个struct Price*
但您期望(在调用方)一个struct point *
。更新:
现在问题已更新,您还需要为
char *x
和char *y
if 分配内存如果您想这样做:我建议您阅读一点(或大量)有关 C++ 中的内存管理和指针的内容。指针并不是C 编程语言中处理数据的唯一方式。
(为了代码简单性,没有进行错误检查)
You need to allocate memory for
struct point
inprocess_message
.Something like this:
Of course, you should
free
the pointer at some point afterwards.Also you should return a
struct point
inprocess_message
or check your type concordance. If you look carefully, you will see you are returning astruct price*
but you are expecting (on the calling side) astruct point *
.UPDATE:
Now that question was updated, you will also need to allocate memory for
char *x
andchar *y
if you want to do this: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)
您的
main
功能正常。您的问题出在 process_message 中,您在其中使用指向结构的指针,但没有为其分配任何内存,访问它,然后返回它。单独声明指针不会创建用于保存结构的内存。因此,您应该为结构体分配(例如使用malloc
)内存,以便该内存在函数结束后“存在”。然后你可以像你正在做的那样返回指向它的指针,然后当你完成它时,你可以在 main 中释放它。鉴于您正在执行的简单操作,可能有更好的方法来实现此处的目标。一种是在 main 函数中保留一个本地“临时”结构,并将其地址传递给处理消息:
这将“重用”本地内存,并且不需要分配/释放。请注意,这里您不返回任何内容,您只需在 process 函数中“填充”结构即可。
最后,如果您使用的是现代版本的 C,您实际上可以根据需要从函数中返回完整的结构:
并像这样调用它:
Your
main
function is OK. Your problem is inprocess_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 (usemalloc
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 wouldfree
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: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:
And call it like this:
在函数
process_message
中分配内存请记住在使用
pt
后,,并记住释放它
free (pt)
。 编辑此外,在分配
pt 后,您还需要分配内存块来将字符串存储到函数中的
。完成工作后,您需要首先释放字符串(内存块),然后释放结构。x
和y
Remember to allocate memory in the function
process_message
also after you have finished working with
pt
remember to free itfree (pt)
.EDIT
Also you need to allocate the memory blocks to store the strings to
x
andy
in the function, after allocation of thept
. When you have done working, you need to free the strings first (memory blocks) and then the structure.每当您创建指针时,它只是一个 sizeof(ptr) 内存,它指向一些数据。因此,您必须有一些内存来存储数据。
因此,要么在函数(process_message)中分配内存,要么在调用函数中分配内存。
它应该像
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
正如现在所写,您正在分配结构点,但该结构包含指向字符串的指针,而不是字符串本身的存储空间。在复制到字符串之前,您需要为字符串分配空间:
在使用 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:
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.