0x56794E
0x56794E

Reputation: 21321

Memory of first element in dynamic char array

My array is defined like this

int buffSize = 80;
char* buff = (char*) malloc(sizeof(char) * buffSize);

First, I thought &buff should be the same as &buff[0], but apparently, it isn't! Did I miss something here? This statement prints two different values for those two:

    printf("COMPARE: buff=%u, buff[0]=%u\n", &buff, &buff[0]);

Second, the reason I asked is because I'm trying to create a big buffer and "manually" divide it up to use with getline. Basically, I'd like to do something like this:

int byte_read, total_read = 0;
do
{
   read = getline(&buff[totalRead], buffSize, inFile); //where inFile is just a file handler
  totalRead += read;
}
while (byte_read > 0);

Upvotes: 0

Views: 143

Answers (1)

juanchopanza
juanchopanza

Reputation: 227558

buff is a pointer, and &buff is the address of that pointer. On the other hand, &buff[0] is the address of the location the pointer points to, and should have the same value as buff.

In summary, expect buff and &buff[0] to have the same value.

Upvotes: 4

Related Questions