Re: Passing an array of structuresfrom a pointer?
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 30 Sep 2008 07:33:00 -0700
Hello Giovanni,
Thankyou for your post!
typedef struct ddListBox{
int x;
long y;
/* long *xxx; //<<< Pointer that holds the address of the DDLB_COLL1[]
array! */
/* @@ so make it struct ddlbColl1 pointer type, not long */
struct ddlbColl1 * xxx;
Yeah.... thats what I was missing! The pointer type had to be the same type
of the array type!
Thanks Giovanni for your help!
--
Best regards
Robert
"Giovanni Dicanio" wrote:
There are several things that should be fixed in your code....
I've tried an attempt to do that, and I tried to "clean" the header and main
..c file.
I post the reviewed files here, with some comments of mine identified by @@
It compiles fine on Visual C++...
<code file="Main.h">
/*
* Main.h
*/
/* @@ Guard to avoid multiple inclusions */
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
struct ddlbColl1{
long LINK_ID;
int LIST_NUMBER;
};
/* @@ Separate structure type definition from structure instantiation */
/* @@ Use g_ prefix to identify global variables, this avoid bad naming
conflicts */
/* @@ This is defined in some .c file */
extern struct ddlbColl1 g_DDLB_COLL1[];
typedef struct ddListBox{
int x;
long y;
/* long *xxx; //<<< Pointer that holds the address of the DDLB_COLL1[]
array! */
/* @@ so make it struct ddlbColl1 pointer type, not long */
struct ddlbColl1 * xxx;
} DDLB; // Eventually Will be defined in a function in .c file.
/* @@ This is defined in some .c file */
extern DDLB * g_obj_DDLB; // Eventually Will be defined in a function in .c
file.
/* @@ Function prototypes */
DDLB* ULC_DDLB_config_ddlb (struct ddlbColl1 DDLB_COLL1[], int x, int y);
void ULC_DDLB_ddlb (DDLB *obj_DDLB);
#endif /* MAIN_H_INCLUDED */
</code>
<code file="Main.c">
/*
* Main.c
*/
#include "Main.h"
#include <stdlib.h>
/*
* Definitions of global variables
*/
struct ddlbColl1 g_DDLB_COLL1[] =
{
200, 4,
201, 8
};
DDLB * g_obj_DDLB = NULL; /* @@ will be assigned later */
/*
* Function Implementations
*/
DDLB* ULC_DDLB_config_ddlb
( struct ddlbColl1 DDLB_COLL1[], int x, int y)
{
g_obj_DDLB = NULL;
g_obj_DDLB = (DDLB*) malloc (sizeof (struct ddListBox));
g_obj_DDLB->x = x;
g_obj_DDLB->y = y;
g_obj_DDLB->xxx = DDLB_COLL1; //Assigning the address of the
DDLB_COLL1[] array!
return g_obj_DDLB;
}
void ULC_DDLB_ddlb ( DDLB *obj_DDLB )
{
// implementation file which uses the infos from DDLB_COLL1 array!
if(obj_DDLB->xxx[0].LINK_ID == 200)
{ //then do code!
}
}
int main()
{
g_obj_DDLB = ULC_DDLB_config_ddlb( g_DDLB_COLL1, 10,5 );
ULC_DDLB_ddlb (g_obj_DDLB);
return 0;
}
</code>
HTH,
Giovanni
-----------------------------------------
"Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> ha scritto nel messaggio
news:C38C4AA4-CC5F-413F-A72A-1B7E0363C0C1@xxxxxxxxxxxxxxxx
Hello Barry,
I just tried to access data from the DDLB_COLL1 array and got an error!
here is the code I am using:
====================================Main Header
struct ddlbColl1{
long LINK_ID;
int LIST_NUMBER;
} DDLB_COLL1[] = // Eventually Will be defined in a function in .c
file.
{
200, 4,
201, 8};
typedef struct ddListBox{
int x;
long y;
long *xxx; //<<< Pointer that holds the address of the DDLB_COLL1[]
array!
} DDLB; // Eventually Will be defined in a function in .c file.
DDLB *obj_DDLB; // Eventually Will be defined in a function in .c file.
DDLB* ULC_DDLB_config_ddlb (struct ddlbColl1 DDLB_COLL1[], int x, int y);
void ULC_DDLB_ddlb ( DDLB *obj_DDLB);
=============================================
=========================================Main
int main()
{
obj_DDLB = ULC_DDLB_config_ddlb ( DDLB_COLL1, 10,5);
ULC_DDLB_ddlb (obj_DDLB);
return 0;
}
DDLB* ULC_DDLB_config_ddlb
( struct ddlbColl1 DDLB_COLL1[], int x, int y)
{
obj_DDLB = NULL;
obj_DDLB = (DDLB*) malloc (sizeof (struct ddListBox));
obj_DDLB->x = x;
obj_DDLB->y = y;
obj_DDLB->xxx = DDLB_COLL1; //Assigning the address of the DDLB_COLL1[]
array!
return obj_DDLB;
}
void ULC_DDLB_ddlb ( DDLB *obj_DDLB)
{
// implementation file which uses the infos from DDLB_COLL1 array!
if(obj_DDLB->xxx[0].LINK_ID == 200)
{ //then do code!}
}
==========================================
I am trying to access the LINK_ID data from the first structure in the
DDLB_COLL1 array! This doesn't reference 200? cause I am getting a
"Expecting
a structure/union" error????
long x;
x=obj_DDLB->xxx[0].LINK_ID
However I tried
long x;
x = obj_DDLB[0].LINK_ID;
It does compile without errors, however I don't think this would return
200
? I mean how could it, I didn't bother to run it on my processor. obj_DDLB
is
a pointer of type DDLB which one of its members hold the address (xxx) to
the DDLB_COLL1 array???? Am interpreting this right? If so, then how can
it
return 200?
oooofff! confused!
Roberto
---------------------------------------------------------------------------------------------
"Robby" wrote:
Hello Barry,
I really must thankyou for your much valued feedback. Now I see the
reason
why we should only declare stuff in headers and only define stuff in .c
files. Thankyou for pointing it out with great scrutny.
Too often I have been so caught up in just getting the project to work at
all costs and without debuging errors which rendered me sort of absent
minded
about specific details that contribute in doing things the right way! And
for
this I appolagize to you and the newsgroups.
It makes sence, to only declare structs in headers and then define the
arrays, variables or pointers of that struct within functions that
require
the use of the that structure! In my example, the struct should be
declared
in the header and the array of that struct should be defined in the
function. Also, this seems to suit me fine since my MCU will use its
memory
more efficiently as opposed to leaving all my structures global.
However, what if you have a simple array like this:
int xxx[5] = {1, 3, 5, 7, 9};
In the header we would declare?
int xxx[5];
and in the .c file we define?
int xxx[0] = 1;
int xxx[0] = 3;
int xxx[0] = 5;
int xxx[0] = 7;
int xxx[0] = 9;
What is the right way? Its nice to just declare and define it in the
haeder
all in one line, but if you say its a nono! then is the above correct?
I re-wrote my example code the best I could so that it compiles.
However, for now there is too much to change to my code if I would define
the array of structures in a functions as opposed to defining it
globally.
Just to mention this, I did try to re-code my file using your
reccomendation
of defining the array of struct in a function of .c file, but another
function gave me an undefined identifier error in one of my other
functions
more precicely at a for loop:
for(i=0; i<DDLB_ITEM_LIST1; i++)
{ ... other code... }
where DDLB_ITEM_LIST1 is a global macro:
#define DDLB_ITEM_LIST1 (long) ((sizeof (DDLB_COLL1)/sizeof (struct
ddlbColl1)))
It will take me some time before I can modify my file to fully implement
the
declations and defines appropriately.
Any how the following compiles without errors!
====================================Main Header
struct ddlbColl1{
long LINK_ID;
int LIST_NUMBER;
} DDLB_COLL1[] = // Eventually Will be defined in a function in .c
file.
{
200, 4,
- References:
- Passing an array of structuresfrom a pointer?
- From: Robby
- Re: Passing an array of structuresfrom a pointer?
- From: Barry Schwarz
- Re: Passing an array of structuresfrom a pointer?
- From: Robby
- Re: Passing an array of structuresfrom a pointer?
- From: Robby
- Re: Passing an array of structuresfrom a pointer?
- From: Giovanni Dicanio
- Passing an array of structuresfrom a pointer?
- Prev by Date: Re: __stdcall compile error?
- Next by Date: Re: Passing an array of structuresfrom a pointer?
- Previous by thread: Re: Passing an array of structuresfrom a pointer?
- Next by thread: Re: Passing an array of structuresfrom a pointer?
- Index(es):
Relevant Pages
|