gpt4 book ai didi

c - &array[0] 和 &array 传递给 C 函数时的区别

转载 作者:太空狗 更新时间:2023-10-29 15:22:21 26 4
gpt4 key购买 nike

传递给 C 函数时 &array[0] 和 &array 之间有区别吗?该数组是一个 void* 数组,目前以整数作为数据。

添加了测试代码

#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);
}

最佳答案

是的,有很大的不同,这取决于上下文。

考虑一下:-

char arrayA[10];
char *arrayB;

&arrayA[0]&arrayB[0] 的类型都是 char *

但是 &arrayA 的类型是 char (*)[10]&arrayB 的类型是 char ** -指针的地址。

对于 arrayA,它们指向相同的地址 - 但对于 arrayB它们不是! C 有一个常见的误解,认为“指针和数组是一样的”。这是一个很好的例子,说明他们不是绝对的,

看这个:http://ideone.com/OcbuXZ

关于c - &array[0] 和 &array 传递给 C 函数时的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14955574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com