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{ ...
随机推荐
- 从报错“无效操作,连接被关闭”探究Transaction的Timeout超时机制
1.报错如下:Invalid Operation the connection is closed,无效操作,连接被关闭.这个错误是并不是每次都报,只有在复杂操作.大事务的情况下才偶然报出来. sta ...
- POJ 2992 求组合数的因子个数
求C(n,k)的因子个数 C(n,k) = (n*(n-1)*...*(n-k+1))/(1*2*...*k) = p1^k1 * p2^k2 * ... * pt^kt 这里只要计算出分子中素数因子 ...
- 各种注释--------html,jsp
1. <!--到梦之都XHTML教程的链接--> <a href="http://www.dreamdu.com/xhtml/"> 学习XHTML < ...
- SQL Server 语句整理
1. 创建数据库 create database dbName 2. 删除数据库 drop database dbName 3. 备份sql server --- 创建 备份数据的 device US ...
- SharePoint 2013 搜索体系结构
博客地址:http://blog.csdn.net/FoxDave 本文参考自微软官方的Chart,记录一下,算是自己对这部分知识的总结. Microsoft® SharePoint® Server ...
- joinfetch之意义
既然被join的对象早晚都要用到,为什么要先从A表取这边的独享,再根据关联关系取B表中的对象,分两次或者多次进行,增加数据库的负载呢? 为什么不把A表和B表join成一张表,从这个组合表中把要取的对象 ...
- 收藏的博客--Ogre
1.Ogre一些东西和流程深入讲解 http://blog.csdn.net/yanonsoftware/article/category/226048 2.Ogre天龙八部分析 http://bl ...
- (转)HTML5开发学习(2):本地存储之localStorage 、sessionStorage、globalStorage
原文:http://www.cnblogs.com/xumingxiang/archive/2012/03/25/2416386.html HTML5开发学习(2):本地存储之localStorage ...
- 2016 - 1- 19 GCD单例模式
一:单例模式的作用: 1.可以保证在程序运行过程中,一个类只有一个实例,而且易于外界访问.2 2.从而方便的控制了实例的个数,节约系统资源. 二:单例模式的应用场景: 代码: 1.在一个需要实现单例模 ...
- IOS聊天对话界面
大家好,百忙之中,抽出点空,写个微博,话说好久没写. 最近项目中有碰到写类似微信聊天界面上的效果,特整理了一下,写了一个小的Demo,希望给没头绪的同学们一个参考! 下载地址:http://files ...