Invalid types 'unsigned char[int]' for array subscript

How can I print the unsigned char array?

 unsigned char byteBuf = ( unsigned char* )malloc((MAX_buff)*sizeof(unsigned char)); 
   //byteBuf  have some values. 

    Serial.print("IN for byteBuf  \n");
    for(int i=0;i<33 ;i++){  Serial.print(byteBuf[i]);}

Where did you get this code? Chat GPT?
What is it supposed to do?
You did not fill your buffer. Why would yiu like to print it?
Sorry, but to me it makes no sense...

?!! I asked on specific thing " How to print the unsigned char array" .
This is just a part of a long code

You are trying to print an array but byteBuf is not declared as an array

You are defining the variable as a unsigned char but assign a pointer to it?

This would make more sense

unsigned char * byteBuf = ( unsigned char* ) …
2 Likes

it is not an " unsigned char array"

Hi Marybell,

Sometimes it is very difficult to see whether the code is written by chatGPT (=nonsense) or by someone that really knows what he/she is doing, specially if it is only a snippet.
The use of malloc is rather rare on this forum. And not really newbee stuff. So before trying to understand your code (I am not really familiar with malloc) I wanted to rule out the option that the code was complete nonsense...(and I would be wasting my time).
Anyway, your problem is solved so that is great!

Groet, Koen.

fair enough @build_1971 - typically the code could have been written as

uint8_t byteBuf [MAX_buff]; // define the buffer

•••

Serial.print("IN for byteBuf  \n");
for(uint8_t  i=0; i< MAX_buff ; i++) {
  Serial.println(byteBuf[i]);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.