1

Let say I have some data (for example numbers representing pixels of a grey image) that are read from a file and packed into a pointer to uint8_t.

uint8_t* data = getData(readFile(filePath));

If I truly understood what a uint8_t pointer is, it is like an array of unsigned char (uint8_t map to unsigned char), in other word, it is a pointer to the first element of an array of unsigned char. Is this correct?

My Questions are:

1) How can I print the value data point to in c++ ?
I read that in C it can be done like this printf(*data) or printf( data[0] ). See Print out the value uint8_t *
But how to do that in c++ ?

2) Is there a way to iterate (in a loop) over the value data point to? something like:

for(auto i=0; i< data.size; i++) {
   auto d = data[i]; 
}
Gaetan
  • 577
  • 2
  • 7
  • 20
  • Is the data that `data` is pointing to a null-terminated byte string? And the way you apparently think it can be printed in C is wrong as well. – Some programmer dude Aug 28 '18 at 12:52
  • Also, a pointer is a pointer to some location and that is all. There no notion of size or length of the data associated with a pointer. If you have a pointer to the first element, and need to loop over a consecutive set of elements, then you need to get the size explicitly from somewhere. – Some programmer dude Aug 28 '18 at 12:54
  • In the read file function you should also keep track of howmany bytes you have read from the file. Say you store the byte cound int the cnt variable you could write a loop like `for (size_t i = 0; i < cnt; i++) data[i];` – hetepeperfan Aug 28 '18 at 12:58
  • @Someprogrammerdude regarding how it is done in C I didn't tried it but it should be like that according to https://stackoverflow.com/questions/31937276/print-out-the-value-uint8-t – Gaetan Aug 28 '18 at 13:08
  • @hetepeperfan Thanks. Any idea about printing the value? An equivalence to the `C` way of doing that? – Gaetan Aug 28 '18 at 13:10
  • 2
    `printf(data[0])` is *very* different from `printf("%d", data[0])` – Some programmer dude Aug 28 '18 at 13:10
  • @Gaetan You have to come already know the number of elements the array pointed to by `data` has. You can't determine that using just `data`. Whatever is providing `getData` should also provide a mechanism for finding out how much data it returned. – François Andrieux Aug 28 '18 at 13:13

2 Answers2

1

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
eerorika
  • 232,697
  • 12
  • 197
  • 326
0

To print in c++ instead printf("%d", data[0] ) use std::cout << data[0];, and instead printf("%d", *data ) use std::cout << *data;

Pay attention, that std::cout << make formatting according to data type, and if you want to change something you need more : e.g. #include <iomanip>.

Also, when you use pointers (does not matter in C or C++) you should not access data until pointer is valid. At least check data != NULL and then use *data in expressions.

And the most important thing - if data is pointer like uint8_t* data; it has no property (field) data.size, so loop should be organized in other way. I mean, you have to find other way to know how much data is available by that pointer... e.g.:

 size_t size = ... // calculate the length of array before loop
 for (size_t cnt = 0; cnt < size; cnt++) {
      std::cout << data[cnt];
 }

or in case when end of array defined by a special value it can be:

 uint8_t* ptr = data;
 while ( *ptr != END_MARKER ) {
      std::cout << *ptr;
      ptr++;
 }

UPDATE:

If it is not uint8_t* but somethin like queue structures

typedef struct{
   uint8_t    data;
   QueueItem* next;
} QueueItem;

NULL pointer itself can be END_MARKER, e.g.:

 QueueItem* ptr = queue;
 while ( ptr != NULL ) {
      std::cout << ptr->data;
      ptr = ptr->next;
 }
VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • While technically true, what is really printed may not be what the OP want to be printed. It really depends on the data and what it represents. – Some programmer dude Aug 28 '18 at 12:53
  • @Someprogrammerdude exactly. The data are numbers. like pixels value of a grey image – Gaetan Aug 28 '18 at 12:56
  • @Gaetan Then you can't print it like characters (which is what printing a single `uint8_t` value will do). You need to cast it to an integer. – Some programmer dude Aug 28 '18 at 12:57
  • @Someprogrammerdude No matter in what type it is printed, I just want to see all the value. But when I do cout << data[0] << endl; it just show me the first value. – Gaetan Aug 28 '18 at 13:00
  • @Gaetan You may be confused with how `cout` works with `char*` pointers. It exceptionally treats it as a pointer to an array of characters and prints them all, until a null terminator is found. This doesn't happen with other pointer types. Fundamentally, the problem is there is no way to know how many elements there are in an array from a pointer to it's first element. It only works for `char*` because a null terminator indicates the end. – François Andrieux Aug 28 '18 at 13:11