Support Forums
[C][help request] variable with typestruct - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Programming with C++ (https://www.supportforums.net/forumdisplay.php?fid=20)
+---- Thread: [C][help request] variable with typestruct (/showthread.php?tid=774)



[C][help request] variable with typestruct - suricata - 10-08-2009

I have this code:

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:

Code:
var='a';
est[1].[var]=0;

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

Greetings!


RE: [C][help request] variable with typestruct - g4143 - 10-08-2009

Do you mean something like this?

Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
     unsigned short a, b, c, d, e, f, g, h;
}est_;

est_ est[512];

int main(int argc, char**argv)
{
  est[0].a = 123;
  est[0].b = 124;
  est[0].c = 125;
  est[0].d = 126;
  est[0].e = 127;
  est[0].f = 128;
  est[0].g = 129;
  est[0].h = 130;

  fprintf(stdout, "ans->%d\n", est[0].a);
  fprintf(stdout, "ans->%d\n", est[0].b);
  fprintf(stdout, "ans->%d\n", est[0].c);
  fprintf(stdout, "ans->%d\n", est[0].d);
  fprintf(stdout, "ans->%d\n", est[0].e);
  fprintf(stdout, "ans->%d\n", est[0].f);
  fprintf(stdout, "ans->%d\n", est[0].g);
  fprintf(stdout, "ans->%d\n", est[0].h);
  exit(EXIT_SUCCESS);
}

The other thing your trying to do is illegal.
var='a';
est[1].[var]=0;
The value 'a' has no meaning in address resolution. Its just the value 0x61 hex...G4143

If this is where your C++ tutorial or book is taking you then get rid of them because your way off with this one...


RE: [C][help request] variable with typestruct - charnet3d - 10-08-2009

You can't do it that way. But you can do something about it so that it behaves like what you imagined.
if you can explain to me what you need to do in more details I may be able to help you..


RE: [C][help request] variable with typestruct - suricata - 10-08-2009

The question was:

Code:
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;
}

But i solved it with a two dimension array Smile

Thanks anyway for the replies!


RE: [C][help request] variable with typestruct - g4143 - 10-08-2009

(10-08-2009, 07:24 PM)charnet3d Wrote: You can't do it that way. But you can do something about it so that it behaves like what you imagined.
if you can explain to me what you need to do in more details I may be able to help you..

You don't know what he wants but you can imagine a way to make it work in C++...This makes all kinds of sense. I can just see the answer...Its supposed to do something like the thing you want but its waiting for you to define what it is supposed to be....G4143


RE: [C][help request] variable with typestruct - charnet3d - 10-09-2009

suricata Wrote:But i solved it with a two dimension array Smile

yeh cool then, because all the variables inside the struct are of the same type, doing it that way is the right thing