Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between &array[0] and &array when passed to a C function

Tags:

c

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);
 }
like image 462
Vijay Avatar asked Feb 18 '23 11:02

Vijay


1 Answers

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

like image 80
Roddy Avatar answered Feb 20 '23 00:02

Roddy