0

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);
4
  • 1
    Don't cast malloc Commented Sep 14, 2015 at 2:23
  • You probably wanted buffsize - total_read in the getline call, otherwise you can overflow. Commented Sep 14, 2015 at 2:23
  • You seem to be mentally confusing a pointer with the thing it is pointing to. Don't do this; they are two completely separate entities. Commented Sep 14, 2015 at 2:33
  • "I'm trying to create a big buffer and manually divide it up to use with getline." - that doesn't make any sense. How is that different to just reading the whole file into a buffer? Commented Sep 14, 2015 at 2:37

1 Answer 1

4

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.

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

7 Comments

So because getline takes &buff, which is the address of the pointer to the char array, presumably, there's no clean way to do what I want as described above?
@abcXYZ getline isn't taking &buff. I think you didn't understand my answer.
This page said getline takes the address of the pointer, i.e., &buff: crasseux.com/books/ctutorial/getline.html I must've misunderstood something?
@abcXYZ yes, it takes &buff, not &buff[totalRead]. This is because it may make the pointer point elsewhere (to a new memory allocation). It has no "append" functionality.
@M.M Right. It does take &buff, but is there anyway I can specify where it starts writing the value to in the buffer I pass?
|

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.