[英]i can't assign one struct to another in c
i have 2 structs, i want to assign one struct to another, but when i print the results, it prints crap, the functions : "ver_tope" is on charge to do that , what am i doing bad?, here is the code:我有 2 个结构,我想将一个结构分配给另一个结构,但是当我打印结果时,它会打印废话,功能:“ver_tope”负责执行此操作,我在做什么坏事?,这是代码:
#include <stdio.h>
#include <stdlib.h>
#define TAM 4
typedef struct{
char nomyap[40];
int edad;
}t_info;
typedef struct {
t_info pila [TAM];
int tope;
}t_pila;
void ver_tope(const t_pila *p, t_info *d);
int main()
{
t_pila pila;
t_info info;
//I CHARGE BOTH STRUCTS
ver_tope(&pila, &info);
return 0;
}
void ver_tope(const t_pila *p, t_info *d)
{
*d = p->pila[(p->tope)-1];
return ;
}
Try adding an initialisation for pila.tope in main() ex:尝试在 main() 中为 pila.tope 添加一个初始化,例如:
... //I CHARGE BOTH STRUCTS
pila.tope =2;
ver_tope(&pila, &info);
...
That stopped the segmentation fault...这停止了分段错误......
int main()
{
t_pila pila;
t_info info;
ver_tope(&pila, &info);
return 0;
}
You have not initialized either variable.您尚未初始化任一变量。 Since pila
is the source of the assignment you can do the following:由于pila
是作业的来源,您可以执行以下操作:
int main()
{
t_pila pila = { 0 };
pila.tope = 1;
t_info info;
ver_tope(&pila, &info);
return 0;
}
Here I default initialized pila
and then set its tope
member to 1
.在这里,我默认初始化pila
,然后将其tope
成员设置为1
。 I did not initialize info
since ver_tope
assigns to it.我没有初始化info
因为ver_tope
分配给它。 It would be clearer if you converted ver_tope
into a function that returned t_info
.如果将ver_tope
转换为返回t_info
的函数会更清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.