"操作符:通过结构体指针访问结构体成员并赋值。例如:struct Student {int id;char name[2" />

租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

c语言结构体指针赋值的有哪些方法,c语言结构体指针做函数参数

发布时间:2023-12-14 01:52:30

c语言结构体指针赋值的有哪些方法

在C语言中,结构体指针赋值有两种方法:

  1. 使用"->"操作符:通过结构体指针访问结构体成员并赋值。例如:
struct Student {
    int id;
    char name[20];
};

int main() {
    struct Student s1;
    struct Student *ptr_s1 = &s1;
    
    ptr_s1->id = 1;
    strcpy(ptr_s1->name, "John");
    
    return 0;
}
  1. 使用"*"操作符:通过解援用结构体指针访问结构体成员并赋值。例如:
struct Student {
    int id;
    char name[20];
};

int main() {
    struct Student s1;
    struct Student *ptr_s1 = &s1;
    
    (*ptr_s1).id = 1;
    strcpy((*ptr_s1).name, "John");
    
    return 0;
}

这两种方法都可以对结构体指针所指向的结构体成员进行赋值操作。