一个指向变量的指针表示的是占内存中起始位置

一个指向结构体的变量的指针表示的是这个结构体变量占内存中的起始位置,同样它也可以指向结构体变量数组

 

定义结构体变量的指针:

//假设已有一个结构体名为Student
struct Student *pStruct
// 结构体类型 * 指针名;

通过指针来引用结构体中的成员,有三种方式

demo:

 # include <stdio.h>
# include <stdlib.h>
/*
1.使用->引用结构体成员 */ int main ()
{ struct Student
{
char cName[];
int iNumber;
char cSex;
int iGrade;
}student={"Girl",,'w',}; struct Student *pStruct;
pStruct = &student; //指向student结构体变量
printf("-----------the sudent's information----------\n");
printf("Name:%s\n",(*pStruct).cName);
printf("Number:%d\n",(*pStruct).iNumber);
printf("Sex:%c\n",(*pStruct).cSex);
printf("Grade:%d\n",(*pStruct).iGrade);
printf("============使用->符号引用结构体成员==============\n");
printf("Name:%s\n",pStruct->cName);
printf("Number:%d\n",pStruct->iNumber);
printf("Sex:%c\n",pStruct->cSex);
printf("iGrade:%d\n",pStruct->iGrade); return ;
}

1.student.iNumber

2.(*pStruct).iNumber

3.pStruct->iNumber

后两种是通过结构体变量指针来引用的结构体变量中的成员,第2种在*pStruct上加上小括号的原因是因为要提升 ”*pStruct“ 的运算优先级,因为在默认情况 . 运算符的优先级是比*运算符的优先级要高的。

个人比较喜欢用"->指向运算符"来引用结构体中的成员

注意:声明结构体的位置可以放在main函数外,也可以放在main函数里

一些习题例子:

 # include <stdlib.h>
# include <string.h> struct Student
{
char name[];
int iNumber;
char cSex;
int iGrade;
}student; int main ()
{
struct Student *pStruct;
pStruct = &student;
strcpy(pStruct->name,"瑶瑶");
pStruct->iNumber = ;
pStruct->cSex='W';
pStruct->iGrade=; printf("------------The student's information---------\n");
printf("Name:%s\n",(*pStruct).name);
printf("Number:%d\n",(*pStruct).iNumber);
printf("Sex:%c\n",(*pStruct).cSex);
printf("Grade:%d\n",(*pStruct).iGrade); return ;
}
 # include <stdio.h>
# include <stdlib.h> //创建一个学生结构体
struct Student
{
char Name[];
char sex;
int score;
char lovep[];
}student; int main () {
struct Student *pStruct;
pStruct = &student;
printf("Input:====================\n");
printf("Name:");
scanf("%s",&pStruct->Name);
getchar(); //在使用%s读入字符串的时候 需要一个getchar来接受空格符号 这样才能不影响下次的输入
printf("sex:");
scanf("%c",&pStruct->sex);
printf("score:");
scanf("%d",&pStruct->score);
printf("love People:");
scanf("%s",&pStruct->lovep);
printf("Print:====================\n");
printf("name:%s\nsex:%c\nscore:%d\nLovePeople:%s\n",pStruct->Name,pStruct->sex,pStruct->score,pStruct->lovep); return ;
}

因为比较简单,所以都没有写上注释,练习的时候有1个点,

1.熟练掌握用指针来操作结构体变量的两种方式

C语言 指向结构体变量的指针的更多相关文章

  1. C/C++中指向结构体变量的指针,调用指向的那个结构体中的成员

    设p是指向结构体变量的指针,则可以通过以下的方式,调用指向的那个结构体中的成员: (1)结构体变量.成员名.如,stu.num. (2)(*p).成员名.如,(*p).num. (3)p->成员 ...

  2. C语言 指向结构体数组的指针

    当结构体指针变量指向一个结构体变量数组的时候,此时指针变量的值就是结构体数组的首地址 关于如何定义结构体数组,和将结构体指针指向结构体变量数组,不是重点. 重点是,明白结构体指针的是怎么移动的, 我个 ...

  3. c语言指向结构体数组的指针

    #include <stdio.h> #include <stdlib.h> struct dangdang { ]; ]; ]; int num; int bugnum; ] ...

  4. 将c语言的结构体定义变成对应的golang语言的结构体定义,并将golang语言结构体变量的指针传递给c语言,cast C struct to Go struct

    https://groups.google.com/forum/#!topic/golang-nuts/JkvR4dQy9t4 https://golang.org/misc/cgo/gmp/gmp. ...

  5. C语言中结构体变量之间赋值

    近期,我阅读了某新员工小刘写的C语言代码,发现其对结构体变量之间的赋值不是非常熟悉. 对于两个同样类型的结构体变量,他均採用的是逐个成员变量直接赋值的形式.例如以下的代码演示样例: 如上代码所看到的, ...

  6. c语言指向结构体的指针作为函数参数

    注意 这里包括形参和实参 struct dangdangtest { ]; int num; }; void change(int num)//值传递 新建一个变量接受传递的值 { num = ; } ...

  7. C语言_结构体变量指针做函数参数的使用案例

    # include <stdio.h> # include <stdlib.h> # include <string.h> # include <malloc ...

  8. c语言中结构体指针

    1.指向结构体的指针变量: C 语言中->是一个总体,它是用于指向结构体,如果我们在程序中定义了一个结构体,然后声明一个指针变量指向这个结构体.那么我们要用指针取出结构体中的数据.就要用到指向运 ...

  9. 01.C语言关于结构体的学习笔记

    我对于学习的C语言的结构体做一个小的学习总结,总结如下: 结构体:structure 结构体是一种用户自己建立的数据类型,由不同类型数据组成的组合型的数据结构.在其他高级语言中称为记录(record) ...

随机推荐

  1. dutacm.club 1094: 等差区间(RMQ区间最大、最小值,区间GCD)

    1094: 等差区间 Time Limit:5000/3000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total ...

  2. 学习LSM(Linux security module)之二:编写并运行一个简单的demo

    各种折腾,经过了一个蛋疼的周末,终于在Ubuntu14.04上运行了一个基于LSM的简单demo程序. 一:程序编写 先简单的看一下这个demo: //demo_lsm.c#include <l ...

  3. JAVA net 笔记

    1.InetAddress 获取主机ip等 2.URL 3.URLConnection (url.openConnection() 创建对象) 4.BufferedReader 5.InputStre ...

  4. 【动态规划】UVALive - 4888 - Railroad

    f(i,j)表示从A序列前面取i个,从B序列前面取j个时,能否拼成C序列.转移自行脑补. A train yard is a complex series of railroad tracks for ...

  5. 找出分数最高的前两个学生 Exercise05_09

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:找出分数最高的前两个学生 * */ public class Exerci ...

  6. 【R笔记】日期处理

    R语言学习笔记:日期处理 1.取出当前日期 Sys.Date() [1] "2014-10-29" date() #注意:这种方法返回的是字符串类型 [1] "Wed O ...

  7. Matlab设置字体大小

    1.  设置坐标轴上下限:axis([xmin,xmax,ymin,ymax]); 2.  设置图片大小:set(gcf,'Position',[x1,y1,dx,dy]); x1和y1是图的左下角坐 ...

  8. iOS页面跳转及数据传递

    转: http://blog.csdn.net/wang9834664/article/details/8025571 iOS页面跳转: 第一种 [self.navigationController  ...

  9. javascript模块化有什么意义?

    以前,所有的javascript都写在一个文件里,方便只加载一个文件就可以了,但是代码越来越多,必须分成多个文件加载,类似: <script src="1.js">&l ...

  10. sun.misc.BASE64Decoder导入异常及处理思路

    Java后台保存base64图片数据 使用byte[] bytes = new BASE64Decoder().decodeBuffer(str);需要引入sun.misc.BASE64Decoder ...