指针数组声明的问题

时间:2022-07-10 21:17:28

When i execute this code

当我执行这段代码时

#include<stdio.h>

int main() {
 int (*x)[5];
printf("\nx = %u\nx+1 = %u\n&x = %u\n&x + 1 = %u",x,x+1,&x,&x+1);
}

This is the output in C or C++:

这是C或c++的输出:

x = 134513520
x+1 = 134513540
&x = 3221191940
&x + 1 = 3221191944

Please explain. Also what is the difference between:

请解释一下。还有什么不同之处:

int x[5] and int (*x)[5] ?

int x[5]和int (*x)[5] ?

4 个解决方案

#1


7  

  • int x[5] is an array of 5 integers
  • int x[5]是一个由5个整数组成的数组
  • int (*x)[5] is a pointer to an array of 5 integers
  • [5]是一个指向5个整数的数组的指针

When you increment a pointer, you increment by the size of the pointed to type. x+1 is therefore 5*sizeof(int) bytes larger than just x - giving the 8048370 and 8048384 hex values with a difference of 0x14, or 20.

当您增加一个指针时,您将增加指向类型的大小。因此,x+1比x大5*sizeof(int)字节——给8048370和8048384个十六进制值,差值为0x14或20。

&x is a pointer to a pointer - so when you increment it you add sizeof(a pointer) bytes - this gives the bf9b08b4 and bf9b08b8 hex values, with a difference of 4.

&x是指向指针的指针——所以当你增加它时,你添加了sizeof(一个指针)字节——这就得到了bf9b08b4和bf9b08b8十六进制值,它们之间的差异是4。

#2


5  

  1. int x[5] is an array of 5 ints
  2. int x[5]是一个5 ints的数组
  3. int (*x)[5] is a pointer to an array of 5 ints
  4. int (*x)[5]是一个指向5 ints数组的指针
  5. int* x[5] is an array of 5 pointers to ints
  6. int* x[5]是一个5个指针的数组

#3


4  

int (*x)[5];

declares a pointer to an array.

声明指向数组的指针。

From the question title, you probably want

从题目中,你可能想要

int* x[5];

instead, which declares an array of pointers.

它声明一个指针数组。

int x[5];

declares a plain old array of ints.

声明了一组普通的ints。

#4


3  

int x[5];

declares an array of five ints.

声明一个包含5个int的数组。

int (*x)[5];

declares a pointer to an array of 5 ints.

声明一个指向5英寸数组的指针。

You might find cdecl.org useful.

你可能会发现cdecl.org网站很有用。

#1


7  

  • int x[5] is an array of 5 integers
  • int x[5]是一个由5个整数组成的数组
  • int (*x)[5] is a pointer to an array of 5 integers
  • [5]是一个指向5个整数的数组的指针

When you increment a pointer, you increment by the size of the pointed to type. x+1 is therefore 5*sizeof(int) bytes larger than just x - giving the 8048370 and 8048384 hex values with a difference of 0x14, or 20.

当您增加一个指针时,您将增加指向类型的大小。因此,x+1比x大5*sizeof(int)字节——给8048370和8048384个十六进制值,差值为0x14或20。

&x is a pointer to a pointer - so when you increment it you add sizeof(a pointer) bytes - this gives the bf9b08b4 and bf9b08b8 hex values, with a difference of 4.

&x是指向指针的指针——所以当你增加它时,你添加了sizeof(一个指针)字节——这就得到了bf9b08b4和bf9b08b8十六进制值,它们之间的差异是4。

#2


5  

  1. int x[5] is an array of 5 ints
  2. int x[5]是一个5 ints的数组
  3. int (*x)[5] is a pointer to an array of 5 ints
  4. int (*x)[5]是一个指向5 ints数组的指针
  5. int* x[5] is an array of 5 pointers to ints
  6. int* x[5]是一个5个指针的数组

#3


4  

int (*x)[5];

declares a pointer to an array.

声明指向数组的指针。

From the question title, you probably want

从题目中,你可能想要

int* x[5];

instead, which declares an array of pointers.

它声明一个指针数组。

int x[5];

declares a plain old array of ints.

声明了一组普通的ints。

#4


3  

int x[5];

declares an array of five ints.

声明一个包含5个int的数组。

int (*x)[5];

declares a pointer to an array of 5 ints.

声明一个指向5英寸数组的指针。

You might find cdecl.org useful.

你可能会发现cdecl.org网站很有用。