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. -
-
#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();
-
}
-
-
-
-
// 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....
-
-
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! 4 2026
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.
thank you for the quick reply, unfortunately I have to use the language C. It can not be C++ or any other language.
OK.
You can use a discriminator. This is a struct with a pointer to the actual data: - struct MyType
-
{
-
void* theData;
-
unsigned int theType;
-
};
-
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: - MyType* TablePtr[10];
-
MyType* discrim = new malloc(sizeof(MyType));
-
sDummy2* stuff = new malloc(sizeof(sDummy2));
-
/*populate stuff with values.*/
-
/*store the address of stuff inside discrim*/
-
discrim->theData = stuff;
-
/*put in the discriminator. Let's say an sDummy2 is
-
#define DUMMY2 10. You will have a #define like this for each of yur structs */
-
discrim->theType = DUMMY2;
-
-
/*Then insert into the array */
-
TablePtr[0] = discrim;
-
To process data you write functions for each of your structs that take MyType* arguments: - int AnsDummy2Function(MyType* arg)
-
{
-
if (arg->theType != DUMMY2)
-
{
-
-
/* arg is not an sDummy2 */
-
return 1; /*fail */
-
}
-
/*process arg as an sDummy2
-
-
return 0; /*success */
-
-
}
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.
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! Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
by: Paul Cook |
last post by:
Hi,
I have three tables:
Countries:
ID
Country
States:
ID
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |