数组衰变成指针 [英] Arrays decaying into pointers

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

问题描述

请帮助我了解下面的节目。

 #包括LT&;&stdio.h中GT;
诠释的main()
{
    诠释一个[7];
    一个[0] = 1976;
    一个[1] = 1984;
    的printf(一个内存位置:%P,一);
    的printf(内存位置%,p值%D,A,* A);
    的printf(内存位置%,P值为%d,&安培; A [1],A [1]);
    返回0;
}

&放大器;一个[1] &安培A + 1 。他们是相同或不同的?

 的#include<&stdio.h中GT;
诠释的main()
{
    INT v [10];
    INT ** P;
    为int *一个[5];
    v [0] = 1234;
    v [1] = 5678;
    一个[0] = v的
    一个[1] = V + 1;
    的printf(%d个\\ t%d个\\ t%d个\\ t%d个\\ N,* A [0],*一[1]中,[0] [0],**一);
    的printf(%d个\\ N的sizeof(V));
    返回0;
}

我想知道如何 *一个[5] 重新$ P $内存psented。为 *一个基类指针指向 A [0],A [1],A [2]中,[3],A [ 4]

 #包括LT&;&stdio.h中GT;诠释的main()
{
    INT v [10];
    INT ** P;
    INT(*一)[10];
    A =&放大器;伏;
    的printf(%d个\\ N,*一);
        返回0;
}

A = V; //提示错误,为什么?确实 v 这里衰变为 * V 。然后执行&放大器; v 获取衰变成(*)[]垂直? &安培;意味着常量指针。在这里,怎么可能设置一个const指向一个非const指针,未作类型转换?

在什么地方得到的数组存储在内存中。它得到存储到存储器的数据段。

 #包括LT&;&stdio.h中GT;诠释的main()
{
    INT CARRAY [5] = {1,2,3,4,5};
    的printf(%d个\\ N,CARRAY [0]);
    的printf(%d个\\ t%d个\\ t%d个\\ N的sizeof(CARRAY)的sizeof(安培; CARRAY)的sizeof(安培; CARRAY [0]));
    返回0;
}

编辑:

我已经经历了其中一些规定的物品不见了,凡数组名称不能decyed到指针只有两种可能的情况是的sizeof &安培; 。但是,在上述程序的sizeof(安培; CARRAY)给出大小为4和&放大器; CARRAY 衰变为(*)[] CARRAY 作为它的右值。

然后就是数组名不能得到腐朽为指针两个条件语句的sizeof &安培; 成为这里假


解决方案

  

&放大器;一个[1] &安培A + 1 。他们是相同或不同的?


不同。 &放大器;一个[1] 相同(A + 1)。一般情况下, X [Y] 是定义等同于 *(X + Y)


  

我想知道如何 *一个[5] 重新$ P $内存psented。请问 *一个是一个基
  指针指向 A [0],A [1],A [2],A [3],A [4]


在你的第二个例子, A 是指针数组。 * A [I] 是对象的值,其地址被存储为I 元素的数组中为止。 *一个在这种情况下是一样的 A [0] ,这是你的数组中的第一个元素(是一个指针)。


  

A = V //为什么这给误差


由于 A (在你的最后一个例子)是一个指向数组的指针。要分配到 A ,那么你需要指定数组 v (或任何其他数组的地址正确的尺寸);

  A =安培;伏;

这是非常好的,你已经COMMITED理解的事情,但没有什么可以帮助你比的一个好的C书

希望这有助于。

Please help me understand the programs below.

#include<stdio.h>
int main()
{
    int a[7];
    a[0] = 1976;
    a[1] = 1984;
    printf("memory location of a: %p", a); 
    printf("value at memory location %p is %d", a, *a); 
    printf("value at memory location %p is %d", &a[1], a[1]);
    return 0;
}

&a[1] and &a+1. Are they same or different?

#include <stdio.h> 
int main() 
{
    int v[10];
    int **p; 
    int *a[5];
    v[0] = 1234;
    v[1] = 5678;
    a[0] = v; 
    a[1] = v+1;
    printf("%d\t%d\t%d\t%d\n", *a[0],*a[1],a[0][0],**a);
    printf("%d\n", sizeof(v));
    return 0;
} 

I wanted to know how *a[5] is represented in memory. Is *a a base pointer that points to a[0],a[1],a[2],a[3],a[4]?

#include<stdio.h>

int main()
{
    int v[10];
    int **p;
    int (*a)[10];
    a=&v;
    printf("%d\n",*a);
        return 0;
}

a=v; // gives error why? does v here decay into *v. Then does &v get decayed into (*)[]v? & means const pointer. Here, how is it possible to set a const pointer to a non-const pointer without a typecast?

Where does the array get stored in the memory. Does it get stored onto the data segment of the memory.

#include<stdio.h>

int main()
{
    int carray[5]={1,2,3,4,5};
    printf("%d\n",carray[0]);
    printf("%d\t%d\t%d\n",sizeof(carray),sizeof(&carray),sizeof(&carray[0]));
    return 0;
}

EDITED:

I have gone through some of the articles which stated that the only two possible situations where an array name cannot be decyed into pointer is the sizeof and &. But in the above program sizeof(&carray) gives the size as 4. and &carray decays into (*)[]carray as its an rvalue.

Then the statement that array name cannot get decayed into pointers on two conditions sizeof and & becomes false here.

解决方案

&a[1] and &a+1. Are they same or different?

Different. &a[1] is the same as (a+1). In general, x[y] is by definition equivalent to *(x+y).

I wanted to know how *a[5] is represented in memory. Does *a is a base pointer that points to a[0],a[1],a[2],a[3],a[4].

In your second example, a is an array of pointers. *a[i] is the value of the object, the address of which is stored as the ith element in your array. *a in this case is the same as a[0], which is the first element in your array (which is a pointer).

a=v //why this gives error

Because a (in your last example) is a pointer to an array. You want to assign to a, then you need to assign the address of the array v (or any other array with correct dimensions);

a = &v;

This is very good that you've commited to understanding things, but nothing will help you better than a good C book.

Hope this helps.

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

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