Re: Passing an array of structuresfrom a pointer?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hello Barry,

Oh boy! Am I in the dog house now! ain't I :)

Okay, for the code sample, I have provided another sample in the post before
Giovanni's post! I would of hoped that you would of looked at that one firts
instead of this one since some of the typos were rectifier. But that's okay,
you brought up very good points on this one.

I don't always compile my code before presenting it, because it takes me so
much time to download my project onto the MCU. So I sometimes try to write a
very watered down version of my problem without compiling it. I know this
makes it that much harder for you guys to evaluate what I am doing because of
the typo's and my errors.

This attempts to define a 0 element array which is illegal. If you
were looking for an assignment statement, it would be
xxx[0] = 3;
without the int.

You are right, this is what I meant!

int xxx[5];
which defines the variable and later in your code you would have
statements to assign values to the elements
xxx[0] = 1;
xxx[1] = 3;
xxx[2] = 5;
xxx[3] = 7;
xxx[4] = 9;


obj_DDLB->x = x;
obj_DDLB->y = y;
obj_DDLB->xxx = DDLB_COLL1;

This is another constraint violation. xxx is a long*. DDLB_COLL1 is
an array of struct. The array expression will be converted to a
pointer to struct. You cannot assign a pointer to struct to a long*
without a cast (and you wouldn't want to even with a cast).

My pointer type was adjusted to a "struct ddlbColl1"


the "ddlbColl1" structure will have an array of itself declared as
"DDLB_COLL1". In my example below I only declared one (1) such strucure
called "ddlbColl1", but someday I could end up having 2, 3 or even 5 of them!

Then you REALLY don't want to use global pointers.

Yes, I am working on this, I would like to open a new post to discuss
programming layouts concerning all the global stuff we have discussed in this
post. I hope you will be part of that one. Always re-adjusting my code this
way is getting harder and harder as my project gets larger!

This post is getting too long and confusing, so I won't bother much longer
with it.

Thanks all for your replies, and help!

--
Best regards
Robert


"Barry Schwarz" wrote:

On Mon, 29 Sep 2008 10:27:01 -0700, Robby
<Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> 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];

That is a definition. A declaration would be
extern int xxx[5];


and in the .c file we define?
int xxx[0] = 1;

This attempts to define a 0 element array which is illegal. If you
were looking for an assignment statement, it would be
xxx[0] = 3;
without the int.

int xxx[0] = 3;
int xxx[0] = 5;
int xxx[0] = 7;
int xxx[0] = 9;

And these would have different subscripts.

But this wouldn't work since xxx has not been defined. In your .c
file you would have either
int xxx[5] = {1, 3, 5, 7, 9};
which would define and initialize the variable declared in the header
or
int xxx[5];
which defines the variable and later in your code you would have
statements to assign values to the elements
xxx[0] = 1;
xxx[1] = 3;
xxx[2] = 5;
xxx[3] = 7;
xxx[4] = 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?

An object defined in a header that is included in two translation
units will lead to duplicate definition errors during the link phase.


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

Then it obviously didn't compile.

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)))

Is the macro in scope at the time it is used.

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!

It doesn't compile without errors. It doesn't even come close.


====================================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;
} 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);

ULC_DDLB_config_ddlb is a function that takes three arguments.

void ULC_DDLB_ddlb ( DDLB *obj_DDLB);
=============================================

=========================================Main

int main()
{

obj_DDLB = ULC_DDLB_config_ddlb (e_CREATE, e_I500, DDLB_COLL1,
pMN_DC->CURR_MENU_ITEM);
ULC_DDLB_ddlb (obj_DDLB);

Here you call the function with four arguments. I suggest you look
again for the mandatory diagnostic.

UCL_DDLB_config_ddlb set the global pointer obj_DDLB (see note below).
It also returned the value of the pointer. You then assign that same
value to the same pointer. In this little section of code you assign
a value to obj_DDLB three times, one useless and a second value twice.
WHY?


return 0;
}


DDLB* ULC_DDLB_config_ddlb
( struct ddlbColl1 DDLB_COLL1[], int x, int y)
{
obj_DDLB = NULL;

Why assign a value to this pointer when you change it in the next
statement?

obj_DDLB = (DDLB*) malloc (sizeof (struct ddListBox));

Here you set the global pointer obj_DDLB.


obj_DDLB->x = x;
obj_DDLB->y = y;
obj_DDLB->xxx = DDLB_COLL1;

This is another constraint violation. xxx is a long*. DDLB_COLL1 is
an array of struct. The array expression will be converted to a
pointer to struct. You cannot assign a pointer to struct to a long*
without a cast (and you wouldn't want to even with a cast).


return obj_DDLB;
}

void ULC_DDLB_ddlb ( DDLB *obj_DDLB)
{
// implementation file which uses the infos from DDLB_COLL1 array!

Why are you passing the pointer to this function when the pointer is
already global?


}

==========================================




the "ddlbColl1" structure will have an array of itself declared as
"DDLB_COLL1". In my example below I only declared one (1) such strucure
called "ddlbColl1", but someday I could end up having 2, 3 or even 5 of them!

Then you REALLY don't want to use global pointers.


I'm sorry but this makes no sense. A structure may not contain
itself. The significance of the case change is not apparent.

I appolagize Barry!

"I will declare a struct called "ddlbColl1" and then define an array of
"ddlbColl1" structures...."

Do not define objects in your header. However, do declare obj_DDLB as
an external pointer with
extern DDLB *obj_DDLB;
so that when you define it in a source file other source files will
have access to it. (Ignoring the issue of whether a global pointer is
a good idea or not.)

I will have to work on this!

{
200, 4,
201, 8};

DDLB* ULC_DDLB_config_ddlb

Surely this function definition is not in a header but in a separate
source file.

Yes it is, I just put it there so I can show my sample code in one page!

Since you don't need obj_DDLB to be global, you should define it here.
You will end up with three objects with that name, one each in main,
ULC_DDLB_config_ddlb, and ULC_DDLB_ddlb. Since they all have
different scope, there is no problem, However, to avoid confusion, you
might want to give the object in each function a slightly different
name

For now, I prefer to just keep the obj_DDLB object global. When I
re-structure the code according to your reccomendations, I will define it as
you say. However when I do this I will most probably use a new post since I
will undoubfully have problems!

The above code has been adapted to the full blown project and now compiles
without errors. If I have problems accessing the informations in the
DDLB_COLL1[] array while in the "void ULC_DDLB_ddlb ( DDLB *obj_DDLB)"
function, I will certainly get back to the newsgroups, under a new post
though!

Barry, I thankyou for your sincere help!

--
Best regards
Roberto

--
Remove del for email

.



Relevant Pages

  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... In the header we would declare? ...
    (microsoft.public.vc.language)
  • Re: Looking for a more elegant way to do memory offsets
    ... The mallocfunction returns memory that is "suitably aligned" ... >which in it contains the pointer to the first element in the linked ... and here a "struct dem_mem" does not contain a pointer to another ... This is not a linked-list access, but rather an array access: ...
    (comp.lang.c)
  • Re: Malloc Query
    ... the arena is an array even though the program using it does not care. ... since the pointer will be of the expected type. ... _every_ struct such that it can be the element type of an array? ... the alignment requirement, to allow for arrays... ...
    (comp.lang.c)
  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... It also returned the value of the pointer. ...
    (microsoft.public.vc.language)
  • Re: Passing an array of structuresfrom a pointer?
    ... I just tried to access data from the DDLB_COLL1 array and got an error! ... int LIST_NUMBER; ... typedef struct ddListBox{ ... to only declare structs in headers and then define the ...
    (microsoft.public.vc.language)