C - 指向数组的分段错误

时间:2021-02-27 08:58:05

i'm hoping someone can help me with this little piece of code. It's a stupid test, but i dont know what is it that i am doing wrong. It's like this:

我希望有人能用这段代码帮助我。这是一个愚蠢的测试,但我不知道我做错了什么。就像这样:

#include <stdio.h>

int **ipp;

int ventas [3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

int main(void){

    ipp = (int **)ventas;

    printf("%d\n", **ipp);

    return 0;
}

It compiles (I'm using GCC), but when I execute it I keep getting a segmentation fault. What am I doing wrong? I think it has something to do with an un-initalized pointer, but 'ventas' is an array so it is already initialized, and it's assigned to **ipp.

它编译(我正在使用GCC),但是当我执行它时,我不断遇到分段错误。我究竟做错了什么?我认为它与非初始化指针有关,但'ventas'是一个数组,因此它已经被初始化,并且被分配给** ipp。

3 个解决方案

#1


1  

  • A pointer-to-pointer is not an array. Nor is it a pointer to a 2D array. They aren't the slightest compatible. Just forget about pointer-to-pointers in this case.

    指向指针的指针不是数组。它也不是指向2D数组的指针。他们没有丝毫兼容。在这种情况下忘记指针指针。

  • If you want a pointer to a 2D array, you must write int (*ipp)[3][4] = &ventas;

    如果你想要一个指向2D数组的指针,你必须写int(* ipp)[3] [4] =&ventas;

  • You can't print a pointer using %d format specifier. You should use %p.

    您无法使用%d格式说明符打印指针。你应该使用%p。

Corrected code for printing the address of the 2D array:

修正了打印2D数组地址的代码:

#include <stdio.h>

int main(void){
  int ventas [3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
  int (*ipp)[3][4];

  ipp = &ventas;
  printf("%p\n", ipp);

  return 0;
}

#2


1  

A pointer to pointer and a 2D array are not interchangeable, change to:

指向指针和2D数组的指针不可互换,请更改为:

int (*ipp)[4]; /* A pointer to an array of 4 ints */
...
ipp = ventas;

#3


1  

When you have such casting like

当你有这样的铸造像

ipp = (int **)ventas;

then the value of the variable is the address of the first element of the array ventas. In this case after dereferencing the pointer

那么变量的值是数组ventas的第一个元素的地址。在这种情况下解除引用指针后

*ipp

you get the value stored at this address. If to assume that sizeof( int ) is equal to sizeof( int * ) than the first element of the array equal to 1 is considered as a memory address. After applying second dereferencing

您将获得存储在此地址的值。如果假设sizeof(int)等于sizeof(int *),则等于1的数组的第一个元素被视为内存地址。应用第二次解除引用后

**ipp

you get memory access violation.

你得到内存访问冲突。

It will be correct to write either like

写任何一个都是正确的

int ( *ipp )[4] = ventas;

and then

printf("%d\n", **ipp);

or like

int *ipp = ( int * )ventas;

and then

printf("%d\n", *ipp);

#1


1  

  • A pointer-to-pointer is not an array. Nor is it a pointer to a 2D array. They aren't the slightest compatible. Just forget about pointer-to-pointers in this case.

    指向指针的指针不是数组。它也不是指向2D数组的指针。他们没有丝毫兼容。在这种情况下忘记指针指针。

  • If you want a pointer to a 2D array, you must write int (*ipp)[3][4] = &ventas;

    如果你想要一个指向2D数组的指针,你必须写int(* ipp)[3] [4] =&ventas;

  • You can't print a pointer using %d format specifier. You should use %p.

    您无法使用%d格式说明符打印指针。你应该使用%p。

Corrected code for printing the address of the 2D array:

修正了打印2D数组地址的代码:

#include <stdio.h>

int main(void){
  int ventas [3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
  int (*ipp)[3][4];

  ipp = &ventas;
  printf("%p\n", ipp);

  return 0;
}

#2


1  

A pointer to pointer and a 2D array are not interchangeable, change to:

指向指针和2D数组的指针不可互换,请更改为:

int (*ipp)[4]; /* A pointer to an array of 4 ints */
...
ipp = ventas;

#3


1  

When you have such casting like

当你有这样的铸造像

ipp = (int **)ventas;

then the value of the variable is the address of the first element of the array ventas. In this case after dereferencing the pointer

那么变量的值是数组ventas的第一个元素的地址。在这种情况下解除引用指针后

*ipp

you get the value stored at this address. If to assume that sizeof( int ) is equal to sizeof( int * ) than the first element of the array equal to 1 is considered as a memory address. After applying second dereferencing

您将获得存储在此地址的值。如果假设sizeof(int)等于sizeof(int *),则等于1的数组的第一个元素被视为内存地址。应用第二次解除引用后

**ipp

you get memory access violation.

你得到内存访问冲突。

It will be correct to write either like

写任何一个都是正确的

int ( *ipp )[4] = ventas;

and then

printf("%d\n", **ipp);

or like

int *ipp = ( int * )ventas;

and then

printf("%d\n", *ipp);