Array Pointers

Hi. Just curious. When passing array pointers around functions is there a difference between the following lines of code? ...

// Function to which the array pointer is passed...
1
2
3
int myFunction(char *buffer){
// arbitrary code
}



// first way of passing an array pointer to the function...
1
2
3
4
int main(){
char myBuffer[1024];
int result = myFunction(myBuffer);
}


// second way of passing an array pointer to the function...
1
2
3
4
int main(){
char myBuffer[1024];
int result = myFunction(&myBuffer[0]);
}


I orginally believed that there was no difference, however in a recent project changing some code from method 1 to method 2 seemed to fix some problems I was having, but am unsure whether or not it was a coincidence or not :)

Why not simply test it for yourself?
1
2
3
4
int myFunction(char *buffer){
  std::cout << "address of the buffer is " << static_cast<void*>(buffer)<<"\n";
// arbitrary code
}


In my opinion they should produce the same result.

But the second option is also valid if you use an std::vector instead of an array whereas the first one will fail to compile.

But maybe it's best to change the signature of the function to an iterator pair or something similiar?
In that case there is no difference. I don't see how a change could have fixed anything in the program. If you use the debugger you'll see that the same address is passed in either way.
1
2
int *ptr = NULL;
&ptr[0]; // Is it safe? 
Topic archived. No new replies allowed.