相似性和通过一个实际的例子数组和指针之间的差 [英] Similarities and differences between arrays and pointers through a practical example

查看:170
本文介绍了相似性和通过一个实际的例子数组和指针之间的差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a[1];
    int * b = malloc(sizeof(int));

    /* 1 */
    scanf("%d", &a);
    printf("%d\n", a[0]);

    /* 2 */ 
    scanf("%d", &b);
    printf("%d\n", b[0]); 

    return 0;
}

当它被编译得到以下警告(的i686-苹果darwin9-GCC-4.0.1):

the following warnings are obtained when it is compiled (i686-apple-darwin9-gcc-4.0.1):

array.c: In function 'main':
array.c:9: warning: format '%d' expects type 'int *', but argument 2 has type 'int (*)[0u]'
array.c:14: warning: format '%d' expects type 'int *', but argument 2 has type 'int **'

但是,为什么会出现在第二的的printf 的执行错误,同时它的工作原理为先的的printf 的?

but, why does an execution-error occur in the second printf, meanwhile it works for the first printf?

更,为什么它获得的输出,如果同一首的 scanf函数的是通过的 scanf函数(%d个,一个)取代;

Even more, why it is obtained the same output if the first scanf is substituted by scanf("%d", a);?

感谢很多提前

推荐答案

在大多数情况下,数组类型的前pression将隐含从为指向TT N个元素的数组转换和它的值将被设定为指向该阵列的第一个元素。此规则的例外是当阵列是&放的操作; 的sizeof 运营商,或者数组是用来初始化声明另一个数组字符串文字之中。

In most contexts, an expression of array type will be implicitly converted from an "N-element array of T" to "pointer to T" and its value will be set to point to the first element of the array. The exceptions to this rule are when the array is an operand of the & or sizeof operators, or if the array is a string literal being used to initialize another array in a declaration.

那么,如何做所有涉及到您code?

So how does all that relate to your code?

在该行

scanf("%d", &a);

您所申请的&安培; 运营商数组。这燮presses从为指向TT数组隐式转换和类型的值返回指向T的指针数组,或 T(*)[N] (所以你的第一个警告)。现在事实证明,一个指针数组和一个指针数组的第一元素的值的值是相同的,它们只是有不同的类型。所以假设 A 在地址0x0001000:

You are applying the & operator to the array. This suppresses the implicit conversion from "array of T" to "pointer to T" and returns a value of type "pointer to array of T", or T (*)[N] (hence your first warning). Now it turns out that the value of a pointer to an array and the value of a pointer to the first element of the array are the same, they just have different types. So assuming that a is at address 0x0001000:

expression      type          value         note
----------      ----          -----         ----
         a      int *         0x0001000     implicitly converted to pointer
        &a      int (*)[N]    0x0001000     
     &a[0]      int *         0x0001000

这就是为什么你的第一次调用 scanf()的工程;你传递正确的指针的的,但是编译器是抱怨,因为键入的前任pression的不匹配函数需要什么。假如你写

That's why your first call to scanf() "works"; you're passing the right pointer value, but the compiler is complaining because the type of the expression doesn't match what the function expects. Had you written

scanf("%d", a);

您不会收到任何警告,因为的类型将被视为为int * ,这就是 scanf()的的期望。注意,这是相同的调用

you would not have received any warnings, since the type of a will be taken to be int *, which is what scanf() expects. Note that this is identical to calling

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

至于 B ...

您明确声明 B 为指针为int和内存块分配给它。当您将&安培; 运营商给它,你回来什么的地址的变量 B 类型 INT ** (因此第二个警告),而不是地址 b 点。

You explicitly declare b as a pointer to int and assign a block of memory to it. When you apply the & operator to it, what you get back is the address of the variable b with type int ** (hence the second warning), not the address that b points to.

expression      type          value         note
----------      ----          -----         ----
         b      int *         0x0040000     value contained in b
        &b      int **        0x0001000     address of b

有关这种情况下,您只需通过未修饰的 B

For that case, you just pass the undecorated b:

scanf("%d", b);

这篇关于相似性和通过一个实际的例子数组和指针之间的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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