Hello DaniWeb!

I've created a function that parses a long string of text (JSON format, but that's not really important) for the specified JSON key/variable name, and it returns the JSON value as pointer to a char array.

For example,

//the function looks like:
char* extract_key( char* buf, char* find_this_key )

//this is how i've been calling it, if the JSON values are car_color and car_maker
char* color_of_car = extract_key(buf, "car_color");
char* brand_of_car = extract_key(buf, "car_maker");

where buf is the string and find_this_key is what we're looking for.

My problem is that I constantly use this function in different parts of my program, and I've found that even though this function is creating dynamically allocated char arrays and returning those, they are being overwritten (I think, since I'm getting bus errors) by the function when it is called again.

I'm new to C++ and am still understanding pointers. What I wanted to do was save the value of the function every time it is called into a local char array, so how would I go about this? Dereference the function? If I need to design this differently, I'm open to any suggestions, I really appreciate it.

generally speaking:

int  * myPointer;
int myValue;


myValue = *myPointer;

myValue now contains a copy of the value that is at the location currently pointed to by the pointer, myPointer.

is this what you're asking?


show the code of extract_key

Well, the problem is that this function returns an array, so I don't know how I would assign every single char? I tried what you mentioned and it only assigned the first letter...

And this appears to be an invalid initializer

char result[] =  extract_key(buf, "car_color");

Use strcpy() defined in <cstring>

char* extract_key( char* buf, char* find_this_key )

strcpy(color_of_car, extract_key(buf, "car_color") );
strcpy(brand_of_car, extract_key(buf, "car_maker") );

show the code of extract_key

Alright, it's not the most readable but here it is...

char * sf_extract_key( const char * buf, const char * find_this )
	int x, b, found_value_length, buf_length = 0;
	char found_key[MAX_KEYNAME_CHARLENGTH];
	char delim;
	char* found_value;
	while ( buf[buf_length] != '\0' ) buf_length++;
	for ( int i=0; i < buf_length; i++ )

		if ( buf[i] == '"' && buf[i-1] != '\\')
			i++; // move past starting "
			for ( b=0; b<MAX_KEYNAME_CHARLENGTH && buf[b+i] != '"' || buf[(b+i)-1] == '\\'; b++ )
				found_key[b] = buf[b+i];
			found_key[b] = '\0';

			if ( strcmp(find_this, found_key) == 0 )

				i=b+i+1; // move past ending "
				if ( buf[i] == ':' && buf[i+1] != '{' && buf[i+1] != '[') // no bracket support.
					i++; //move past :
					if (! isdigit( buf[i]) )
						delim = '\"';
						else delim=',';
					for ( found_value_length=0; buf[i+found_value_length] != delim || buf[(i+found_value_length)-1] == '\\'; found_value_length++ );
					found_value = new char[found_value_length];
						for ( x=0; x < found_value_length; x++)
					if (verbose) cout << "PARSER: parsed " << found_key <<
						" which has value "<< found_value <<endl;

					return found_value;
	return 0;

Use strcpy() defined in <cstring>

Didn't even think about that! Thanks!

>>Didn't even think about that! Thanks!
If your problem is solved, mark the thread as Solved so that no other person scratches head on your problem.

>>Didn't even think about that! Thanks!
If your problem is solved, mark the thread as Solved so that no other person scratches head on your problem.

Still having a problem, but I'll start a new thread. Since my original question was solved I went ahead and marked it.

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.