Search code examples
c++unsigned-char

Some Simple C++ Questions


I don't know much about C++ (almost nothing), meaning I'm a noob at C++.

1.

Let's say you have this code:

typedef unsigned char u8;

With this code, does it mean that when you create a variable you can write u8 instead of unsigned char? Is an unsigned char a one byte value ranging from 0 to 255 or is it something else?

2.

Now I add something:

typdef unsigned char u8;

u8 *someVariable;
someVariable = new u8[12345];

What's the variable someVariable now? Is it a list/array with 12345 items where every entry is of type u8?

3. Adding some more:

typedef unsigned char u8;

u8 *someVariable;
someVariable = new u8[12345];

someVariable+=4;

What happens to someVariable now? Does it add 4 to every index in someVariable or only to a certain one? Or am I totally wrong with the list or array thing?


Solution

    1. Yes you can write u8 stuff; instead of unsigned char stuff; given the typedef. Yes, it might range from 0 to 255. It might be bigger. See here

    2. In this example you have allocated an array (std::list or C++11's std::array is different) or unsigned chars (and don't seem to delete[] them)

    3. Adding a number to a pointer will affect the pointer, not what it points to, so the third example will move the pointer along to the fourth item, but not change any values.