Jump to content

Help request with structures!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
suricata

suricata

    Newbie

  • Members
  • Pip
  • 3 posts
I have this code:

typedef struct {

	unsigned short a, b, c, d, e, f, g, h;

}est_;

est_ est[512];

I want to know if there is a way to access to a variable of est by this kind of thing:

var='a';

est[1].[var]=0;

Yes, it doesnt work but, there is a way to do such a thing?

Greetings!

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
You're trying to call a variable name as a variable in itself, and no, you can't do that. It's very easy to do this:
est[1].a = 0;
But you can't try and use the value of a variable as the name of another variable, it's simply impossible. I'm not even sure why you'd want to try and do this, is there some reason for it?
Wow I changed my sig!

#3
suricata

suricata

    Newbie

  • Members
  • Pip
  • 3 posts
Ok the question is that i need it inside a function . . .


int f_mov(char uno, char dos, int op, int pid)

{

	if(oneop(op) || op == O_NULL)

		return ERROR;

        /*Here, i want to do something like:

        est[pid].[uno]=est[pid].[dos];

        or something to avoid a lot of comparisions*/

	return OK;

}


#4
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts

ZekeDragon said:

You're trying to call a variable name as a variable in itself, and no, you can't do that. It's very easy to do this:
est[1].a = 0;
But you can't try and use the value of a variable as the name of another variable, it's simply impossible. I'm not even sure why you'd want to try and do this, is there some reason for it?

Mwoahaha wanna bet? :rules: Well, technically you are right, but this might help you suricata:

typedef struct {
       unsigned short a, b, c, d, e, f, g, h;
    }est_;
    est_ est[512];
      
    int i, a;
    unsigned short* p_est[512][8];
         
    for(a = 0; a < 512; ++a)
    {
    for(i = 0; i < 8; ++i)
    {
        switch(i)
        {
            case 0:
                p_est[a][i] = &est[a].a;
                break;
            case 1:
                p_est[a][i] = &est[a].b;
                break;
            case 2:
                p_est[a][i] = &est[a].c;
                break;
            case 3:
                p_est[a][i] = &est[a].d;
                break;
            case 4:
                p_est[a][i] = &est[a].e;
                break;
            case 5:
                p_est[a][i] = &est[a].f;
                break;
            case 6:
                p_est[a][i] = &est[a].g;
                break;
            case 7:
                p_est[a][i] = &est[a].h;
                break; 
        }                   
    }
    }
As you can see we now have a two dimensional array of pointers.
To access for example est[23].g we would do:

*p_est[23][6] = 5; /* Set est[23].g to 5 */
So to fill every est (all 512) a,b,c,d,e,f,g,h with fives you could do:

    for(a = 0; a < 512; ++a)    
        for(i = 0; i < 8; ++i)
            *p_est[a][i] = 5;       

Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#5
suricata

suricata

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks for the replies. . . . two dimension arrays is the solution :) (thank you marwex89)

Greetings!

#6
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
No problem :)
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa