多阵列和多阵列[0],&安培;多阵列[0]一样的吗? [英] multiArray and multiArray[0] and &multiArray[0] same?

查看:85
本文介绍了多阵列和多阵列[0],&安培;多阵列[0]一样的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在6号线,而不是多阵列[0],当我写的多阵列,计划仍然有效。不明白为什么。我想在此之前,多阵列是一个指向多阵列[0]这是一个指向多阵列[0] [0]。所以,多阵列孤单是一个指针的指针。多阵列[0]是一个指向一个4元素的int数组。如此看来,多阵列和多阵列[0]必须是不同的。但在跌破code,两者的工作。我写了打印功能需要一个指向一个4元素的int数组。因此,只有多阵列[0]必须工作,多阵列不能工作。但两者的工作原理。不明白这一点。

 的#include<&stdio.h中GT;空隙printArr(中间体(* ptr的)[4]);INT I,K;诠释主要(无效){INT多阵列[3] [4] = {{1,5,2,4},{0,6,3,14},{132,4,22,5}};INT(*点)[4] =多阵列[0];为(K = 0; k 3;; k ++)
{
    printArr(点++);}的getchar();}空隙printArr(中间体(* ptr的)[4]){为int * TEMP =(INT *)PTR;对于(I = 0; I&下; 4;我+ +)
{
    的printf(%d个,*温度);
    临时++;
}看跌期权(\\ n);
}


解决方案

别人写的多维数组是语法糖1-D阵列。

这是有点像说 INT 是一个只是语法糖unsigned char型[4] 。你可以废除前pressions像 4 + 5 和操纵的4个字节数组得到同样的结果。

您甚至可以说,C是只是一个通用图灵机脚本语法糖,如果你想远一点将这个概念。

现实情况是,多维数组是在C型系统的一部分,并且它们具有与它们相关的语法。还有对皮肤有猫的方法不止一种。

在移动,C的方式安排我们正在调用一个多维数组地说:阵列只能有一个维度,但元素类型本身可以是另一个数组。我们说多维阵列作为方便起见,但语法和类型系统实际上反映该阵列的一维性质

因此​​, INT多阵列[3] [4] 是3个元素的数组。各个元素是4 整数

数组

在内存中,数组的元素是连续存储 - 无论元素类型是什么。因此,内存布局是4 INT ,数组紧接着的4 INT ,最后又另一个数组数组4 INT

有12个连续 INT 在存储器中,并在C型系统它们被分组成3组,每组4

您会注意到,第一个12 INT 的第一 INT 第一组4.这就是为什么我们发现,如果我们问什么是int的内存位置?,什么是第一组的4个整数的存储位置?和什么是12整数整个集团的内存位置?,我们得到每一次相同的答案。 (在C中,一个多字节对象的存储器位置被认为是在其第一字节的位置开始)。

现在,说说指针语法和再presentation。在C语言中,指针会告诉你在内存中的对象都可以找到。有两个方面是:对象的内存位置,什么类型的对象,它是。 (该对象的大小的类型的必然结果)。

有些presentations只专注于那些第一次的,他们会说这样的话一个指针只是一个数字。但是,这是忘掉类型的信息,这是一个指针的一个重要组成部分。

当您打印%P 指针,你失去的类型信息。你只是在第一个字节的内存扑灭位置。因此,他们看起来都一样,尽管这三个指针以不同大小的物体(相互重叠像matruskha娃娃)指出这一事实。

在C的大多数实现中,类型信息都计算在编译时,因此,如果您尝试通过汇编code(有些人这样做)比较根源$ C ​​$ C,了解C,你只看到指针的内存位置的部分。这会导致如果你忘了类型的信息也是至关重要的误解。

脚注:的这一切都是独立的一对夫妇的C有语法怪癖的;这造成了很多混乱,多年来(但也有用有时)。这位前pression X 和放大器的快捷方式; X [0] 如果 X 是一个数组,如&放的操作使用时除外; 的sizeof 。 (否则这将是一个递归定义!)。第二个怪癖是,如果你写的东西看起来像一个函数形参列表数组声明,它实际上是因为如果你写了一个指针声明符。我再次强调,这些都只是语法古怪,他们什么也不说关于数组和指针的性质,这实际上并不复杂的根本。语言将工作一样良好,没有这两个怪癖。

On 6th line instead of multiArray[0], when I write multiArray, program still works. Don't understand why. I was thinking before that multiArray is a pointer to multiArray[0] which is a pointer to multiArray[0][0]. So multiArray alone is a pointer to a pointer. multiArray[0] is a pointer to a 4 element int array. So it seems that multiArray and multiArray[0] must be different. But in below code, both work. Print function I wrote expects a pointer to a 4 element int array. So only multiArray[0] must work and multiArray must not work. But both works. Didn't understand that.

#include <stdio.h>

void printArr(int(*ptr)[4]);

int i, k;

int main(void){

int multiArray[3][4] = { { 1, 5, 2, 4 }, { 0, 6, 3, 14 }, { 132, 4, 22, 5 } };

int(*point)[4] = multiArray[0];

for (k = 0; k < 3; k++)
{  
    printArr(point++);

}

getchar();

}

void printArr(int(*ptr)[4]){

int *temp = (int *)ptr;

for (i = 0; i < 4; i++)
{
    printf("%d ", *temp);
    temp++;
}

puts("\n");
}

解决方案

Someone else wrote "Multi-dimensional arrays are syntactic sugar for 1-D arrays".

This is sort of like saying that int is just syntactic sugar for a unsigned char[4] . You could do away with expressions like 4 + 5 and get the same result by manipulating arrays of 4 bytes.

You could even say that C is just syntactic sugar for a Universal Turing Machine script, if you want to take this concept a bit further.

The reality is that multi-dimensional arrays are a part of the type system in C, and they have syntax associated with them. There's more than one way to skin a cat.

Moving on, the way C arranges what we are calling a multi-dimension array is to say: "Arrays can only have one dimension, but the element type may itself be another array". We say "multi-dimension array" as a matter of convenience, but the syntax and the type system actually reflect the one-dimensional nature of the array.

So, int multiArray[3][4] is an array of 3 elements. Each of those elements is an array of 4 ints.

In memory, an array's elements are stored contiguously -- regardless of what the element type is. So, the memory layout is an array of 4 int, immediately followed by another array of 4 int, and finally another array of 4 int.

There are 12 contiguous int in memory, and in the C type system they are grouped up into 3 groups of 4.

You will note that the first int of the 12 is also the first int of the first group of 4. This is why we find that if we ask "What is the memory location of the first int?", "What is the memory location of the first group of 4 ints?", and "What is the memory location of the entire bloc of 12 ints?", we get the same answer every time. (In C, the memory location of a multi-byte object is considered to start at the location of its first byte).

Now, to talk about the pointer syntax and representation. In C, a pointer tells you where in memory an object can be found. There are two aspects to this: the memory location of the object, and what type of object it is. (The size of the object is a corollary of the type).

Some presentations only focus on the first of those, they will say things like "A pointer is just a number". But that is forgetting about the type information, which is a crucial part of a pointer.

When you print the pointer with %p, you lose the type information. You're just putting out the location in memory of the first byte. So they all look the same, despite the fact that the three pointers are pointing at differently-sized objects (which overlap each other like matruskha dolls).

In most implementations of C, the type information is all computed at compile-time, so if you try to understand C by comparing source code with assembly code (some people do this), you only see the memory-location part of the pointer. This can lead to misunderstanding if you forget that the type information is also crucial.

Footnote: All of this is independent of a couple of syntax quirks that C has; which have caused a lot of confusion over the years (but are also useful sometimes). The expression x is a shortcut for &x[0] if x is an array, except when used as the operand of & or sizeof. (Otherwise this would be a recursive definition!). The second quirk is that if you write what looks like an array declarator in a function formal parameter list, it is actually as if you wrote a pointer declarator. I stress again that these are just syntax oddities, they are not saying anything fundamental about the nature of arrays and pointers, which is actually not that complicated. The language would work just as well without both of these quirks.

这篇关于多阵列和多阵列[0],&安培;多阵列[0]一样的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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