在C中使用strcpy通过函数初始化结构

斯托克斯

我是c的初学者,想知道为什么我的函数feed_struct不将我处理的字符串复制到它。该函数(feed_struct)应该获取输入数据并将其放入我全局定义的结构中。有谁知道为什么这个结构什么都没有发生?谢谢您的帮助!

void feed_struct(struct student x, char name [20], char lname [20], double a, char adres [50], int b)
{
    strcpy(x.name, name);
    strcpy(x.lastname, lname);
    x.number = a;
    strcpy(x.adres, adres);
    x.course = b;


}

int main (void)
{
    struct student new_student;
    feed_struct(new_student, "Peter", "Panther", 1230, "El-Lobo-Street 32", 72);
    struct_print(new_student);
    return 0;

}  
dbush

你传递new_studentfeed_struct按值直接。因此,该功能的更改在中不可见main

您需要将指针传递struct studentfeed_struct然后,您可以取消引用该指针以更改指向的对象。

// first parameter is a pointer
void feed_struct(struct student *x, char name [20], char lname [20], double a, char adres [50], int b)
{
    strcpy(x->name, name);
    strcpy(x->lastname, lname);
    x->number = a;
    strcpy(x->adres, adres);
    x->course = b;


}

int main (void)
{
    struct student new_student;
    // pass a pointer
    feed_struct(&new_student, "Peter", "Panther", 1230, "El-Lobo-Street 32", 72);
    struct_print(new_student);
    return 0;

}  

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章