&之间的差异放大器;数组[0],&安培;阵列时传递给C函数 [英] difference between &array[0] and &array when passed to a C function
问题描述
时之间有功放和一个差;数组[0],&安培;数组传递到C函数时。这个数组是一个void *阵列目前采用整数数据。
加试code
的#include<&iostream的GT;
#包括LT&;&CONIO.H GT;使用命名空间std;INT的read_buffer [10] = {0,0,0,0,0,0,0,0,0,0};INT write_buffer [10] = {0,1,2,3,4,5,6,7,8,9};无效WriteBlock(无效* SrcPtr)
{
// WriteBlock将使用SrcPtr并将该数据存储到一个公共存储器块,其读出数据块将访问。
}无效读出数据块(无效* DstPtr)
{
//读出数据块功能将获取从readBuffer数据,并把数据回* DstPtr。
}无效的主要()
{
WriteBlock(为(int *)及write_buffer);
//是否有这两个下面的调用之间的差。
读出数据块(安培;的read_buffer [0]);
读出数据块(安培;的read_buffer);
}
是的,有一个很大的区别,它依赖于上下文。
考虑一下: -
字符arrayA [10];
字符* arrayB;
&放大器; arrayA [0]
和&放大器; arrayB [0]
都有键入的char *
。
但&放大器; arrayA
的类型字符(*)[10]
,而&放; arrayB
的类型的char **
- 指针的地址
有关 arrayA
,这些都指向同一个地址 - 但 arrayB
,他们不这样做! 有一个通用的C误解,认为指针和数组是一样的。这是他们在哪里absoluelty不是一个很好的例子,
看到这个: http://ideone.com/OcbuXZ
Is there a difference between &array[0] and &array when passed to a C Function. This array is a void* array which currently takes integer as data.
Added the test code
#include <iostream>
#include <conio.h>
using namespace std;
int read_buffer[10] = {0,0,0,0,0,0,0,0,0,0};
int write_buffer[10] = {0,1,2,3,4,5,6,7,8,9};
void WriteBlock(void* SrcPtr)
{
//WriteBlock will use SrcPtr and store the data to a common memory block which ReadBlock will access.
}
void ReadBlock(void* DstPtr)
{
//ReadBlock function will fetch data from readBuffer and put the data back into the *DstPtr.
}
void main()
{
WriteBlock((int*)&write_buffer);
//Is there a difference between these two below calls.
ReadBlock(&read_buffer[0]);
ReadBlock(&read_buffer);
}
Yes, there's a big difference, and it depends on context.
Consider this:-
char arrayA[10];
char *arrayB;
&arrayA[0]
and &arrayB[0]
both have type char *
.
But &arrayA
has type char (*)[10]
while &arrayB
has type char **
- the address of the pointer.
For arrayA
, these point to the same address - but for arrayB
, they do not! There's a common C misconception that "pointers and arrays are the same". This is a great example of where they are absoluelty not,
See this : http://ideone.com/OcbuXZ
这篇关于&之间的差异放大器;数组[0],&安培;阵列时传递给C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!