Pointer Dilemna

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Pointer Dilemna

Post 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?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Dilemna

Post 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;
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Re: Pointer Dilemna

Post 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?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Dilemna

Post 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?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Re: Pointer Dilemna

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Dilemna

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Re: Pointer Dilemna

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Dilemna

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Re: Pointer Dilemna

Post 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?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Dilemna

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Dilemna

Post 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;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Pointer Dilemna

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Dilemna

Post 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;
}

Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Re: Pointer Dilemna

Post 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. :)
Post Reply