473,395 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do you create a table with variables from multiple structs?

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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #define MAX_CNT 3
  3.  
  4. typedef struct
  5. {
  6.    int nNum;
  7.    int nCnt;   
  8. }sDummy1;
  9.  
  10. typedef struct
  11. {
  12.    int nAge;
  13.    double epsilon;
  14.    int nCnt;   
  15. }sDummy2;
  16.  
  17. typedef struct
  18. {
  19.    int nData;
  20.    double pi;
  21.    int nCnt;   
  22. }sDummy3;
  23.  
  24. sDummy1 *s1;
  25. sDummy2 *s2;
  26. sDummy3 *s3;
  27.  
  28. static char * sInfo[MAX_CNT] = {
  29.    (char *)s1, // removing the & from here makes no difference
  30.    (char *)&s2,
  31.    (char *)&s3
  32. };
  33.  
  34. static int * sTablePtr[MAX_CNT] = {
  35.    &s1->nCnt,
  36.    &s2->nCnt,
  37.    &s3->nCnt
  38. };
  39.  
  40. /* when i declare a table like this, it just crashes with a memory error
  41.    basically as if i was writing to a non allocated section of memory
  42. static int sTable[MAX_CNT] = {
  43.    s1->nCnt,
  44.    s2->nCnt,
  45.    s3->nCnt
  46. };
  47.  
  48. */
  49. void main()
  50. {   
  51.    int i;
  52.    int nValuesOver2;
  53.  
  54.    s1 = (sDummy1*) malloc (sizeof(sDummy1));
  55.    s2 = (sDummy2*) malloc (sizeof(sDummy2));
  56.    s3 = (sDummy3*) malloc (sizeof(sDummy3));
  57.  
  58.    memset(s1, 0, sizeof(sDummy1));
  59.    memset(s2, 0, sizeof(sDummy2));
  60.    memset(s3, 0, sizeof(sDummy3));
  61.  
  62.    s1->nCnt = 1;
  63.    s1->nNum = 1;
  64.  
  65.    s2->nAge = 2;
  66.    s2->nCnt = 2;
  67.    s2->epsilon = 2.7;
  68.  
  69.    s3->nCnt = 3;
  70.    s3->nData = 3;
  71.    s3->pi = 3.14;
  72.  
  73.    for(i = 0; i < MAX_CNT; i++)
  74.    {
  75.       // this just prints values 4, 16, and 16 for s1, s2 and s3 respectively
  76.       // when i dereference it (&sTablePtr[i]) it prints the address of the variables
  77.       // when i debug it, the values are blank inside
  78.       printf("%d: %d\n", i, sTablePtr[i]);
  79.       // I am trying to print out the values 1, 2 and 3 here.
  80.       if(sTablePtr[i] > 2)
  81.       {
  82.          nValuesOver2++;
  83.       }
  84.    }
  85.  
  86.    // These print the correct values
  87.    printf("S1: %d\n", s1->nCnt); // prints 1
  88.    printf("S2: %d\n", s2->nCnt); // prints 2
  89.    printf("S3: %d\n", s3->nCnt); // prints 3
  90.  
  91.    printf("There are %d values over 2", nValuesOver2);
  92.  
  93.    _getch();
  94. }
  95.  
  96.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. // keep in mind i have many structs, it's not limited to 3   
  3. // I'm trying to avoid this  
  4.  
  5. if(s1->nCnt > 2)
  6.    nValuesOver2++;
  7.  
  8. if(s2->nCnt > 2)
  9.    nValuesOver2++;
  10.  
  11. if(s3->nCnt > 2)
  12.    nValuesOver2++
  13.  
  14. etc....
  15.  
  16.  
Aug 1 '12 #1

✓ answered by bmerlover

Thanks a lot for your help! That pretty much did the trick, I made a few slight modifications but it's following your idea. Thanks again!

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5.  
  6. #define MAX_CNT 3
  7.  
  8. typedef struct
  9. {
  10.    int nNum;
  11.    int nCnt;   
  12. }sDummy1;
  13.  
  14. typedef struct
  15. {
  16.    int nAge;
  17.    double epsilon;
  18.    int nCnt;   
  19. }sDummy2;
  20.  
  21. typedef struct
  22. {
  23.    int nData;
  24.    double pi;
  25.    int nCnt;   
  26. }sDummy3;
  27.  
  28. typedef struct 
  29. {
  30.    void*  theData;
  31.    unsigned int theType;
  32. }MyType;
  33.  
  34. #define DUMMY1 1
  35. #define DUMMY2 2
  36. #define DUMMY3 3
  37.  
  38. int AnsDummy2Function(MyType* arg)
  39. {
  40.    if (arg->theType != DUMMY2)
  41.    { 
  42.       /* arg is not an sDummy2 */
  43.       return 1; /*fail */
  44.     }
  45.     /*process arg as an sDummy2 */
  46.  
  47.     return 0;   /*success */   
  48. }
  49.  
  50. void main()
  51. {  
  52.    int i = 0;
  53.    int nValid = 0;
  54.    MyType* TablePtr[MAX_CNT] = {malloc(sizeof(MyType)),
  55.                                 malloc(sizeof(MyType)),
  56.                                 malloc(sizeof(MyType))};
  57.  
  58.    sDummy1* s1 = malloc(sizeof(sDummy1));
  59.    sDummy2* s2 = malloc(sizeof(sDummy2));
  60.    sDummy3* s3 = malloc(sizeof(sDummy3));
  61.  
  62.    sDummy1* sd1 = malloc(sizeof(sDummy1));
  63.    sDummy2* sd2 = malloc(sizeof(sDummy2));
  64.    sDummy3* sd3 = malloc(sizeof(sDummy3));
  65.  
  66.    /*populate stuff with values.*/
  67.    s1->nCnt = 1;
  68.    s1->nNum = 4;
  69.  
  70.    s2->nCnt = 2;   
  71.    s2->nAge = 5;
  72.    s2->epsilon = 2.7;
  73.  
  74.    s3->nCnt = 3;
  75.    s3->nData = 15;
  76.    s3->pi = 3.14;
  77.  
  78.    TablePtr[0]->theData = s1;
  79.    TablePtr[0]->theType = DUMMY1;
  80.  
  81.    TablePtr[1]->theData = s2;
  82.    TablePtr[1]->theType = DUMMY2;
  83.  
  84.    TablePtr[2]->theData = s3;
  85.    TablePtr[2]->theType = DUMMY3;
  86.  
  87.    for(i = 0; i < MAX_CNT; i++)
  88.    {
  89.       nValid = AnsDummy2Function(TablePtr[i]);
  90.    }
  91.  
  92.    sd1 = (sDummy1*)TablePtr[0]->theData;
  93.    sd2 = (sDummy2*)TablePtr[1]->theData;
  94.    sd3 = (sDummy3*)TablePtr[2]->theData;
  95.  
  96.    printf("SD1: %d\n", sd1->nCnt); // prints 1
  97.    printf("SD2: %d\n", sd2->nCnt); // prints 2
  98.    printf("SD3: %d\n", sd3->nCnt); // prints 3
  99.  
  100.    _getch();
  101. }
  102.  
  103.  

4 2026
weaknessforcats
9,208 Expert Mod 8TB
Are you familiar with polymorphism?

If you use C you wil have 100 times the work over C++.

This is a common programming situation. Your table will contain addresses of objects where each object can be of a different struct.

If yu are not familiar with polymorphism, read: http://bytes.com/topic/c/insights/79...polymorphism-c.

Post again to continue the discussion.
Aug 1 '12 #2
thank you for the quick reply, unfortunately I have to use the language C. It can not be C++ or any other language.
Aug 1 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
OK.
You can use a discriminator. This is a struct with a pointer to the actual data:

Expand|Select|Wrap|Line Numbers
  1. struct MyType
  2. {
  3.    void*  theData;
  4.    unsigned int theType;
  5. };
  6.  
So you create an object of sme struct of your, let's call it Dummy2 and assgn its address to theData member of the discriminator:

Expand|Select|Wrap|Line Numbers
  1. MyType* TablePtr[10];
  2. MyType* discrim = new malloc(sizeof(MyType));
  3. sDummy2* stuff = new malloc(sizeof(sDummy2));
  4. /*populate stuff with values.*/
  5. /*store the address of stuff inside discrim*/
  6. discrim->theData = stuff;
  7. /*put in the discriminator. Let's say an sDummy2 is
  8.   #define DUMMY2 10. You will have a #define like this for each of yur structs */
  9. discrim->theType = DUMMY2;
  10.  
  11. /*Then insert into the array */
  12. TablePtr[0] = discrim;
  13.  
To process data you write functions for each of your structs that take MyType* arguments:

Expand|Select|Wrap|Line Numbers
  1. int AnsDummy2Function(MyType* arg)
  2. {
  3.    if (arg->theType != DUMMY2)
  4.    {
  5.  
  6.       /* arg is not an sDummy2 */
  7.       return 1; /*fail */
  8.     }
  9.     /*process arg as an sDummy2
  10.  
  11.     return 0;   /*success */  
  12.  
  13. }
Now if you run a loop from 0 to 9 and call AnsDummy2Function with each TablePtr element, only the elements that point to an SDummy2 will get processsed.

BTW: I just typed up this post without compiling it. I hope you see the idea here.
Aug 1 '12 #4
Thanks a lot for your help! That pretty much did the trick, I made a few slight modifications but it's following your idea. Thanks again!

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5.  
  6. #define MAX_CNT 3
  7.  
  8. typedef struct
  9. {
  10.    int nNum;
  11.    int nCnt;   
  12. }sDummy1;
  13.  
  14. typedef struct
  15. {
  16.    int nAge;
  17.    double epsilon;
  18.    int nCnt;   
  19. }sDummy2;
  20.  
  21. typedef struct
  22. {
  23.    int nData;
  24.    double pi;
  25.    int nCnt;   
  26. }sDummy3;
  27.  
  28. typedef struct 
  29. {
  30.    void*  theData;
  31.    unsigned int theType;
  32. }MyType;
  33.  
  34. #define DUMMY1 1
  35. #define DUMMY2 2
  36. #define DUMMY3 3
  37.  
  38. int AnsDummy2Function(MyType* arg)
  39. {
  40.    if (arg->theType != DUMMY2)
  41.    { 
  42.       /* arg is not an sDummy2 */
  43.       return 1; /*fail */
  44.     }
  45.     /*process arg as an sDummy2 */
  46.  
  47.     return 0;   /*success */   
  48. }
  49.  
  50. void main()
  51. {  
  52.    int i = 0;
  53.    int nValid = 0;
  54.    MyType* TablePtr[MAX_CNT] = {malloc(sizeof(MyType)),
  55.                                 malloc(sizeof(MyType)),
  56.                                 malloc(sizeof(MyType))};
  57.  
  58.    sDummy1* s1 = malloc(sizeof(sDummy1));
  59.    sDummy2* s2 = malloc(sizeof(sDummy2));
  60.    sDummy3* s3 = malloc(sizeof(sDummy3));
  61.  
  62.    sDummy1* sd1 = malloc(sizeof(sDummy1));
  63.    sDummy2* sd2 = malloc(sizeof(sDummy2));
  64.    sDummy3* sd3 = malloc(sizeof(sDummy3));
  65.  
  66.    /*populate stuff with values.*/
  67.    s1->nCnt = 1;
  68.    s1->nNum = 4;
  69.  
  70.    s2->nCnt = 2;   
  71.    s2->nAge = 5;
  72.    s2->epsilon = 2.7;
  73.  
  74.    s3->nCnt = 3;
  75.    s3->nData = 15;
  76.    s3->pi = 3.14;
  77.  
  78.    TablePtr[0]->theData = s1;
  79.    TablePtr[0]->theType = DUMMY1;
  80.  
  81.    TablePtr[1]->theData = s2;
  82.    TablePtr[1]->theType = DUMMY2;
  83.  
  84.    TablePtr[2]->theData = s3;
  85.    TablePtr[2]->theType = DUMMY3;
  86.  
  87.    for(i = 0; i < MAX_CNT; i++)
  88.    {
  89.       nValid = AnsDummy2Function(TablePtr[i]);
  90.    }
  91.  
  92.    sd1 = (sDummy1*)TablePtr[0]->theData;
  93.    sd2 = (sDummy2*)TablePtr[1]->theData;
  94.    sd3 = (sDummy3*)TablePtr[2]->theData;
  95.  
  96.    printf("SD1: %d\n", sd1->nCnt); // prints 1
  97.    printf("SD2: %d\n", sd2->nCnt); // prints 2
  98.    printf("SD3: %d\n", sd3->nCnt); // prints 3
  99.  
  100.    _getch();
  101. }
  102.  
  103.  
Aug 5 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Morten Gulbrandsen | last post by:
USE company; DROP TABLE IF EXISTS EMPLOYEE; CREATE TABLE EMPLOYEE ( # PK SSN CHAR(9) NOT NULL, # FK SUPERSSN CHAR(9), DNO INT NOT NULL DEFAULT 1, CONSTRAINT EMPPK
7
by: Dimitri Furman | last post by:
What would be the correct syntax, if any, that allows updating a table variable in a statement that uses the same table variable in a correlated subquery? Here's an example: DECLARE @t table (N1...
2
by: Paul Cook | last post by:
Hi, I have three tables: Countries: ID Country States: ID
1
by: serge | last post by:
I am running SQL Server Best Practices on a SQL 2000 database and it is recommending me to change the temp tables inside SPs to table variables. I had read already in other places to use table...
2
by: sean.gilbertson | last post by:
Hi, I have a user-defined function which returns a table (call it '@a'), and has another table defined as a variable (call it '@b'). When I try to do the following query, I get "Must declare...
0
by: Greg Corradini | last post by:
Hello, Lately I've been using the mx.ODBC module to query Access (mdb) tables. For the life of me, I can't get the 'create table' sql command to work. I use this command in Oracle and I've seen...
24
by: Dan2kx | last post by:
Hello to all that read and thank you to all that post Background: I have been tasked to create a holiday database for the employees in my lab, (so expect many more posts) im stuck at the first...
3
by: askohen | last post by:
I have this CREATE TABLE statement: CREATE TABLE howto ( id INT IDENTITY(1,1) PRIMARY KEY, title VARCHAR(100) UNIQUE NOT NULL, url VARCHAR(20) UNIQUE NOT NULL, order_on_homepage SMALLINT...
2
by: jarea | last post by:
I have read quite a bit about this error but I have yet to find the solution to my problem. I am trying to execute the following mysql statement: alter table line_items add...
2
jbt007
by: jbt007 | last post by:
All, Access 2003 - WinXP I thought this would be a no brainer, but it seems to be a perplexing problem. I have a simple table I use for importing several text reports, use VBA to run through...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.