Page 1 of 1

Pointer Dilemna

Posted: Mon Nov 02, 2009 11:03 pm
by Daxtorax
I've just recently tried to start incorporating pointers into my program when I stumbled. I don't really know how to explain what I'm trying to do, but it doesn't compile. I'm basically trying to add my own form of scripting and when the script calls a variable and i read the script i want to be able to have a pointer point to the address of the referenced value. Below is what I think this should look like however it doesn't compile as is.

Code: Select all

getline(myfile, com);
comp = &(*com); 
This obviously isn't my entire program but were assuming that comp is a string pointer and com is obviously a string. There's probably something I'm not thinking through properly and i tried google however i don't tink i worded my search properly, any help would be greatly appreciated, and possibly an example?

Re: Pointer Dilemna

Posted: Mon Nov 02, 2009 11:12 pm
by avansc
Daxtorax wrote:I've just recently tried to start incorporating pointers into my program when I stumbled. I don't really know how to explain what I'm trying to do, but it doesn't compile. I'm basically trying to add my own form of scripting and when the script calls a variable and i read the script i want to be able to have a pointer point to the address of the referenced value. Below is what I think this should look like however it doesn't compile as is.

Code: Select all

getline(myfile, com);
comp = &(*com); 
This obviously isn't my entire program but were assuming that comp is a string pointer and com is obviously a string. There's probably something I'm not thinking through properly and i tried google however i don't tink i worded my search properly, any help would be greatly appreciated, and possibly an example?
if comp is a String* and com is a String
then it would be comp = &com;

Re: Pointer Dilemna

Posted: Mon Nov 02, 2009 11:18 pm
by Daxtorax
avansc wrote:
Daxtorax wrote:I've just recently tried to start incorporating pointers into my program when I stumbled. I don't really know how to explain what I'm trying to do, but it doesn't compile. I'm basically trying to add my own form of scripting and when the script calls a variable and i read the script i want to be able to have a pointer point to the address of the referenced value. Below is what I think this should look like however it doesn't compile as is.

Code: Select all

getline(myfile, com);
comp = &(*com); 
This obviously isn't my entire program but were assuming that comp is a string pointer and com is obviously a string. There's probably something I'm not thinking through properly and i tried google however i don't tink i worded my search properly, any help would be greatly appreciated, and possibly an example?
if comp is a String* and com is a String
then it would be comp = &com;
Say that I have an integer int1 equal to 3 for the sake of calling an integer through my scripting to my program and com turns out to equal int1 wouldn't that just set comp1 equal to int1, rather than setting comp1 equal to 3? So how would I do that in that situation?

Re: Pointer Dilemna

Posted: Mon Nov 02, 2009 11:21 pm
by avansc
you will have to explain better than that.

do you want to be able to create variables in you scripting language, or do you have a set variable in the program that you want to change?

Re: Pointer Dilemna

Posted: Mon Nov 02, 2009 11:24 pm
by Daxtorax
avansc wrote:you will have to explain better than that.

do you want to be able to create variables in you scripting language, or do you have a set variable in the program that you want to change?
I have set variables that your able to change the value of, but in the case of this I just want the ability to call the value of the set variables.

Re: Pointer Dilemna

Posted: Mon Nov 02, 2009 11:29 pm
by avansc
Daxtorax wrote:
avansc wrote:you will have to explain better than that.

do you want to be able to create variables in you scripting language, or do you have a set variable in the program that you want to change?
I have set variables that your able to change the value of, but in the case of this I just want the ability to call the value of the set variables.
show me an example script.

Re: Pointer Dilemna

Posted: Mon Nov 02, 2009 11:33 pm
by Daxtorax
avansc wrote:
Daxtorax wrote:
avansc wrote:you will have to explain better than that.

do you want to be able to create variables in you scripting language, or do you have a set variable in the program that you want to change?
I have set variables that your able to change the value of, but in the case of this I just want the ability to call the value of the set variables.
show me an example script.
I cut down my code to include the section that uses this:

Code: Select all

            if (com == "if.*")
            {
                getline(myfile, comp1);
                comp1p = &(*comp1);
                getline(myfile, com);
                if (com == "=")
                {
                    getline(myfile, comp2);
                    comp2p = &(*comp2);
                    if (*comp1p == *comp2p)
                    {
                        cout << "The Statement is True" << endl;
                    }
                    else
                    {
                        cout << "The Statement is False" << endl;
                    }
                }
            }
The way the script would look might be something like this:

Code: Select all

if.*
int1
=
int2
basically just a way to check to see if two set variables are equal, i left the comp1p = &(*comp1);
just to show the areas of trouble.

Re: Pointer Dilemna

Posted: Mon Nov 02, 2009 11:40 pm
by avansc
Daxtorax wrote:
avansc wrote:
Daxtorax wrote:
avansc wrote:you will have to explain better than that.

do you want to be able to create variables in you scripting language, or do you have a set variable in the program that you want to change?
I have set variables that your able to change the value of, but in the case of this I just want the ability to call the value of the set variables.
show me an example script.
I cut down my code to include the section that uses this:

Code: Select all

            if (com == "if.*")
            {
                getline(myfile, comp1);
                comp1p = &(*comp1);
                getline(myfile, com);
                if (com == "=")
                {
                    getline(myfile, comp2);
                    comp2p = &(*comp2);
                    if (*comp1p == *comp2p)
                    {
                        cout << "The Statement is True" << endl;
                    }
                    else
                    {
                        cout << "The Statement is False" << endl;
                    }
                }
            }
The way the script would look might be something like this:

Code: Select all

if.*
int1
=
int2
basically just a way to check to see if two set variables are equal, i left the comp1p = &(*comp1);
just to show the areas of trouble.
http://elysianshadows.com/phpBB3/viewto ... ers#p36043

look at that. i would store all variables as char*. just convert them to whatever you want. atoi and atof will convert them to integers and floats respectivly
if you dont understand how you can use this let me know and i'll try and make some pseudo code.

Re: Pointer Dilemna

Posted: Mon Nov 02, 2009 11:43 pm
by Daxtorax
avansc wrote:
Daxtorax wrote:
avansc wrote:
Daxtorax wrote:
avansc wrote:you will have to explain better than that.

do you want to be able to create variables in you scripting language, or do you have a set variable in the program that you want to change?
I have set variables that your able to change the value of, but in the case of this I just want the ability to call the value of the set variables.
show me an example script.
I cut down my code to include the section that uses this:

Code: Select all

            if (com == "if.*")
            {
                getline(myfile, comp1);
                comp1p = &(*comp1);
                getline(myfile, com);
                if (com == "=")
                {
                    getline(myfile, comp2);
                    comp2p = &(*comp2);
                    if (*comp1p == *comp2p)
                    {
                        cout << "The Statement is True" << endl;
                    }
                    else
                    {
                        cout << "The Statement is False" << endl;
                    }
                }
            }
The way the script would look might be something like this:

Code: Select all

if.*
int1
=
int2
basically just a way to check to see if two set variables are equal, i left the comp1p = &(*comp1);
just to show the areas of trouble.
http://elysianshadows.com/phpBB3/viewto ... ers#p36043

look at that. i would store all variables as char*. just convert them to whatever you want. atoi and atof will convert them to integers and floats respectivly
if you dont understand how you can use this let me know and i'll try and make some pseudo code.
Do you think you might be able to?

Re: Pointer Dilemna

Posted: Mon Nov 02, 2009 11:45 pm
by avansc
hey, its a little late right now, but send me a pm on here so i dont forger and tomorrow i'll post a small scripting engine.

Re: Pointer Dilemna

Posted: Tue Nov 03, 2009 11:56 am
by avansc
not sure if this is what you wanted, but this way you can register variables from your program and change them through your scripting language.
you can also create new variabes. note there is not error checking in here.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <map>

class cDB
{
public:
	cDB();
	void registerInt(char *name, int &ref);
	int getInt(char *name);
	void changeInt(char *name, int val);
	void createInt(char *name, int val);

private:
	std::map<char*, int*> intMap;
};

cDB::cDB()
{
}

void cDB::registerInt(char *name, int &ref)
{
	this->intMap[name] = &ref;
}

int cDB::getInt(char *name)
{
	return *this->intMap[name];
}

void cDB::changeInt(char *name, int val)
{
	*this->intMap[name] = val;
}

void cDB::createInt(char *name, int val)
{
	this->intMap[name] = (int*)malloc(sizeof(int));
	*this->intMap[name] = val;
}

int main()
{
	cDB *test = new cDB();

	int a = 10;
	printf("local int a : %d\n", a);

	test->registerInt("int1", a);
	printf("non local reference int1 : %d\n\n", test->getInt("int1"));

	printf("changing registered int(non local) to 1\n\n");
	test->changeInt("int1", 1);
	printf("local int a : %d\n", a);
	printf("non local reference int1 : %d\n\n", test->getInt("int1"));

	printf("changing local int to 20\n\n");

	a = 20;
	printf("local int a : %d\n", a);
	printf("non local reference int1 : %d\n\n", test->getInt("int1"));

	printf("creating int2 value of 123\n");
	test->createInt("int2", 123);
	printf("calling int2 that was just created %d\n", test->getInt("int2"));


	return 0;
}

Re: Pointer Dilemna

Posted: Tue Nov 03, 2009 2:03 pm
by Falco Girgis
Daxtorax wrote:I've just recently tried to start incorporating pointers into my program when I stumbled. I don't really know how to explain what I'm trying to do, but it doesn't compile. I'm basically trying to add my own form of scripting and when the script calls a variable and i read the script i want to be able to have a pointer point to the address of the referenced value. Below is what I think this should look like however it doesn't compile as is.

Code: Select all

getline(myfile, com);
comp = &(*com); 
This obviously isn't my entire program but were assuming that comp is a string pointer and com is obviously a string. There's probably something I'm not thinking through properly and i tried google however i don't tink i worded my search properly, any help would be greatly appreciated, and possibly an example?
I think there is a fundamental misunderstanding here.

When C/++ is compiled (or any other language for that matter), there are no such things as variable "names." Each variable name is replaced by an associated address. What you are trying to do with:

Code: Select all

comp = &(*com);
is take the address of the variable named "com." At runtime, variable names don't exist. What this line is actually doing is setting the character array comp equal to the starting address of the com character array that you allocated in your C program. You aren't magically retrieving a variable from your script.

Code: Select all

&(*com);
com is the address of the first character in the character array. You are dereferencing it then taking its memory address (redundant). Definitely not what you want to do.
Daxtorax wrote: Say that I have an integer int1 equal to 3 for the sake of calling an integer through my scripting to my program and com turns out to equal int1 wouldn't that just set comp1 equal to int1, rather than setting comp1 equal to 3? So how would I do that in that situation?
Exactly. How would you do that? You have to have some sort of way of associating a name with a variable or memory address. Because at compile-time these associations cease to exist. It looks like you are understanding why what you are trying to do will never work.

Your example of your "scripting engine" is not going to work the way you would like. If you take a look at avansc's code, you will see that he used the STL map to create a relationship between variable names and their values. Each integer has an associated name that you can reference it by.

Re: Pointer Dilemna

Posted: Tue Nov 03, 2009 3:57 pm
by avansc
okay. this has simple scripting in. its very basic, but should be what you need. it just has an if and print clause, but you can add others. its also lexically dumb.

here is the sample script.

Code: Select all

if
int1
<
int2
{
print
inside if
}
print
outside if

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <map>

class cDB
{
public:
	cDB();
	void registerInt(char *name, int &ref);
	int getInt(char *name);
	void changeInt(char *name, int val);
	void createInt(char *name, int val);

	// scripting related //

	void runScript(char *script);
	bool checkIf(char *var1, char *condition, char *var2);

	///////////////////////

private:
	std::map<char*, int*> intMap;
};

cDB::cDB()
{
}

void cDB::runScript(char *script)
{
	FILE *fp = fopen(script, "r");

	char *line = (char*)malloc(sizeof(char)*80);
	char *args[3];

	while(fgets(line, 80, fp) != NULL)
	{
		line[strlen(line)-1] = '\0';
		if(strcmp(line, "if") == 0)
		{
			for(int a = 0;a < 3;a++)
			{
				fgets(line, 80, fp);
				line[strlen(line)-1] = '\0';
				args[a] = strdup(line);
			}
			if(!this->checkIf(args[0], args[1], args[2]))
			{
				int count = 0;
				while(fgets(line, 80, fp) != NULL)
				{
					line[strlen(line)-1] = '\0';
					if(strcmp(line, "{") == 0)
						count++;
					if(strcmp(line, "}") == 0)
						count--;
					if(count == 0)
						break;
				}
			}
		}else if(strcmp(line, "print") == 0)
		{
			fgets(line, 80, fp);
			line[strlen(line)-1] = '\0';
			printf("%s\n", line);
			//this->print(line);
		}
	}

	fclose(fp);
}

bool cDB::checkIf(char *var1, char *condition, char *var2)
{
	int x1=0;
	int x2=1;

	for( std::map<char*, int*>::iterator ii=intMap.begin(); ii!=intMap.end();ii++)
	{
		if(strcmp((*ii).first, var1) == 0)
		{
			x1 = *(*ii).second;
		}
		if(strcmp((*ii).first, var2) == 0)
		{
			x2 = *(*ii).second;
		}
	}

	if(strcmp(condition, "==") == 0)
	{
		if(x1 == x2)
		{
			return true;
		}
	}else if(strcmp(condition, "!=") == 0)
	{
		if(x1 != x2)
		{
			return true;
		}
	}else if(strcmp(condition, "<=") == 0)
	{
		if(x1 <= x2)
		{
			return true;
		}
	}else if(strcmp(condition, ">=") == 0)
	{
		if(x1 >= x2)
		{
			return true;
		}
	}else if(strcmp(condition, "<") == 0)
	{
		if(x1 < x2)
		{
			return true;
		}
	}else if(strcmp(condition, ">") == 0)
	{
		if(x1 > x2)
		{
			return true;
		}
	}

	return false;
}

void cDB::registerInt(char *name, int &ref)
{
	this->intMap[name] = &ref;
}

int cDB::getInt(char *name)
{
	return *this->intMap[name];
}

void cDB::changeInt(char *name, int val)
{
	*this->intMap[name] = val;
}

void cDB::createInt(char *name, int val)
{
	this->intMap[name] = (int*)malloc(sizeof(int));
	*this->intMap[name] = val;
}

int main()
{
	cDB *test = new cDB();

	/*int a = 10;
	printf("local int a : %d\n", a);

	test->registerInt("int1", a);
	printf("non local reference int1 : %d\n\n", test->getInt("int1"));

	printf("changing registered int(non local) to 1\n\n");
	test->changeInt("int1", 1);
	printf("local int a : %d\n", a);
	printf("non local reference int1 : %d\n\n", test->getInt("int1"));

	printf("changing local int to 20\n\n");

	a = 20;
	printf("local int a : %d\n", a);
	printf("non local reference int1 : %d\n\n", test->getInt("int1"));

	printf("creating int2 value of 123\n");
	test->createInt("int2", 123);
	printf("calling int2 that was just created %d\n", test->getInt("int2"));

	void *x1 = (void*)test->getInt("int1");
	void *x2 = (void*)20;

	if(x1 == x2)
		printf("equal\n");*/

	test->createInt(strdup("int1"), 1);
	//int a = 1;
	//test->registerInt("int1", a);
	
	test->createInt(strdup("int2"), 10);

	test->runScript("a.s");

	//test->changeInt("int2", 1);
	//test->runScript("a.s");

	return 0;
}


Re: Pointer Dilemna

Posted: Tue Nov 03, 2009 6:57 pm
by Daxtorax
Thanks for all the help, I knew it wouldn't be possible to use something like comp1p = &(*comp1); and wasn't what I was wanting anyway. Thank you for the examples and I'll look more into the map.h, sorry if that makes me sound like a noob. :)