4

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);
 }
13
  • both expressions effectively give you the same address Commented Feb 19, 2013 at 10:51
  • Which means the elements of your array are of type void *. Is that really what you have? How is array declared? Commented Feb 19, 2013 at 11:06
  • 2
    &array[0]==>void** AND &array==>void*** Commented Feb 19, 2013 at 11:33
  • @OneManCrew &array is of type void* (*)[how many elements in the array], IOW, pointer to array of pointer to void. Those upvoting OMC's comment, think. Commented Feb 19, 2013 at 11:40
  • @AlexeyFrunze - Don't be too harsh. The OP hasn't shown a line of code yet. You're making one assumption, OMC is making another. (And I'm hedging my bets...) Commented Feb 19, 2013 at 11:43

5 Answers 5

6

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

Sign up to request clarification or add additional context in comments.

6 Comments

Actually, &arrayA has type char (*) [10].
(&array[0]) + 1 != (&array) + 1 same or not ?? plz tell me... @roddy
@dev - I cant say unless you show me the code where array is defined. But in general, no.
arrayB is not an array, it's a pointer and so your example significantly differs from what's being talked about in the question.
@AlexeyFrunze : Pointer/array confusion is a common beginner problem, because they're so deeply inter-related. (e.g. void foo(char *a, char b[]). The OP hasn't shown a line of code yet, so - as I said - the answer depends on the context.
|
2

Assuming array is declared

void *array[N];

then the expressions &array[0] and &array will yield the same value (the address of the first element of the array is the same as the address of the array itself), but will have different types.

Expression        Type
----------        ----
    &array        void *(*)[10]  -- pointer to 10-element array of `void *`
  &array[0]       void **        -- pointer to pointer to void

Your function prototype will need to match up with whichever expression you pass. If you call the function as

func(&array);

then the function prototype needs to be

void func(void *(*arrp)[10]) {...}

If you call the function as

func(&array[0]);

then the function prototype needs to be

void func(void **arrp) {...}

although in that case you should pass the size of the array as a separate parameter.

Now, assuming array is declared

void **array = malloc(sizeof *array * N);

then the expressions &array and &array[0] will yield different values and different types.

Expression        Type
----------        ----
    &array        void ***  
 &array[0]        void **   

&array will give you the address of the array variable itself, which is different from the address of the heap memory that's been allocated for the array. Again, your function prototype will need to match up with the type of the expression you use.

1 Comment

&array[0] -- void ** pointer to pointer to void. It is not a pointer to pointer. It is just an pointer to int. Am i wrong
0

If array is really an array, then

  • &array[0] is the pointer to element 0 of array[]
  • &array is the pointer to the entire array[]

So, these two expressions are of different types. And that's the main difference that may cause your code to fail to compile if you pass the wrong one of the two.

At the low level, however, the two pointers are going to hold the same address.

2 Comments

pointer to the entire array points to the first(oth element) of the array only
@HussainAkhtarWahid Have you not read the last sentence or are you suggesting to reword it?
0

Yes there is a big different

&array[0]==>void** 

AND

&array==>void***

2 Comments

Your're assuming array is a pointer, not an array. What if he means void *array[10]; ?
That means that you can look at the array as a void** pointer that point to the first void* pointer in the array!
0

This won't compile, you are using a void * and try to get the first element of it. But what size does it have? The compiler doesn't know. Using int * may compile, if you are not trying something like this:

int main (void) {
  int *arr = malloc( 10 );

  arr = &arr[0]; // this is ok
  arr = &arr;    // wrong data type
}

&array returns an int **, &array[0] returns int *. These are different data types.

1 Comment

The question is tagged C, while your code is C++ (new). How is this supposed to answer the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.