指针 - 数组和指针的区别 [英] Pointers - Difference between Array and Pointer

查看:132
本文介绍了指针 - 数组和指针的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么放大器一之间的差,及;一,第一元件的地址一个[0]?
同样p是一个指向一个整数,但指定为阵列地址。
这是否指针[]会做指针算法和获取价值按数据类型?
而且什么样的价值不指望*?它应该是一个指针?

 #包括LT&;&stdio.h中GT;
 诠释的main()
{诠释一个[] = {5,6,7,8};
INT * P = A;
的printf(\\ n这是一个%U的地址,而与安培值;一%U的第一个元素%U地址,价值指向由%U型,一,&安培; A,&安培; A [0],*一个);
的printf(\\ n这是P%的地址U,在P%值u并用p%d个指向的价值,&安培; P,P,* P);
的printf(\\ n);
}这是一个3219815716的地址,值和放大器;第一个元素3219815716的3219815716,地址,价值指向由5
这是在P 3219815712在P 3219815716的地址,价值和由第5页指出的值


解决方案

  int类型的[] = {5,6,7,8};
INT * P = A;

请注意,在阵列(在大多数情况下)的情况下,说阵列 A ADDRESS_OF A 是一样的 ADDRESS_OF 的array.ie的第一要素, ADDRESS_OF(一)是一样的 ADDRESS_OF(A [0])&安培; ADDRESS_OF 运营商,因此在数组的情况下, A &放大器;一个&放大器;一个[0] 都是一样的。

我已强调,在大多数情况下,一个阵列的名称被变换成它的第一个元件的地址;当它的sizeof的操作数,如果做的东西使用malloc是工作,这是至关重要的一个显着的例外是。另一种情况是,当一个数组名的&放的操作;地址运算符。在这里,它被转换成整个数组的地址。有什么不同?即使你认为地址会以某种方式'一样',关键的区别是,他们有不同的类型。对于类型T的n个元素的数组,那么第一个元素的地址的类型为指向T';整个数组的地址的类型指针类型T的n个元素的数组';显然有很大不同。

下面是它的一个例子:

  INT AR [10];
为int *的IP;
INT(* ar10i)[10]; / *指向10个整数数组* /IP = AR; / *第一个元素的地址* /
IP =安培;芳[0]; / *第一个元素的地址* /
ar10i =放大器; AR; / *整个阵列的地址* /

有关详细信息,您可以参阅的C书。

What is the difference between a,&a,Address of first element a[0] ? Similarly p is a pointer to an integer but assigned as array address . Does that pointer[] would do pointer arithmetic and fetch the value as per the datatype ? Further what value does * expect ? Should it be a pointer ?

#include<stdio.h> 
 int main()
{

int a[]={5,6,7,8};
int *p= a;
printf("\nThis is the address of a %u, value of &a %u ,Address of first element %u ,    Value pointed by a %u",a,&a,&a[0],*a);
printf("\nThis is the address at p %u , value at p %u and the value pointed by p %d",&p,p,*p);
printf("\n");
}

This is the address of a 3219815716, value of &a 3219815716 ,Address of first element  3219815716 , Value pointed by a 5
This is the address at p 3219815712 , value at p 3219815716 and the value pointed by p 5

解决方案

int a[]={5,6,7,8};
int *p= a;

Note that in case of arrays(In most cases), say array a, the ADDRESS_OF a is same as ADDRESS_OF first element of the array.ie, ADDRESS_OF(a) is same as ADDRESS_OF(a[0]). & is the ADDRESS_OF operator and hence in case of an array a, &a and &a[0] are all the same.

I have already emphasized that in most cases, the name of an array is converted into the address of its first element; one notable exception being when it is the operand of sizeof, which is essential if the stuff to do with malloc is to work. Another case is when an array name is the operand of the & address-of operator. Here, it is converted into the address of the whole array. What's the difference? Even if you think that addresses would be in some way ‘the same’, the critical difference is that they have different types. For an array of n elements of type T, then the address of the first element has type ‘pointer to T’; the address of the whole array has type ‘pointer to array of n elements of type T’; clearly very different.

Here's an example of it:

int ar[10];
int *ip;
int (*ar10i)[10];       /* pointer to array of 10 ints */

ip = ar;                /* address of first element */


ip = &ar[0];            /* address of first element */
ar10i = &ar;            /* address of whole array */

For more information you can refer The C Book.

这篇关于指针 - 数组和指针的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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