Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting first byte in a char* buffer

Tags:

c++

c

I have a char* buffer and I am interested in looking at the first byte in the char* buffer, what is the most optimal way to go about this.

EDIT: Based on the negative votes I might want to explain why this question, I am aware of methods but in the code base that I have been looking for getting first byte people do all kinds of crazy things like do a copy of the buffer , copy it to a stream and then do a get.

like image 601
kal Avatar asked Feb 11 '09 20:02

kal


People also ask

Is sizeof char always 1?

Even if you think of a “character” as a multi-byte thingy, char is not. sizeof(char) is always exactly 1. No exceptions, ever.

What is char * buf?

The char buf[] is a placeholder for a string. Since the max length of the string is not known at compiletime, the struct reserves the name for it, so it can be properly adressed.

How do I determine the size of a char buffer?

first, the char variable is defined in charType and the char array in arr. Then, the size of the char variable is calculated using sizeof() operator. Then the size of the char array is find by dividing the size of the complete array by the size of the first variable.

What is a char buffer in C?

Char buffers can be created either by allocation , which allocates space for the buffer's content, by wrapping an existing char array or string into a buffer, or by creating a view of an existing byte buffer. Like a byte buffer, a char buffer is either direct or non-direct.


1 Answers

Just use

char firstByte = buffer[0];
like image 199
Johannes Weiss Avatar answered Oct 03 '22 17:10

Johannes Weiss