pauliwago
pauliwago

Reputation: 6695

write function: is there a way to write only a part of the buffer?

I'm learning to use the write function and am trying to print only a part of a buffer array of chars. So it looks like this:

char *tempChar;
char *buf;
buf=&tempChar;
read(0, buf, 10);

write(1, [???], 1);

I thought about putting buf[3] where the [???] is, but that didn't work. I also thought about using tempChar[3], but that didn't work either.

Any ideas? Thanks so much.

Upvotes: 0

Views: 846

Answers (1)

Matthew Flaschen
Matthew Flaschen

Reputation: 285047

You would use buf + 3. This is pointer arithmetic. It takes buf and gives you a new pointer 3 characters down. buf[3] is equivalent to *(buf + 3). Note the unwanted dereference.

As another note:

buf=&tempChar;

is probably not right.

That assigns the address of the tempChar variable to buf, which is probably not what you want.

Upvotes: 2

Related Questions