2

Got this code right here to print out the name of an opened file (its a height map in case you were wondering) and every time I try to print out I get format warnings, which format specifier should I use for this?

 unsigned char* data = stbi_load("textures/height.png", &width, &height, &nr_components, 0); printf("Loaded file: %u\n", data); 
8
  • To be clear I'm just asking for the format specifier Commented Jan 30, 2023 at 17:06
  • Do you want the format specifier for unsigned char or unsigned char*? Commented Jan 30, 2023 at 17:08
  • data is defined as a pointer to an unsigned char presumably it holds the address of the first element of an array of unsigned char Commented Jan 30, 2023 at 17:09
  • 3
    stbi_load doesn't return a string. It returns bitmap data. You can't print it like a string. The name of the file is presumably textures/height.png. Commented Jan 30, 2023 at 17:10
  • 1
    Re "not errors but still", No, definitely an error, even if your compiler proceeded. Commented Jan 30, 2023 at 17:12

1 Answer 1

11

If your goal is to print the address where the data was loaded, that would be %p:

printf("Loaded file: %p\n", (void*)data); 

If you want to print the actual data, byte by byte, you should loop over the bytes and use %hhu (for decimal) or %hhx (for hexadecimal):

printf("Loaded file:\n"); for(int i = 0; i < width*height*nr_components; ++i) printf("%hhx ", data[i]); printf("\n"); 

data doesn't contain the name of the file though, so if you want to print just the name, then print that same string that you passed to stbi_load:

const char *filename = "textures/height.png"; unsigned char* data = stbi_load(filename, &width, &height, &nr_components, 0); printf("Loaded file: %s\n", filename); 
Sign up to request clarification or add additional context in comments.

1 Comment

Minor: to better handle large files, use size_t: size_t n = (size_t)width*height*nr_components; for(size_t i = 0; i <n; ++i)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.