指针数组。为什么ptr [1]内的值和ptr [1]的地址每次都相同。对于ptr [0],ptr [2] ... ptr [n],观察到相同的行为。 [英] Array of pointers. Why value inside ptr[1] and address of ptr[1] is same every time. Same behavior is observed for ptr[0], ptr[2]...ptr[n].

查看:132
本文介绍了指针数组。为什么ptr [1]内的值和ptr [1]的地址每次都相同。对于ptr [0],ptr [2] ... ptr [n],观察到相同的行为。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>

int main()
{

int var[5]={10,20,30,40};
int(*ptr)[5];//array of pointer.

ptr=&var;
printf("Address of var : %u\n",&var);
printf("Value of var[0] is : %d\n",*(&var[0]));
printf("Value inside ptr after ptr=&var : %u\n",ptr);
printf("Value of ptr[0] is : %u\n",*(&ptr[0]));
printf("Adress of ptr[0] is : %u\n",&ptr[0]);

printf("Value of ptr[1] is : %u\n",*(&ptr[1]));
printf("Adress of ptr[1] is : %u\n",&ptr[1]);

printf("Value of ptr[2] is : %u\n",*(&ptr[2]));
printf("Adress of ptr[2] is : %u\n",&ptr[2]);
}

推荐答案

正如您已经了解自己的声明是错误的。但似乎并不理解为什么它是错误的

重点是你没有声明一个指针数组,而是指向一个指针数组整数数组

括号的使用强调了声明变量'ptr'的指针性质。

请参阅下面的代码,其中显示了声明的声明:

1.指向int的指针数组
2.指向int数组的指针
3.指向数组的指针指向int的指示

代码也显示了它们的初始化。

As you have already understood by yourself your declaration is wrong. But doesn't seem that you understood also why it is wrong.
The point is that you have not declared an array of pointers, but a pointer to an array of integers!
The use of brackets emphasized the pointer nature of the declared variable 'ptr'.
Please see the following code that shows the declarations of:
1. An array of pointers to int
2. A pointer to an array of int
3. A pointer to an array of pointers to int
The code shows also the initialization of them.
//Case array of pointers to int
int *ptr[5];    //ptr is an array of 5 pointers to int
int var=100;
int var1 = 1, var2=2, var3=3, var4=4;
ptr[0]=&var;
ptr[1]=&var1;
ptr[2]=&var2;
ptr[3]=&var3;
ptr[4]=&var4;

//case pointer to array of integers
int (*ptr1)[5]; //ptr is a pointer to an array of 5 integers.
int arr[5]={10,20,30,40,50};        //array of integers
ptr1 = &arr;

//case pointer to array of pointers to integer
int *(*ptr2)[5];  //ptr is a pointer to an array of 5 pointers to int
ptr2 = &ptr;



现在回到你的代码有很多错误,因为如果你的意图是将'ptr'声明为指向int的指针数组,你就无法分配给它是一个int的地址。

任何体面的编译器都应该抛出一堆错误并发出很多警告。您是否仔细阅读过?


Now back to your code there are many errors, because if your intent was to declare 'ptr' as an array of pointers to int you cannot assign to it the address of an int.
Any decent compiler should have throw a bunch of errors an a lot of warnings. Have you read them carefully?


更改以下代码



Change below code

printf("Value of ptr[1] is : %u\n",*(&ptr[1]));
printf("Adress of ptr[1] is : %u\n",&ptr[1]);





这个





with this

printf("Value of ptr[1] is : %u\n",*(ptr[1]);
printf("Adress of ptr[1] is : %u\n",ptr[1]);





看看你自己做错了什么!:)



and see yourself what you were doing wrong! :)

在某些情况下,数组名称可用作指针。你可能会这样看: http://stackoverflow.com/questions/1641957/is -array-name-a-pointer-in-c [ ^ ]。



由于printf是用...声明的参数,因此没有验证编译时间也没有任何转换因此你必须知道它是如何工作的。



总之,数组& array 是该上下文中的同义词。数组已经是指针。
Array name can be used as a pointer in some cases. You might look this: http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c[^].

And since printf is declared with ... for arguments, there is no validation at compile time nor any conversion and thus you have to know how it works.

In summary, array and &array are synonym in that context. An array is already a pointer.


这篇关于指针数组。为什么ptr [1]内的值和ptr [1]的地址每次都相同。对于ptr [0],ptr [2] ... ptr [n],观察到相同的行为。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆