English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I want main() to repeatedly calls a function called getToken().
I want getToken to read the next token in the input file, and then return to main() the token type, the actual string (lexeme) of the token.

The token type is an integer (1, 2, 3, whatever) and the string is, well, a string.

How can I make getToken() return both an int and a string?

2006-10-03 02:56:40 · 6 answers · asked by Anonymous in Computers & Internet Programming & Design

6 answers

Use somethiing like:

int getToken (char **tokString)
{
int retvalue;
char *str1;

//code in here to assign str1 to the token string and retvalue to token type

*tokString = str1;
return retvalue;
}

Your calling function would look something like this.

char *str; (declare it, don't define it)
int toktype;
toktype = getToken(&str);

I used to use this all the time in my Visual C++ code (actually replace char with TCHAR). It's a lot better than using a struct.

But watch for memory leaks.

===

You could also make the integer a pointer in the argument, of course.

In fact, you can return as many variables as you want as function arguments, if you do them as pointers. Just be aware of memory leaks (again) and scope.

Hope this helps.

2006-10-03 03:41:48 · answer #1 · answered by TJ 6 · 0 0

I don't think template will help u, because string is not a basic data type.
why dont u just read all inputs as string and then in the main (or any function), check if it is string or number. According to result of that check, use atoi(char *) function to get the integer value of the string.

2006-10-03 03:07:46 · answer #2 · answered by Seyed Salim T 2 · 0 0

i imagine you're false impression the theory. you've the important() on your important software. This software receives linked in with libraries and could change into this methodology itself. enable's say this is going to be myprog.exe. From this important, you're making function calls which contain: effect=myfunc(123); by skill of this, you're calling your subroutine, myfunc() with parameter 123. at the same time as your myfunc subroutine finishes, it exits and it comes again on your important all by skill of itself. It returns a fee and the fee get loaded to "effect" variable. Then, your important software maintains. You not in any respect call important() out of your subroutine.

2016-11-26 00:26:01 · answer #3 · answered by ? 4 · 0 0

C++ standard library has a template called "pair".

#include

using namespace std;

pair getToken() {
....
pair p(TYPE1,"something");
return p;
}

The contents can be retrieved with data members .first and .second. "pair" is basically a struct, as suggested by previous anwers. Just you do not need to define it yourself.

see http://www.unc.edu/depts/case/pgi/pgC++_lib/stdlibcr/pai_5818.htm

2006-10-03 04:03:16 · answer #4 · answered by muon 3 · 0 0

Create a struct -

struct Foo
{
int zint;
char* zstr;
};

Then use it like this -

Foo temp;
temp.zint = 1;
temp.zstr = "test";

2006-10-03 03:07:51 · answer #5 · answered by Anonymous · 1 0

read about --templates---....
the solution could be something like that:

template
TT getToken()
{
TT retValue;
//..........
return retValue;
}

void main()
{
int t = getToken();
char c = getToken();
//..... goodLuck
}

2006-10-03 03:59:50 · answer #6 · answered by Ugi 2 · 0 0

fedest.com, questions and answers