Hi, I would like to create an array of struct variables inside a table to avoid using many if/else and/or switch/case statements. I have attached a simple example of what I am trying to do, it doesn't work. For the table "sTablePtr" I see different values than what I set, when I dereference it with a "&" it just shows me the addresses of the variables. However at the end, when I do print out the values, by just using the structs themselves, they print out the values correctly.
My structure declarations must remain a pointer. Any help would be greatly appreciated. I will continue to look for a solution and post it if I do find one. Thanks
To be clear, what I am trying to do is have sTablePtr[i] show me the correct values I set, in this case I want the for loop to print out 1, 2 and 3. If there is a different way of doing this, I am open to it as long as I can use a table of my struct variables to avoid many if statements.
My structure declarations must remain a pointer. Any help would be greatly appreciated. I will continue to look for a solution and post it if I do find one. Thanks
To be clear, what I am trying to do is have sTablePtr[i] show me the correct values I set, in this case I want the for loop to print out 1, 2 and 3. If there is a different way of doing this, I am open to it as long as I can use a table of my struct variables to avoid many if statements.
Code:
#define MAX_CNT 3
typedef struct
{
int nNum;
int nCnt;
}sDummy1;
typedef struct
{
int nAge;
double epsilon;
int nCnt;
}sDummy2;
typedef struct
{
int nData;
double pi;
int nCnt;
}sDummy3;
sDummy1 *s1;
sDummy2 *s2;
sDummy3 *s3;
static char * sInfo[MAX_CNT] = {
(char *)s1, // removing the & from here makes no difference
(char *)&s2,
(char *)&s3
};
static int * sTablePtr[MAX_CNT] = {
&s1->nCnt,
&s2->nCnt,
&s3->nCnt
};
/* when i declare a table like this, it just crashes with a memory error
basically as if i was writing to a non allocated section of memory
static int sTable[MAX_CNT] = {
s1->nCnt,
s2->nCnt,
s3->nCnt
};
*/
void main()
{
int i;
int nValuesOver2;
s1 = (sDummy1*) malloc (sizeof(sDummy1));
s2 = (sDummy2*) malloc (sizeof(sDummy2));
s3 = (sDummy3*) malloc (sizeof(sDummy3));
memset(s1, 0, sizeof(sDummy1));
memset(s2, 0, sizeof(sDummy2));
memset(s3, 0, sizeof(sDummy3));
s1->nCnt = 1;
s1->nNum = 1;
s2->nAge = 2;
s2->nCnt = 2;
s2->epsilon = 2.7;
s3->nCnt = 3;
s3->nData = 3;
s3->pi = 3.14;
for(i = 0; i < MAX_CNT; i++)
{
// this just prints values 4, 16, and 16 for s1, s2 and s3 respectively
// when i dereference it (&sTablePtr[i]) it prints the address of the variables
// when i debug it, the values are blank inside
printf("%d: %d\n", i, sTablePtr[i]);
// I am trying to print out the values 1, 2 and 3 here.
if(sTablePtr[i] > 2)
{
nValuesOver2++;
}
}
// These print the correct values
printf("S1: %d\n", s1->nCnt); // prints 1
printf("S2: %d\n", s2->nCnt); // prints 2
printf("S3: %d\n", s3->nCnt); // prints 3
printf("There are %d values over 2", nValuesOver2);
_getch();
}
Code:
// keep in mind i have many structs, it's not limited to 3 // I'm trying to avoid this if(s1->nCnt > 2) nValuesOver2++; if(s2->nCnt > 2) nValuesOver2++; if(s3->nCnt > 2) nValuesOver2++ etc....
Comment