Code: Select all
getline(myfile, com);
comp = &(*com);
Moderator: Coders of Rage
Code: Select all
getline(myfile, com);
comp = &(*com);
if comp is a String* and com is a StringDaxtorax 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.
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?Code: Select all
getline(myfile, com); 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?avansc wrote:if comp is a String* and com is a StringDaxtorax 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.
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?Code: Select all
getline(myfile, com); comp = &(*com);
then it would be comp = &com;
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.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?
show me an example script.Daxtorax wrote: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.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 cut down my code to include the section that uses this:avansc wrote:show me an example script.Daxtorax wrote: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.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?
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;
}
}
}
Code: Select all
if.*
int1
=
int2
http://elysianshadows.com/phpBB3/viewto ... ers#p36043Daxtorax wrote:I cut down my code to include the section that uses this:avansc wrote:show me an example script.Daxtorax wrote: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.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?The way the script would look might be something like 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; } } }
basically just a way to check to see if two set variables are equal, i left the comp1p = &(*comp1);Code: Select all
if.* int1 = int2
just to show the areas of trouble.
Do you think you might be able to?avansc wrote:http://elysianshadows.com/phpBB3/viewto ... ers#p36043Daxtorax wrote:I cut down my code to include the section that uses this:avansc wrote:show me an example script.Daxtorax wrote: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.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?The way the script would look might be something like 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; } } }
basically just a way to check to see if two set variables are equal, i left the comp1p = &(*comp1);Code: Select all
if.* int1 = int2
just to show the areas of trouble.
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.
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;
}
I think there is a fundamental misunderstanding here.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.
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?Code: Select all
getline(myfile, com); comp = &(*com);
Code: Select all
comp = &(*com);
Code: Select all
&(*com);
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.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?
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;
}