如何使用指针声明访问二维数组中的值

时间:2019-02-09 17:00:32

标签: c pointers

question1: 如何访问数组中的值2 (我不想使用像int t [] []这样的声明)

int *t[3] ={{1,2},{3,4},{5,6}};

question2:

int * t1 = {1,2,3}; // how this line of code compiles 

1 个答案:

答案 0 :(得分:0)

关于这个问题:

int * t1 = {1,2,3}; // how this line of code compiles 

以下是尝试编译的结果:

int main( void )
{
    int * t1 = {1,2,3};
}

我将其放入名为“ untitled2.c”的文件中

gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled2.c" 
untitled2.c: In function ‘main’:
untitled2.c:7:14: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
int * t1 = {1,2,3};
            ^
untitled2.c:7:14: note: (near initialization for ‘t1’)
untitled2.c:7:16: warning: excess elements in scalar initializer
int * t1 = {1,2,3};
              ^
untitled2.c:7:16: note: (near initialization for ‘t1’)
untitled2.c:7:18: warning: excess elements in scalar initializer
int * t1 = {1,2,3};
                ^
untitled2.c:7:18: note: (near initialization for ‘t1’)
untitled2.c:7:8: warning: unused variable ‘t1’ [-Wunused-variable]
int * t1 = {1,2,3};
      ^~

那么,它如何编译?不是