 question about returning pointer from a function (syntax)

Hi,

I hav a question about returning a pointer from a function. Are the following two function equivalent (the return statement is different)?

If they are how come they are the same?

uint8_t buff;

{
memset(buff, 0, sizeof(buff));

buff = 'T';
buff = 'C';
buff = temp_c&0xff;
buff = (temp_c>>8)&0xff;
buff = (temp_c>>16)&0xff;
buff = (temp_c>>24)&0xff;

return (&buff);

}
uint8_t buff;

{
memset(buff, 0, sizeof(buff));

buff = 'T';
buff = 'C';
buff = temp_c&0xff;
buff = (temp_c>>8)&0xff;
buff = (temp_c>>16)&0xff;
buff = (temp_c>>24)&0xff;

return (buff);

}

I don't know if they are the same; the returned result is the same.

buff is an array; for arrays, it's the pointer to the first element.

buff is the content of the first element; & takes the address of that element and hence &buff points to the first element.

So in both cases, you return a pointer to the first element.

Yes they are the same thing

buff is already a pointer to the start of the array

Guix,

How is buff already a pointer to the start of the array? I thought it is just an array. Or this just the way c works?

Yes buff is equivalent to a pointer to an array.

int array  = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
...
...
int x = array ;
int y = *(array + 1);
int z = 1[array];

Will result in x, y and z all being assigned the value 2.

Why do you need the function to return a pointer to a global variable? Whatever is calling the function will already have access to the global variable (array or not).