c中的数组名称到底是什么? [英] What exactly is the array name in c?

查看:27
本文介绍了c中的数组名称到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am having a tough time understanding the type and use of the name of the array in C. It might seems a long post but please bear with me.

I understand that the following statement declares a to be of type int [] i.e array of integers.

int a[30];

While a also points the first element of array and things like *(a+2) are valid. Thus, making a look like a pointer to an integer. But actually the types int [] and int* are different; while the former is an array type and later is a pointer to an integer.

Also a variable of type int [] gets converted into a variable of type int* when passing it to functions; as in C arrays are passed by reference (with the exception of the sizeof operator).

Here comes the point which makes me dangle. Have a look at the following piece of code:

int main()
{
    int (*p)[3];
    int a[3] = { 5, 4, 6 };

    p = &a;

    printf("a:%d	&a:%d
",a,&a);
    printf("%d",*(*p + 2));
}

OUTPUT:

a:2686720       &a:2686720
6

So, how does the above code work? I have two questions:

  1. a and &a have the same values. Why?
  2. What exactly does int (*p)[3]; do? It declares a pointer to an array, I know this. But how is a pointer to an array different from the pointer to the first element of the array and name of the array?

Can anyone clarify things up? I am having a hell of a lot of confusions.

I know that I should use %p as a placeholder instead of using %d for printing the value of pointer variables. As using the integer placeholder might print truncated addresses. But I just want to keep things simple.

解决方案

  1. a and &a have the same values.How?

They have the same value but different types. Array objects have no padding between elements (before or after) so the address of the array and the address of the first element of the array are the same.

That is:

(void *) a == (void *) &a

  1. What exactly it does int (*p)[3]; Declares a pointer to an array,i know this.But,how a pointer to an array is different from the pointer to the first element of the array and name of the array?

These are two different pointer types. Take for example, pointer arithmetic:

a + 1   /* address of the second element of the array */
&a + 1  /* address one past the last element of the array */

EDIT: due to popular demand I added below some information about conversion of arrays.

With three exceptions, in an expression an object of type array of T is converted to a value of type pointer to T pointing to the first element of the array. The exceptions are if the object is the operand of sizeof or & unary operator or if the object is a string literal initializing an array.

For example this statement:

printf("a:%d	&a:%d
", a, &a);

is actually equivalent to:

printf("a:%d	&a:%d
", &a[0], &a);

Also please note that d conversion specifier can only be use to print a signed integer; to print a pointer value you have to use p specifier (and the argument must be void *). So to do things correctly use:

printf("a:%p	&a:%p
", (void *) a, (void *) &a);

respectively:

printf("a:%p	&a:%p
", (void *) &a[0], (void *) &a);

这篇关于c中的数组名称到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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