Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

struct question

Status
Not open for further replies.

electroRF

Member
Hi,

I want to define struct (in C), which will have n items, each item is composed of an index (Unsigned int) and a pointer.

I'd like to initialize the struct upon definition, so that:

- 1st Item will have an index equal to 0, and an address &globalVar0,

- 2nd Item will have an index equal to 1, and an address &globalVar1,

- and so on.

How would you typedef such struct?

Thank you.
 
You need to be a bit clearer..

A struct is just an array of dissimilar variables.... typedef can be used with any variable..
C:
typedef unsigned char UCHAR;
typedef unsigned int UINT;
typedef struct
    {
    somevars;
    }mystruct;
As long as its a valid type and your new identifier is valid....

A struct can contain structs...
C:
typedef struct
   {
   UINT  x1 = 0;
   UINT  x2= 0;
   }globalvar;

typedef struct
   {
   globalvar global1;
   globalvar global2;
   } myglobals;

myglobals tonsofglobals;

tonsofglobals.global1.x1 = 12;
AND they can all be pointers if need be... BUT!! the big question... If they are all global why do you want pointers???
 
Hi Ian,

Thank you very much.

Thanks to your answer, I now understand that I did not phrase the question correctly.

I'd like to define and initialize a structure Array as follows:

C:
STRUCT_TYPE myStruct[ ] =
{
{INDEX0,  &gVar0},
{INDEX1, &gVar1},
...
{IndexN, &gVarN}
};

and I want that automatically INDEX0 will get the Value 0, INDEX1, will get the Value 1, and so on, as happens in ENUM.

Is it possible?

If so, how should I typedef STRUCT_TYPE ?
 
If you have an array you can index that way... The struct has the ability to hold different types that means access is by name... What you are looking for is a pointer array... What are the pointers pointing at....

BTW & is the "address of" operator.... The * is the pointer operator... If you could give me more detail I may have something else... You can always enumerate the vars in the struct... But until I know precisely whats going on I'm second guessing...
 
What I normally do with the lastest gcc for static structures.
Code:
struct spi_param_type {
uint16_t range : 1;
uint16_t bits : 2;
uint16_t link : 1;
uint16_t pic18 : 2;
uint16_t chan : 4;
struct spi_device *spi;
struct comedi_device *dev;
uint8_t device_type;
};
static struct spi_param_type spi_adc = {
.device_type = MCP3002
}, spi_dac = {
.device_type = MCP4802
};

struct pic_platform_data {
uint16_t conv_delay_usecs, cmd_delay_usecs;
int chan, timer;
struct mutex drvdata_lock;
};
static struct pic_platform_data pic_info_pic18 = {
.chan = 0,
.timer = 0,
.cmd_delay_usecs = 10,
.conv_delay_usecs = 30
};

I'm not 100% sure about the array question but here is the standard.
https://publications.gbdirect.co.uk/c_book/chapter6/initialization.html
The address constant is a pointer to an object that has static storage duration or a pointer to a function. You can get these by using the & operator or through the usual conversions of array and function names into pointers when they are used in expressions. The operators [], ., ->, & (address of) and * (pointer dereference) as well as casts of pointers can all be used in the expression as long as they don't involve accessing the value of any object.
 
Been for a walk....

I think I know what you are after.. You are thinking objectively.... In oop you get this kind of automation..

If we define a struct and just include an index variable and a pointer variable... You can write a constructor function to fill your structures..
C:
typedef struct 
   {
   int Idx;
   int * GlobPtr;
   } GLOBvar;

GLOBvar  StructArray[5];

for(x=0;x<5;x++)
   {
   StructArray[x].Idx = x;
   }

Like this you can have n arrays all index linked... All you need to do is initialize the pointers...
 
Hi,

When i have a variable length set of data i like to use malloc and/or calloc and realloc.
The data is set up as a set of bytes, in this case 8 bytes for a 32 bit system, which would be 4 for the int and 4 for the pointer. You could then malloc 8*N where N is the number needed, then you can always add on later if you end up having to add more pointers to the bunch.

Also, you do not need to add an index value to every pointer unless you plan to swap later as in a sort operation, where you still need to know the original order, or the index also points to some other data, etc.
If you gave us the exact reason you want to do this i would bet somebody here could come up with a good way to do it, perhaps optimum.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top