10 Replies - 1443 Views - Last Post: 29 March 2012 - 01:39 PM Rate Topic: -----

#1 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Assigning strings as variable names. (incognito) part 2

Posted 25 March 2012 - 05:06 PM

A c program problem.
Ok. After some study I now understand my confusion.
In this thread: Assigning strings as variable names. (incognito) I was way off on what I was thinking would work for assignment of string parts for variables.

I've realized that this is the actual thing I should be focusing on.

Quote

struct Variables 
{
  char name[MAXNAME+1];
  char datatype;

  int     i;
  double  d;
  char    ch;   

};


There is only one data type at a time for each variable, so a union can be used for the values - to save space/memory.

The above was posted by: #define

I've decided to deal with tokening later.
Let's forget that in the other thread I confused macro tokens with script lexer tokens. :)
I'm starting to glean what it takes to make duck typing and I'm sure that the method would use dynamic structs.
The struct members would have to have pointers to their own memory to establish storage for user variables.

This assertion is making me confused:
There is only one data type at a time for each variable, so a union can be used for the values - to save space/memory.

So far I understand that a union creates a shared memory space for a piece of data and that space can have more than one type.

I am having problems thinking about the nature of pointers used on stuctures and unions to establish assignment and storage.

If any of the above is wrong or if there is a better way I'd appreciate if you'd tell me.

Also, could you give me some direction with this?
I'm still learning, but I'm doing the best I can to understand.

This could be a way to make new variables.
MAXNAME+1

I'm thinking this could be a way to assert type(for the duck typing).
  char datatype;

  int     i;
  double  d;
  char    ch;  


Like I said "I'm still learning" but I'm not yet to the point where I can just get this.
Until then I'm just trying to discuss where I'm wrong, where to make the next move, and what next steps to take.
Forgive me if it seems like I'm trying to jump to a place that's too high for my coding legs. :)

If it helps you to know I have some pretty good skills with php, javascript, and python, but mostly php.
php has all the logic, and loops and many of the control constructs of c.
Though I am now noticing the huge difference between how variables, functions, and other control structures are used between the languages.
:snap: <-- I'm the pirate and c is the ninja.

This post has been edited by hiddenghost: 25 March 2012 - 08:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Assigning strings as variable names. (incognito) part 2

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: Assigning strings as variable names. (incognito) part 2

Posted 25 March 2012 - 07:23 PM

Since you use set I assume C++

struct VariableValue 
{
  char type;

  int     i;
  double  d;
  char    ch;
  string   s;   
};

VariableValue value;

value.type = 'i';
value.i = 289;

set(values,"number", value);




A union can be used to save space - Unions.

struct VariableValue 
{
  char type;
  
  union
  {
    int     i;
    double  d;
    char    ch;
    string   s;   
  } v;
};

VariableValue value;

value.type = 'c';
value.v.ch = 'A';

set(values,"letter", value);




Anonymous unions can be used in C++.

struct VariableValue 
{
  char type;
  
  union
  {
    int     i;
    double  d;
    char    ch;
    string   s;   
  }; // no name hence anonymous
};

VariableValue value;

value.type = 'd';
value.d = 3.14159;

set(values,"pi", value);


Was This Post Helpful? 1
  • +
  • -

#3 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Assigning strings as variable names. (incognito) part 2

Posted 25 March 2012 - 08:17 PM

View Post#define, on 25 March 2012 - 09:23 PM, said:

Since you use set I assume C++


No I'm using c.
It's ok though. Without the oop they are very similar.

I think the only thing is the interference of the namespace with c structs.
I'll clarify that I'm using c in the top post.

Hahaha! I just realized when I looked back at that first post that I was using the word set as more of a potential type of function rather than a library one.
Had no idea C++ had that function.

You know it's funny that I thought "Here I'm gonna create a hypothetical object." and c++ already has something with the same name. Maybe I saw it in some documentation somewhere and my subconscious wanted me to think I was being creative. :)

This post has been edited by hiddenghost: 25 March 2012 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: Assigning strings as variable names. (incognito) part 2

Posted 25 March 2012 - 08:53 PM

View Posthiddenghost, on 26 March 2012 - 05:17 AM, said:

Hahaha! I just realized when I looked back at that first post that I was using the word set as more of a potential type of function rather than a library one.
Had no idea C++ had that function.


:) I didn't look too closely I was confusing set and map anyway.


In C the struct variable would need struct before it.

struct VariableValue value;




Although it can be typedef'd

typdef struct VariableValue VariableValue;

VariableValue value;



I'm wanting to give VariableValue a different name but not sure what.
Was This Post Helpful? 0
  • +
  • -

#5 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Assigning strings as variable names. (incognito) part 2

Posted 25 March 2012 - 09:04 PM

View Post#define, on 25 March 2012 - 10:53 PM, said:

typdef struct VariableValue VariableValue;

VariableValue value;



I'm wanting to give VariableValue a different name but not sure what.


Maybe something related to memory.
  • store
  • mem
  • cache
  • register
  • reg


Or something related to the unrelated. :)
  • bobble
  • hole
  • buster
  • noodleStretcher


Sorry, got a little silly there.
I'm thinking maybe a sense of humor would be valuable when trying such a heavy duty programming task.
Though weird names may get in the way of describing the method directly in the code.

This post has been edited by hiddenghost: 25 March 2012 - 09:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: Assigning strings as variable names. (incognito) part 2

Posted 25 March 2012 - 10:45 PM

Lets not get too crazy! :crazy:

And register is a keyword.
Was This Post Helpful? 0
  • +
  • -

#7 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Assigning strings as variable names. (incognito) part 2

Posted 26 March 2012 - 01:18 AM

View Post#define, on 26 March 2012 - 12:45 AM, said:

Lets not get too crazy! :crazy:

And register is a keyword.


Oh ya! Hmm. Just making brain storm is making brain fart. :wacko:

Could register be used to optimize something like this?

So if I have something like this:
value.type = 'c';
value.v.ch = 'A';

would it be right to say, I can type cast by assignment through the struct and union?
Was This Post Helpful? 0
  • +
  • -

#8 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Assigning strings as variable names. (incognito) part 2

Posted 26 March 2012 - 01:26 AM

Ok. I now have this.
#include <stdio.h>

int main(void)
{
    char preHexS = 'S', preHexm = 'm';
    int postHexS, postHexm;
    postHexS = preHexS; // postHexS converts to decimal equivalent of hex.
    postHexm = preHexm; // postHexm converts to decimal equivalent of hex.
    
    printf("S = %c, m = %c \n", preHexS, preHexm);
    printf("S in hex = %d, m in hex = %d \n", postHexS, postHexm);

    if(postHexS > 64 && postHexS < 91){ // Here we check for decimal equivalent of hex.
        printf("The value of the variable preHexS is a character.\n");
    }
    
    return 0;
    
}

It's mostly a demonstration of type checking by the decimal equivalent of the character.
So in theory instead of the line:
printf("The value of the variable preHexS is a character.\n");

I would have:
value.type = 'c'; // remember the type of preHexS
value.v.ch = preHexS; // assign the 'S' to the struct+union



I'm sure there is a better way than this, but it's a start.
There would have to be more 'and' operations too because character integers are not all in the same range.

Also, if the first character is a number the whole string would have to be checked like:
'myVariable = 1234;' // all characters checked

And if the value was quoted:
'myVariable = "my string";' // only first character is checked

The parser would see the first quote and check m then assign the whole string as a char string.

I guess that it's also possible just to treat strings as chars if there are quotes.

No quotes after an = sign would make one of the number types.

But the byte checking would assure correct type if there are no quotes like:
'myVariable = 3L24bla;' // wouldn't pass type check for number

This post has been edited by hiddenghost: 26 March 2012 - 03:57 AM

Was This Post Helpful? 0
  • +
  • -

#9 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Assigning strings as variable names. (incognito) part 2

Posted 26 March 2012 - 01:05 PM

#define, on 25 March 2012 - 10:53 PM, said:

struct makeAddress newAddress[0];

newAddress[0].v.i = 20;
newAddress[0].type = 'int';
newAddress[0].p.i = &newAddress[0].v.i;


I just realized in order to make the struct newAddres[] there has to be an initial number for the max.
Like:
struct makeAddress newAddress[99] // 100 members.


or the array would have to be dynamic.

I suppose the char length would have to be dynamic too.

One pass parsing will be hard with this.

This post has been edited by hiddenghost: 26 March 2012 - 03:45 PM

Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: Assigning strings as variable names. (incognito) part 2

Posted 26 March 2012 - 10:50 PM

Start with an array, a few hundred elements should be enough to begin. Later you could change it to a linked list if needed.
Was This Post Helpful? 0
  • +
  • -

#11 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Assigning strings as variable names. (incognito) part 2

Posted 29 March 2012 - 01:39 PM

I'm going to try that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1