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{ ...
随机推荐
- SQL SERVER 创建作业
),, , , , , ),SERVERPROPERTY(N, , , ...
- js 获取div 图片高度
使用jquery获取网页中图片的高度其实很简单,有两种常用的方法都可以打到我们的目的 $("img").whith();(返回纯数字) $("img").css ...
- SA 的参数
SA 的参数也只能是常数数组. http://www.cnblogs.com/del/archive/2009/10/27/1590692.html ja := SA([]); jo := SO(); ...
- 【STL】-auto_ptr的用法
初始化: #include<memory> //auto_ptr header void f() { auto_ptr<classA> ptr(new classA); } 拷 ...
- sql alter表字段处理
--添加字段 ALTER table WCOLLECTION add CLT_ID int null default(0) --将已有字段类型为 NULL 修改为 NOT NULLalter tabl ...
- ctime、atime
Linux系统文件有三个主要的时间属性,分别是ctime(change time, 而不是create time), atime(access time), mtime(modify time).后来 ...
- Ohlàlà
Chap 1数数字 un 1 deux 2 trois 3 quatre 4 cinq 5 six 6 sept 7 huit 8 neuf 9 dix 10 Chap 2 讲地名 Paris 巴 ...
- android 原生dialog对话框
http://www.cnblogs.com/xiaoluo501395377/p/3419398.html
- iOS:图片拉伸不变形技巧
方法: 假设图片为60*24 CGFloat top = image.height*0.5-1; // 顶端盖高度 CGFloat bottom = top ; // 底端盖高度 CGFloat le ...
- java.lang.NoSuchMethodError: org.springframework.web.context.request.ServletRequestAttributes.<init>
今天学习 srping mvc 的配置 在核心版本对的情况下,把项目从Server中移除,然后重新加入即可.来源stack over flow