聚合数据类型

能够同时存储超过一个的单独数据。 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的更多相关文章

  1. C函数及指针学习1

    1 大段程序注释的方法 #if 0#endif 2三字母词 以两个问号 开始的都要注意 3 字面值(常量) 在整型号字面值后加 字符L (long),U(unsigned)说明字符常量 为长整型 或( ...

  2. c 函数及指针学习 10

    标准库函数 1算数运算stdlib.h 2随机数stdlib.h 3字符串转化stdlib.h 4数学函数 math.h 5日期和时间 time.h 6信号 signal.h 7打印可变参数列表std ...

  3. 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 ...

  4. c 函数及指针学习 7

    1.结构的存储分配 1 2 printf("%d \n",sizeof(char)); printf("%d \n",sizeof(int)); int 类型为 ...

  5. c 函数及指针学习 4

    1数组和指针声明的差别 声明数组:为数组分配内存,为数组名分配内存(指针常量 4个字节) 指针:为指针分配内存(指针变量 4个字节) 1 2 3 4 5 6 7 8 9 10 #include < ...

  6. c 函数及指针学习 3

    strlen(x) 返回 size_t 类型,size_t是 unsigned int 类型,所以 strlen(x)-strlen(y) 返回 unsigned int 始终 >=0 1 2 ...

  7. C函数及指针学习2

    1.break  永久终止循环,continue 结束当前循环 2.switch 每个case标签要有唯一值,(且为常量或常量表达式) 不加break 时执行流会贯穿整个case 标签 3 赋值操作符 ...

  8. c 函数及指针学习 8

    联合体 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h>   union sa     {     double a;     int b; ...

  9. 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{ ...

随机推荐

  1. Codeforce385C 树状数组+素因子分解

    题目大意: 给多个区间的询问,在询问区间内每一个出现的素数去计算所有数中有多少个数能被这个素数整除 然后将所有素数得到的对应值求和 这里因为初始给定的数不超过10000000,最多670000不到的素 ...

  2. 单人SVN提交bug

    The working copy "初识tableVIew" failed to commit files. fatal: Unable to create '/Users/zjj ...

  3. 在网页中编辑报表的报表设计器Stimulsoft Reports Designer.Web报表控件

    Stimulsoft Reports Designer.Web报表控件是一款网页报表设计器.您想在网页中编辑您的报表吗?现在是可能的! Stimulsoft Reports Designer.Web ...

  4. android开发艺术探索

    android开发艺术探索  百度任玉刚 http://blog.csdn.net/singwhatiwanna/article/details/46810527

  5. lightoj1027

    //Accepted 1688 KB 0 ms //概率简单题 //假设我们在n个门前加个起点,在n个门后加个终点,起点可以到达n个门, //为正的门可以到达终点,为负的回到起点 //则假设我们从起点 ...

  6. stm32 dac库函数解读

    1.简述: 12位数字输入,电压输出,DAC可以配置为8位或12位模式.有2个输出通道.在双DAC模式下,两个通道可以独立地工作. 特殊功能: 噪声波形生成,三角波形生成,外部触发转换,双DAC同时或 ...

  7. android-volley-at-a-glance

    http://liubin.org/2013/05/27/android-volley-at-a-glance/ http://www.androidhive.info/2014/05/android ...

  8. sap 根据TOCE找 USER_EXIT

    *&---------------------------------------------------------------------* *& Report  ZUSER_EX ...

  9. 2016 - 1- 22 Build a Nav bar (intro to HTML&CSS)

    一:Learn how to build a NavBar --- allow user navigate ur site 1. The hypetext refrence link This att ...

  10. LeetCode---- 二叉树中,找出和为某值的所有路径

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...