C语言结构体struct指针
结构体指针
C语言结构体struct的指针就是指向结构体内存地址的指针,其声明定义方式与其它类型的指针一样,语法如下:
struct tagName *struct_pointer_name
结构体指针的赋值方式
与其它指针的赋值方式一样,也可以通过寻址运算符的“&”方式将一个结构体struct变量的内存地址赋值给结构体指针,比如:
struct_pointer_name = &struct_var_name
结构体指针访问结构体成员的运算符
与结构体访问其成员使用点“.”的运算符不同,结构体指针访问其成员需要使用如下运算符号:
->
struct指针实例代码
#include <stdio.h>
#include <string.h>
// 定义结构体Student
struct Vegetable{
char name[50];
int price;
};
int main() {
// 实例化Vegetable结构体
struct Vegetable tomato;
strcpy(tomato.name, "tomato");
tomato.price = 1.5;
// 定义指向tomato的指针
struct Vegetable *t = &tomato;
// 访问指针指向的tomato的成员
printf("%s\n", t->name);
return 0;
}
代码编译运行,得到输出:
tomato
免责声明:内容仅供参考,不保证正确性!