Hello, I've got a function that returns a memory addres of an array. However making the char * static seems to be the only solution. However it since its static it doesnt get cleaned out so everything is kept.

#include <iostream> // cout << endl ...
#include <fstream> // for reading in the file ...
#include <cstring> // for comparing strings ...

using namespace std;

char temp[100]; // Global array to hold line by line

char  * getsym();
void file_input(void);

int main()
{
	char * a;

	file_input();
	
	a = getsym();
	cout <<a;

	a = getsym();
cout << a;
	

return 0;
}

//sorts out the temp array and returns tokens.
char * getsym()
{

	char buffer[10];
    char ch;

    cout << "getting the next token" << endl;
    static int i = 0;
    ch = temp[i];

	if(!isdigit(ch))
	{
	
		while(isalpha(ch))
			{
			buffer[i] = ch;
			cout << "***Is alpha function called *** " << endl;
			i++;
			ch = temp[i];
			}
	return buffer;
	}

	if(!isalpha(ch))
	{
		while(isdigit(ch))
		{
		buffer[i] = ch;
		cout << "***Is digit function called *** " << endl;
		i++;
		ch = temp[i];
	
		}
	
	return buffer;
	}


    if(ch == '+')
    {
        cout << "\nLanguage symbol found = " << ch << endl;
		buffer[0] = '+';
        return (buffer);
    }

    if(ch == '$')
    {
    cout << "\nReserved symbol found = " << ch << endl;
    i++;
    }

return NULL;
}

I've got a theory of how to fix it. put a for loop in that loops round 10 times filling it with. Err Don't know. Can anyone help with this? point me in the right direction ? (no pun intended? )

Thanks.

Recommended Answers

All 9 Replies

You rarely want to return the address of a local variable, and then the only way for it to work is to declare it as static. Why? Because local variables are created when the function begins and destroyed when the function ends. The memory you're referencing no longer belongs to you. Use a pointer and dynamic memory if you want something persistent but unique for each call.

Or better yet, don't use global variables at all and pass both the line and a buffer to getsyn.

Use a pointer and dynamic memory if you want something persistent but unique for each call.

but won't this meanthat getsym will take parameters? Since it can't :(

>but won't this meanthat getsym will take parameters?
No, you declare a pointer inside of getsym and allocate memory inside of getsym. Then you return a pointer to that memory from getsym:

char *getsym()
{
  char *buffer = new char[100];

  // Blah blah blah

  return buffer;
}

No parameters are needed for that solution, even though I think a global variable for a line in the file is the height of stupidity.

hmm only problem with that is i get 'bbb====&^%$$%%&' i only want bbb.

Member Avatar for iamthwee

I would consider using char arrays instead of strings to be the height of stupidity. There really is no excuse for not using them in c++.

They make tokenising and the like significantly easier. If I were you I'd rethink your plan...There's a more elegant solution and I think you've just missed the bus :(

hmm yes strings sound like an option however chars are needed for this projct myfriend. I was just wondering how I'd be able to get rid of all those junkand only return bbb. instead of bbb and serveral chunks of memory afterwards!

Any ideas? Thanks for all the replies so far.

> hmm only problem with that is i get 'bbb====&^%$$%%&' i only want bbb.
As in, you're getting garbage characters or it's not tokenizing like you want? The latter would be a logic issue and you'll need to post your current code. The former means you're not null terminating the string.

> hmm only problem with that is i get 'bbb====&^%$$%%&' i only want bbb.
As in, you're getting garbage characters or it's not tokenizing like you want? The latter would be a logic issue and you'll need to post your current code. The former means you're not null terminating the string.

Hello thanks the latter fixed the bug. However another as as pop'd up. putting bbb+11 into the text file creates the following outpout:

getting the next token
*** Is alpha function called***
*** Is alpha function called***
*** Is alpha function called***
bbb
getting the next token
getting the next token
Press any key to continue

Hmm it doesnt like the plus symbol even though ive put a condition statement to pick it up :eek:

#include <iostream> // cout << endl ...
#include <fstream> // for reading in the file ...
#include <cstring> // for comparing strings ...

using namespace std;

char temp[100]; // Global array to hold line by line

char  * getsym();
void file_input(void);

int main()
{
	char * a;

	file_input();
	
	a = getsym();
	cout <<a;

a = getsym();

cout << a;
	
	a = getsym();
	cout <<a;
	

return 0;
}

//sorts out the temp array and returns tokens.
char * getsym()
{

	
	char * buffer= new char[20];;
    char ch;
    static int i = 0;
	int j = 0;

    cout << "getting the next token" << endl;
   
    ch = temp[i];

	if(!isdigit(ch))
	{
		while(isalpha(ch))
			{
			buffer[j] = ch;
				cout << "***Is alpha function called *** " << endl;
				i++;
				j++;
				ch = temp[i];
			}
	buffer[j] = '\0'; //terminate the string
	return buffer;
	}

	if(!isalpha(ch))
	{
		while(isdigit(ch))
		{
			buffer[j] = ch;
				cout << "***Is digit function called *** " << endl;
				i++;
				j++;
				ch = temp[i];
		}
		buffer[j] = '\0';
		return buffer;
	}


    if(ch == '+')
    {
        cout << "\nLanguage symbol found = " << ch << endl;
		buffer[j] = '+';
        return (buffer);
    }

    if(ch == '$')
    {
		cout << "\nReserved symbol found = " << ch << endl;
		i++;
    }

return "Error";
}

//Reads the file and puts the incomming char's into the temp array
void file_input(void)
{
    char ch;
    static int i = 0;
    ifstream infile;
    infile.open("temp"); // (a,b,c)
        if(infile.fail())
        {
            cout << "unable to locate file " << endl;
            exit(1); // error causes a problem
        }
        else
        {
            while((ch = infile.get() )!= EOF) // checks for EOF
            {
            if (ch != ' ') // strips the whitespaces and adds the chars into the temp buffer
                {
                temp[i] = ch;
                i++;
                }

            }
        }
}

Tell me, is '+' a digit? If not, why would you assume that it would make it past the first conditional test?

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.