c 函数及指针学习 5
聚合数据类型
能够同时存储超过一个的单独数据。 c语言提供了数组和结构体。
1.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <stdio.h>#include <math.h>void main(){struct { int a; }x,*b;int c[2]={1,2};x.a=1;b=c;printf("%d \n",b[1]);printf("%d \n",x.a);} |
|
1
2
|
warning C4133: '=' : incompatible types - from 'int *' to 'struct *'Linking... |
为了证明,指针变量未初始化时,只分配了指针的4个字节的内存空间,上面的程序运行后

2.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <stdio.h>#include <math.h>void main(){struct { int a; int b; }x;struct { int a; int b; }*b;b=&x;} |
|
1
|
warning C4133: '=' : incompatible types - from 'struct *' to 'struct *' |
即使成员列表完全相同,编译器仍将其当作不同的结构体。
可以这样实现
|
1
2
3
4
5
6
7
|
struct sa{ int a; int b; };struct sa x;struct sa *b;//sa称作标签b=&x; |
还可以这样
|
1
2
3
4
5
6
7
|
typedef struct { int a; int b; }sa;sa x;sa *d;d=&x; |
一般都这么做,可以将其放在一个头文件中。
3
结构的自引用
|
1
2
3
4
5
|
struct sa{ int a; int b; struct sa sb; }; |
|
1
|
error C2079: 'sb' uses undefined struct 'sa' |
结构体的长度是没办法确定的,(产生了无穷的递归)
|
1
2
3
4
5
|
struct sa{ int a; int b; struct sa *sb; }; |
结构体的长度是确定的,指针的长度始终为4个字节
进一步说明了 指针变量和 聚合数据类型名(数组名,结构体名的区别)
|
1
2
3
4
5
|
typedef struct { int a; int b; ss *sb; }ss; |
这么定义是错误的,在结构体内部ss还未定义。
应该这么定义
|
1
2
3
4
5
|
typedef struct sa{ int a; int b; struct sa *sb; }ss; |
c 函数及指针学习 5的更多相关文章
- C函数及指针学习1
1 大段程序注释的方法 #if 0#endif 2三字母词 以两个问号 开始的都要注意 3 字面值(常量) 在整型号字面值后加 字符L (long),U(unsigned)说明字符常量 为长整型 或( ...
- c 函数及指针学习 10
标准库函数 1算数运算stdlib.h 2随机数stdlib.h 3字符串转化stdlib.h 4数学函数 math.h 5日期和时间 time.h 6信号 signal.h 7打印可变参数列表std ...
- c 函数及指针学习 9
指针的高级应用 处理命令行参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <stdio.h> int main(int ar ...
- c 函数及指针学习 7
1.结构的存储分配 1 2 printf("%d \n",sizeof(char)); printf("%d \n",sizeof(int)); int 类型为 ...
- c 函数及指针学习 4
1数组和指针声明的差别 声明数组:为数组分配内存,为数组名分配内存(指针常量 4个字节) 指针:为指针分配内存(指针变量 4个字节) 1 2 3 4 5 6 7 8 9 10 #include < ...
- c 函数及指针学习 3
strlen(x) 返回 size_t 类型,size_t是 unsigned int 类型,所以 strlen(x)-strlen(y) 返回 unsigned int 始终 >=0 1 2 ...
- C函数及指针学习2
1.break 永久终止循环,continue 结束当前循环 2.switch 每个case标签要有唯一值,(且为常量或常量表达式) 不加break 时执行流会贯穿整个case 标签 3 赋值操作符 ...
- c 函数及指针学习 8
联合体 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> union sa { double a; int b; ...
- c 函数及指针学习 6
不完整声明 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 /* 方法一 */ struct tag_a{ ...
随机推荐
- java基础之 switch
switch 语句的格式: switch ( 整型或字符型变量 ) { case 变量可能值1 : 分支一; break; case 变量可能值2 : 分支二; break; case 变量可 ...
- touch ImageView
package com.example.touchdemo; import android.os.Bundle;import android.app.Activity;import android.u ...
- matlab 画框(一)
matlab进行图像处理之后,很多时候需要在图像上画出矩形框:如,调用matlab的某个检测函数,得到结果之后,往往需要将检测结果的矩形框画在图像上,直观.方便的进行查看:下面的代码就是这个目的: f ...
- [windows操作系统]windows模块
smss.exe csrss.exe Client/Server Runtime Server Subsystem
- 按Right-BICEP要求的任务二的测试
测试方法:Right-BICEP 测试计划: 1.Right-结果是否正确? 2.B-是否所有的边界条件都是正确的? 3.P-是否满足性能要求? 4.是否有乘除法? 5.是否有括号? 6.是否有输出方 ...
- ulink 固件更新问题
1前言 ulink版本升级.. 当使用keil4.7.4版本时,ulink固件库更新了. 2官方资料解释: Boot Mode Utility Home » Appendix » Utilities ...
- (转)深入浅出 iOS 之生命周期
原文:http://www.cocoachina.com/applenews/devnews/2011/0817/3129.html 深入浅出 iOS 之生命周期 发布于:2011-08-17 10: ...
- php大力力 [014节] 八杆子打不着的非技术文章,哈哈
今天似乎是个值得纪念的日子 一个创业失败者的忠告:请不要再参加创业活动 PHP mysql_connect() 函数 mysql默认用户 随意发几个链接,没任何意义
- 查看ubuntu文件目录的大小和文件夹包含的文件数 zT
查看ubuntu文件目录的大小和文件夹包含的文件数 查看linux文件目录的大小和文件夹包含的文件数 统计总数大小 du -sh xmldb/ du -sm * | sort -n //统计当前目录大 ...
- Mac抓包工具Charles
一.下载 先到它的官网http://www.charlesproxy.com/可下载到最新版本,这个下载有点慢,我已经将它放到网盘中了:http://pan.baidu.com/s/1gdu0S4V ...