Hi..

whats the best way to initialize a char* ? i tried using char* p = ""; and it gives segmentation fault at times. not sure how to initialize it.

this is the function where i'm using it. screenReturnValue variable is not getting initialised properly everytime, when i call this method the second time it returns a value which is a concatanation of last value and current value.

char*
generateReturnValue(string screenClassName)
{
	char* screenReturnValue = "";
	char* ptr = "Ptr";
	strcat(screenReturnValue,screenClassName.c_str());
	strcat(screenReturnValue,ptr);
	return screenReturnValue;
}

thanks

char *p = NULL;

Because p is a pointer so it should not be pointing to anything hence NULL. If you want to nitialize pointer, initialize it with valid address;

like:

char *name = "ABCD";
char *p = name;

sorry, i forgot to mention that i tried that too but even that doesn't work. i dont think using char*'s is proving to be a good idea.

>>strcat(screenReturnValue,screenClassName.c_str());

You are attemtping to copy screenClassName into some unallocated memory pointer. screenReturnValue must be allocated before executing this line, and it must be allocated enough memory to hold both screenClassName and "Ptr". To fix it, use the new function to allocate the memory char* screenReturnValue = new char[screenClassName.size() + strlen("Ptr") + 1];

and don't forget to delete the memory once you're done with it

Thanks guys that worked... i think that was quite a decent mistake at this stage .... thanks again

-chandra

thanks again

-chandra

Aha, it's you! Didn't recognize you with your new name. I have avatars switched off..

:) .. yeah it's me ... was trying to get it changed to just 'chandra' but that wasnt available so now its a totally new one...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.