question: correct way to pass a char array

Started by
6 comments, last by CJWR 18 years, 10 months ago
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
Charles Reed, CEO of CJWR Software LLC
Advertisement
oh, when i say the code doesn't work, i mean that it compilers, but doesn't do what it is supposed too.
Charles Reed, CEO of CJWR Software LLC
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.
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)		return(0);	// 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	_lseek(file_handle,-(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);	// 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 || 	   bitmap->bitmapinfoheader.biBitCount==24){		// delete the last image if there was one		if (bitmap->buffer)			free(bitmap->buffer);		// allocate the memory for the image		if (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage))){			// close the file			_lclose(file_handle);			// return error			return(0);	    } // end if		// now read it in		_lread(file_handle,bitmap->buffer,bitmap->bitmapinfoheader.biSizeImage);	} // end if	else{		// serious problem		return(0);	} // end else	#if 0	// write the file info out 	printf("\nfilename:%s \nsize=%d \nwidth=%d \nheight=%d \nbitsperpixel=%d \ncolors=%d \nimpcolors=%d",		   filename,		   bitmap->bitmapinfoheader.biSizeImage,           bitmap->bitmapinfoheader.biWidth,           bitmap->bitmapinfoheader.biHeight,		   bitmap->bitmapinfoheader.biBitCount,           bitmap->bitmapinfoheader.biClrUsed,           bitmap->bitmapinfoheader.biClrImportant);	#endif	// close the file	_lclose(file_handle);	// flip the bitmap	flip_bitmap(bitmap->buffer, 		        bitmap->bitmapinfoheader.biWidth*(bitmap->bitmapinfoheader.biBitCount/8), 			    bitmap->bitmapinfoheader.biHeight);	// return success	return(1);} 
Charles Reed, CEO of CJWR Software LLC
also, yes, changing everything to const had no effect.
Charles Reed, CEO of CJWR Software LLC
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.
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 stringusing namespace std;string foo;foo = "filename.txt";functionThatTakesACString( foo.c_str() );
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.
Charles Reed, CEO of CJWR Software LLC

This topic is closed to new replies.

Advertisement