apologies if this is a basic question but I haven’t been able to figure it out from other posts. I’m trying to read a bunch of images that are labelled "1.jpg" "2.jpg" "3.jpg" etc. I’d like to do so using a for loop. The file reader (I’m using SOIL here, as recommended in the OpenGL nehe tutorial I’m following), requires a const char* as the filename input.
char numbers[6] = "123456";
for (int i=0;i<6;i++)
{
const char* filestring = new char[5];
*filestring = numbers[i] + ".jpg";
texture[i] = SOIL_load_OGL_texture(filestring, ...
... // filestring should be const char* form
}
The errors are:
(1) error: initializer-string for array of chars is too long [-fpermissive]
but i don’t see what is wrong with char numbers[6] = "123456"?
error: assignment of read-only location ‘* filestring’
error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
I’m a bit baffled. Would be grateful for any advice!
Thanks.
Change this:
To this:
The size of the array is determined automatically by the compiler, so you don’t need to specify it explicitly. You are trying to set it too small anyway because it is actually 7 bytes wide because of the trailing null terminator.
It’s not strictly necessary for it to be
const, but it makes it clearer what your intention is if you do mark it as such.Also, this won’t work:
You can’t concatenate C strings that way. Easiest thing to do would be this:
When you need to use it as a C string you can do this: