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{ ...
随机推荐
- 关于Tcp,为什么一定要进行三次握手呢?
主要是防止已经失效的请求报文段突然又传送到了服务端而产生的连接的误判. 考虑如下的情况:客户端发送了一个连接请求报文段到服务端,但是在某些网络节点上长时间滞留了,而后客户端又超时重发了一个连接请求报文 ...
- <转>提高iOS开发效率的方法和工具
介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先要说的肯定是IDE了,说到IDE,Xcode不能跑,当然你也可能同时在使用AppCode等其他的ID ...
- linux的简单网络配置
1,修改IP edit file: # if rh family system /etc/sysconfig/network-scripts/ifcfg-eth0 (eth0可能会是别的名字) # i ...
- 用C#感受MongoDB MapReduce之魅力 转
MapReduce这个名词随着hadoop的用户的增多,越来越被人关注.MapReduce可谓MongoDB之中的亮点,我也想深入了解MapReduce,加上MongoDB操作简单,所以就选择了它.M ...
- ios开发值json数据文件的存取
将Json存进本地文件夹 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainM ...
- JSON解析和XML解析
一. XML:用到一个开源解析类,GDataXMLNode(将其加入项目中),添加libxml2.dylib框架 经常用到的方法: 1.- (id)initWithXMLString:(NSStrin ...
- oracle视图如何使用
1.视图的概述 视图其实就是一条查询sql语句,用于显示一个或多个表或其他视图中的相关数据.视图将一个查询的结果作为一个表来使用,因此视图可以被看作是存储的查询或一个虚拟表.视图来源于表,所有对视图数 ...
- struts2常见配置
<struts> <!--开发模式设置:该属性设置Struts2应用是否使用开发模式.如果设置该属性为true,则可以在应用出错时显示更多.更友好的出错提示.该属性只接受true和f ...
- eclipse安卓模拟器窗口大小调整
引自百度经验的链接: http://jingyan.baidu.com/article/3aed632e18c7e97011809161.html
- BZOJ 1954 The xor-longest Path
问题转化为一些数里面选两个数异或和最大. #include<iostream> #include<cstdio> #include<cstring> #includ ...