究竟是什么在C数组名? [英] What exactly is the array name in c?

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

问题描述

我有一个艰难的时间理解C.数组名称的类型和使用它可能似乎还有很长的文章,但请多多包涵。

据我了解,下面的语句声明 A 为类型 INT [] 阵整数

  int类型的[30];

A 也指出阵列和东西像的第一个元素*(A + 2)是有效的。因此,使得 A 看起来像一个指向一个整数。但实际上类型 INT [] 为int * 是不同的;而前者是一个数组类型,后来是一个指向一个整数

另外类型的变量 INT [] 被转换成类型的变量为int *传递给时功能;在 C 数组是通过引用传递(与的sizeof 经营者除外)。

下面谈到这点让我吊着的地步。看看下面的一段code的:

  INT的main()
{
    INT(* P)[3];
    诠释一个[3] = {5,4,6};    P =&放大器;一个;    的printf(一个数:%d \\ T&放大器;一个数:%d \\ n,一,&安培; A);
    的printf(%d个*(* P + 2));
}

OUTPUT:

  A:2686720&放大器;一个:2686720
6

那么,如何在上述code的工作?我有两个问题:


  1. A &放大器;一个有相同的价值观。为什么呢?

  2. 到底是什么 INT(* P)[3]; 吗?它声明的指针数组,这个我知道。但如何是一个指针数组不同的指向数组的第一个元素数组的名称

任何人都可以清楚的事情了?我有很多混乱的地狱。

我知道,我应该使用%P 作为一个占位符,而不是使用%d个打印的价值指针变量。作为使用整数占位符可能会打印截断地址。但我只想让事情变得简单。


解决方案

  

      
  1. a和安培&;一个具有相同的values​​.How

  2.   

它们具有相同的值,但不同的类型。数组对象有和元素(之前或之后)之间没有填充所以数组的地址数组的第一个元素的地址都是一样的。

这就是:

 (无效*)A ==(无效*)及一


  <醇开始=2>
  
  • 究竟它INT(* P)[3];声明一个指针数组,我知道this.But,一个指向数组的指针是如何从指针数组的数组和名称?
  • 的第一个元素不同
      

    这是两个不同的指针类型。举个例子,指针运算:

     数组的第二个元素的+ 1 / *地址* /
    &放大器;一个+ 1 / *在地址一过去阵列的最后一个元素* /

    编辑:由于我在下面关于阵列转换一些信息,大众的需求。

    有三个例外,在一个前pression T 转换为指针类型的值 T类型数组的对象指向数组的第一个元素。唯一的例外是,如果对象是的sizeof的操作数&安培; 单目运算符,或者如果对象是一个字符串文字初始化一个数组。

    例如以下语句:

     的printf(一个数:%d \\ T&放大器;一个数:%d \\ n,一,&安培; A);

    其实就相当于:

     的printf(一个数:%d \\ T&放大器;一个数:%d \\ n,&安培; A [0],&安培; A);

    另外,请注意, D 转换符只能用打印符号整数;打印你必须使用一个指针值 P 符(和参数必须是无效* )。所以,我们要做的事情正确使用:

     的printf(A:%P \\ T&放大器;一个:%P \\ N(无效*)A,(无效*)及一);

    分别为:

     的printf(A:%P \\ T&放大器;一个:%P \\ N(无效*)及一个[0],(无效*)及一);

    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\t&a:%d\n",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\t&a:%d\n", a, &a);
    

    is actually equivalent to:

    printf("a:%d\t&a:%d\n", &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\t&a:%p\n", (void *) a, (void *) &a);
    

    respectively:

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

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

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