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{ ...
随机推荐
- Memcached 及 Redis 架构分析和比较
Memcached和Redis作为两种Inmemory的key-value数据库,在设计和思想方面有着很多共通的地方,功能和应用方面在很多场合下(作为分布式缓存服务器使用等) 也很相似,在这里把两者放 ...
- zabbix3.0部署(LAMP)
0.1 初始化 #!/bin/sh yum clean all systemctl stop firewalld.service systemctl disable firewalld.service ...
- [转载]Android核心分析
2013-12-19 15:44:03 转载自: http://blog.csdn.net/column/details/androidcore.html 很好的文章,阅读请跳转到转载链接,转载备以后 ...
- 用ant进行编译和打包(java)
ant是目前java环境下最好用的打包部署工具,其采用xml的格式进行编写,功能非常强大.现介绍一下如何手工使用ant进行java程序的编译打包.一.安装ant1.下载并安装ant.到官方主页http ...
- JDBC 连接池
数据库连接池(connection pool) JDBC数据库连接池的必要性 在使用开发基于数据库的web程序时,传统的模式基本是按以下步骤: 在主程序(如servlet.beans)中建立数据库连接 ...
- 经典线程同步 互斥量Mutex
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...
- 更改layout的布局
创建了一个layout,缺省都是LinearLayout,原来都在xml把他改成其他布局,习惯了拖放的方式觉得挺麻烦. 其实vs2013有这个功能,就是在设计界面单机右键可以删除布局,然后拖一个布局进 ...
- form表单验证2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 扩展jQuery easyui datagrid增加动态改变列编辑的类型
$.extend($.fn.datagrid.methods, { addEditor : function(jq, param) { if (param instanceof Array) { $. ...
- openstack中运行定时任务的两种方法及源代码分析
启动一个进程,如要想要这个进程的某个方法定时得进行执行的话,在openstack有两种方式: 一种是通过继承 periodic_task.PeriodicTasks,另一种是使用loopingcall ...