Advertisement Jump to content
Sign in to follow this  

question: correct way to pass a char array

This topic is 4962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, i have a function take a c string as input: load_image("gar.bmp", /*few other data members*/); load_image now needs to pass that c string onto another function: int IMAGE::load_image(char *filename, /*few other data members*/){ //load the 8-bit image if(!load_bitmap(&bitmap, *filename)){ return(0); } //do some other stuff } this is load_bitmap's header: int load_bitmap(BITMAP_FILE_PTR bitmap, char *filename); now when i pass filename into load_bitmap as "*filename" i get an error. however when i pass it in as "filename" the code doesn't work. however, i know that simplying passing load_bitmap "gar.bmp" does work (i tried it). so how do i pass filename correctly? thanks

Share this post

Link to post
Share on other sites
oh, when i say the code doesn't work, i mean that it compilers, but doesn't do what it is supposed too.

Share this post

Link to post
Share on other sites
Pass it in as 'filename'

One possible reason this might not be working is that you're passing a string literal as a non-constant parameter.

You should change your functions to take const *char rather than just char* for the name, e.g

int load_bitmap(BITMAP_FILE_PTR bitmap, const char *filename);

If your load_bitmap needs to do any string processing with the filename that requires it to be non-const, make a copy of it and do the processing on the copy.

EDIT: actually that probably isn't why it doesn't work, but you should probably do it anyway, in order to ensure that your functions don't accidentally modify the filename string. To know why it doesn't work, I'd probably need to see your load_bitmap function.

Share this post

Link to post
Share on other sites
alright, here is the function

int IMAGE::load_bitmap(BITMAP_FILE_PTR bitmap, char *filename){
// this function opens a bitmap file and loads the data into bitmap

int file_handle, // the file handle
index; // looping index

UCHAR *temp_buffer = NULL; // used to convert 24 bit images to 16 bit
OFSTRUCT file_data; // the file data information

// open the file if it exists
if ((file_handle = OpenFile(filename,&file_data,OF_READ))==-1)

// now load the bitmap file header
_lread(file_handle, &bitmap->bitmapfileheader,sizeof(BITMAPFILEHEADER));

// now load the bitmap file header
_lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));

// now load the color palette if there is one
if (bitmap->bitmapinfoheader.biBitCount == 8){
_lread(file_handle, &bitmap->palette,MAX_COLORS_PALETTE*sizeof(PALETTEENTRY));

// now set all the flags in the palette correctly and fix the reversed
// BGR RGBQUAD data format
for (index=0; index < MAX_COLORS_PALETTE; index++){
// reverse the red and green fields
int temp_color = bitmap->palette[index].peRed;
bitmap->palette[index].peRed = bitmap->palette[index].peBlue;
bitmap->palette[index].peBlue = temp_color;

// always set the flags word to this
bitmap->palette[index].peFlags = PC_NOCOLLAPSE;
} // end for index

} // end if

// finally the image data itself

// now read in the image, if the image is 8 or 16 bit then simply read it
// but if its 24 bit then read it into a temporary area and then convert
// it to a 16 bit image

if(bitmap->bitmapinfoheader.biBitCount==8 || bitmap->bitmapinfoheader.biBitCount==16 ||
// delete the last image if there was one
if (bitmap->buffer)

// allocate the memory for the image
if (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage))){
// close the file

// return error
} // end if

// now read it in
} // end if
// serious problem
} // end else

#if 0
// write the file info out
printf("\nfilename:%s \nsize=%d \nwidth=%d \nheight=%d \nbitsperpixel=%d \ncolors=%d \nimpcolors=%d",

// close the file

// flip the bitmap
// return success

Share this post

Link to post
Share on other sites
Well for a start, _lread is obsolete, and although those functions should still work, it is probably better to use the more up to date functions like ReadFile.

also, _lseek is the wrong function. _lseek is for a low level IO library, whereas the win32 equivalent for use with lread and so forth is called _llseek. that's two 'l's. Confusing isn't it?

Although you should be using SetFilePointer instead.

If that doesn't fix it, try stepping through with the debugger to see where it falls over.

Share this post

Link to post
Share on other sites
I'm sure you've heard it before, but std::string can make your life easier. If you need to pass a C string to a function (for instance I use SDL and some of the loading functions take C strings):

#include string
using namespace std;

string foo;
foo = "filename.txt";

functionThatTakesACString( foo.c_str() );

Share this post

Link to post
Share on other sites
i never thought about using a string object. the load_bitmap code was from a book, and so i was just using that for my game. But a string object is a good idea. thank you very much.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!