鱼C论坛

 找回密码
 立即注册
查看: 1154|回复: 5

[已解决]一个二维数组赋给int *p问题

[复制链接]
发表于 2021-4-20 14:20:45 | 显示全部楼层 |阅读模式

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

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

x
#include <stdio.h>

void main(){
    int numbers[][2] = {1,2,3,4,5,6,7,8,9,10,11,12};
    int *p = numbers;
    printf("address for numbers: %p\n", p);
    printf("values for numbers[0]:%d\n",*(p+1));
    printf("sizeof  for numbers:%d\n",sizeof numbers);
    for(int i=0;i < (sizeof(numbers)/sizeof(int));i++){
        printf("The values for numbers[%d]:%d\n", i, *(p+i));
    }
}

如题, 在cloin下能正常 运行, 但是这个代码有没有问题呢?
比如int *p=numbers这条语句, 赋值有没有问题?两边的类型相同吗?
最佳答案
2021-4-20 14:29:12
你的代码出现下面的问题
main.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    3 | void main(){
      |      ^~~~
main.c: In function ‘main’:
main.c:4:24: warning: missing braces around initializer [-Wmissing-braces]
    4 |     int numbers[][2] = {1,2,3,4,5,6,7,8,9,10,11,12};
      |                        ^
      |                         {  }{  }{  }{  }{   }{    }
main.c:5:14: warning: initialization of ‘int *’ from incompatible pointer type ‘int (*)[2]’ [-Wincompatible-pointer-types]
    5 |     int *p = numbers;
      |              ^~~~~~~
main.c:8:34: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
    8 |     printf("sizeof  for numbers:%d\n",sizeof numbers);
      |                                 ~^    ~~~~~~~~~~~~~~
      |                                  |    |
      |                                  int  long unsigned int
      |                                 %ld
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-4-20 14:21:18 | 显示全部楼层
输出如下:
address for numbers: 0x7ffdefd38400
values for numbers[0]:2
sizeof  for numbers:48
The values for numbers[0]:1
The values for numbers[1]:2
The values for numbers[2]:3
The values for numbers[3]:4
The values for numbers[4]:5
The values for numbers[5]:6
The values for numbers[6]:7
The values for numbers[7]:8
The values for numbers[8]:9
The values for numbers[9]:10
The values for numbers[10]:11
The values for numbers[11]:12

Process finished with exit code 12
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-20 14:28:21 | 显示全部楼层
你用的教材有点老了,换一个吧
一开始学到的是错误的知识,之后想要改变是非常困难的
#include <stdio.h>

int main(void) {
    int numbers[][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}};
    int (*p)[2] = numbers;
    printf("address for numbers: %p\n", p);
    printf("values for numbers[0]:%p\n",*(p+1));
    printf("sizeof  for numbers:%lu\n",sizeof numbers);
    for(size_t i=0;i < (sizeof(numbers)/sizeof(int));i++) {
        printf("The values for numbers[%lu]:%p\n", i, *(p+i));
    }
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-20 14:29:12 | 显示全部楼层    本楼为最佳答案   
你的代码出现下面的问题
main.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    3 | void main(){
      |      ^~~~
main.c: In function ‘main’:
main.c:4:24: warning: missing braces around initializer [-Wmissing-braces]
    4 |     int numbers[][2] = {1,2,3,4,5,6,7,8,9,10,11,12};
      |                        ^
      |                         {  }{  }{  }{  }{   }{    }
main.c:5:14: warning: initialization of ‘int *’ from incompatible pointer type ‘int (*)[2]’ [-Wincompatible-pointer-types]
    5 |     int *p = numbers;
      |              ^~~~~~~
main.c:8:34: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
    8 |     printf("sizeof  for numbers:%d\n",sizeof numbers);
      |                                 ~^    ~~~~~~~~~~~~~~
      |                                  |    |
      |                                  int  long unsigned int
      |                                 %ld
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-20 14:37:09 | 显示全部楼层
人造人 发表于 2021-4-20 14:29
你的代码出现下面的问题

怎么我编译器没报错呢, 奇怪了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-20 14:47:15 | 显示全部楼层
喜欢散步 发表于 2021-4-20 14:37
怎么我编译器没报错呢, 奇怪了

我用的gcc,编译命令是 gcc -g -Wall -o main main.c
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 15:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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