it is a pointer to the first element of an array of unsigned char. Is this correct?
uint8_t*
is a pointer to an object of type uint8_t
. If uint8_t
is an alias of unsigned char
, then uint8_t*
is indeed a pointer to unsigned char
.
It can either point to null, or an address that doesn't have an object (a dangling pointer) or it can point to an object. Whether that pointed object is a first element of an array, is impossible for us to say. If the documentation of getData
says that it is, then the best we can do is assume that it doesn't lie. If there is no documentation, then we need to see the implementation to know what it does.
1) How can I print the value data point to in c++ ?
Although printf
is available in C++ as well, it is notoriously difficult to use correctly by beginners (in fact, your suggestions would not have been correct). Preferably, you can insert an object into std::cout
from the <iostream>
header to print it (using the stream insertion operator <<
). Example:
std::cout << data[0];
However, if uint8_t
is indeed an alias of unsigned char
, then the data will be streamed as a character. If you want to stream the integer value, then you need to convert the value to a non-character integer type first:
std::cout << int(data[0]);
To print a data structure such as an array, you can iterate over the array and print each element,
2) Is there a way to iterate (in a loop) over the value data point to?
Yes.
However, in order to iterate an array, you need to somehow know when to stop iterating. This is same as knowing where the array ends. This is also something that the documentation of getData
should reveal. Does the array have a constant length? Is the array terminated by some value? Does it set some global variable that gives us the length of the array? Maybe; we cannot know.
You can use the following algorithm:
1 let the pointer point to the first element of the array
2 if the pointer points to the end of the array, you're done
3 indirect the pointer to get the value at current index
4 you you can now for example print the value
5 increment the pointer
6 jump to 2