鱼C论坛

 找回密码
 立即注册
查看: 970|回复: 2

[已解决]关于强制转换

[复制链接]
发表于 2020-5-10 20:35:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int array[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  5.         int (*p)[3] = (int (*)[3])&array;

  6.         printf("%d\n", p[2][2]);

  7.         return 0;
  8. }
复制代码


请问这个强制转换的格式是    :(数据类型)这个格式吗
这个代码中应该是强制转换成一个指向一个一维数组的数组指针,为什么说强制把array转化成了一个二维数组,如果转换成指向一个二维数组的话的指针的话,应该是(int(*)【3】【3】)
,没明白这个是怎么用的强制转换,越详细越好,麻烦大家了。谢谢
最佳答案
2020-5-10 22:24:52
这里我看指针数组那一部分知识将它这样理解:
首先
int (*p)[3]定义一个含有3个元素的指针数组,这3个元素都是指向一个数组的指针,然后(int(*)[3])&array将array强制转换成一个指向数组的指针;最后对应着有p[0]=&array,p[1]=&array[1],p[2]=&array[2]。个人理解,有错误请多指点
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-10 21:07:44 | 显示全部楼层
给你两个程序,你来观察这两个程序的区别
注意,我把 test5 注释掉了,因为这个函数是错误的

  1. #include <stdio.h>

  2. #define LEN 5

  3. void test1(int data[]) {
  4.     for(int i = 0; i < LEN; ++i)
  5.         printf("%d ", data[i]);
  6.     printf("\n");
  7. }

  8. void test2(int data[LEN]) {
  9.     for(int i = 0; i < LEN; ++i)
  10.         printf("%d ", data[i]);
  11.     printf("\n");
  12. }

  13. void test3(int data[5]) {
  14.     for(int i = 0; i < LEN; ++i)
  15.         printf("%d ", data[i]);
  16.     printf("\n");
  17. }

  18. void test4(int *data) {
  19.     for(int i = 0; i < LEN; ++i)
  20.         printf("%d ", data[i]);
  21.     printf("\n");
  22. }

  23. int main(void) {
  24.     int data[LEN] = {1, 2, 3, 4 ,5};
  25.     test1(data);
  26.     test2(data);
  27.     test3(data);
  28.     test4(data);
  29.     return 0;
  30. }
复制代码

  1. #include <stdio.h>

  2. #define LEN 3

  3. void test1(int data[][5]) {
  4.     for(int y = 0; y < LEN; ++y) {
  5.         for(int x = 0; x < 5; ++x) {
  6.             printf("%d ", data[y][x]);
  7.         }
  8.         printf("\n");
  9.     }
  10.     printf("\n");
  11. }

  12. void test2(int data[LEN][5]) {
  13.     for(int y = 0; y < LEN; ++y) {
  14.         for(int x = 0; x < 5; ++x) {
  15.             printf("%d ", data[y][x]);
  16.         }
  17.         printf("\n");
  18.     }
  19.     printf("\n");
  20. }

  21. void test3(int data[3][5]) {
  22.     for(int y = 0; y < LEN; ++y) {
  23.         for(int x = 0; x < 5; ++x) {
  24.             printf("%d ", data[y][x]);
  25.         }
  26.         printf("\n");
  27.     }
  28.     printf("\n");
  29. }

  30. void test4(int (*data)[5]) {
  31.     for(int y = 0; y < LEN; ++y) {
  32.         for(int x = 0; x < 5; ++x) {
  33.             printf("%d ", data[y][x]);
  34.         }
  35.         printf("\n");
  36.     }
  37.     printf("\n");
  38. }

  39. /*
  40. void test5(int *data[5]) {
  41.     for(int y = 0; y < LEN; ++y) {
  42.         for(int x = 0; x < 5; ++x) {
  43.             printf("%d ", data[y][x]);
  44.         }
  45.         printf("\n");
  46.     }
  47.     printf("\n");
  48. }
  49. */

  50. int main(void) {
  51.     int data[LEN][5] = {
  52.         {11, 12, 13, 14 ,15},
  53.         {21, 22, 23, 24 ,25},
  54.         {31, 32, 33, 34 ,35}
  55.     };

  56.     test1(data);
  57.     test2(data);
  58.     test3(data);
  59.     test4(data);
  60.     //test5(data);
  61.     return 0;
  62. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-10 22:24:52 | 显示全部楼层    本楼为最佳答案   
这里我看指针数组那一部分知识将它这样理解:
首先
int (*p)[3]定义一个含有3个元素的指针数组,这3个元素都是指向一个数组的指针,然后(int(*)[3])&array将array强制转换成一个指向数组的指针;最后对应着有p[0]=&array,p[1]=&array[1],p[2]=&array[2]。个人理解,有错误请多指点
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 18:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表