Novice: Pointing to arrays in array of structures 
Author Message
 Novice: Pointing to arrays in array of structures

Hi,

Oops, I think I wrote this to a wrong forum... I'll try igain.

I have an array of structures and whish to point to an array in each
struct. I get the right results (I think, anyway), but I have not seen
this done anywhere else- hence I am a bit nervous about it. Is the
follow  example illegal/dangerous/stupid?

#include headers

struct particle{
  float x[3];

Quote:
};

typedef struct particle PA;

void init(PA *ptr, int N, int M){
  int n,m;

  srand(129);
  for (n=0; n<N; n++){
    for (m=0; m<M; m++)
      (*(ptr+n)).x[m]=rand()/(RAND_MAX+1.0);
  }

Quote:
}

int main(void){
  int n,m;

  PA sys[2];
  init(&sys[0],2,3);

  return 0;

Quote:
}

Sent via Deja.com http://www.*-*-*.com/
Before you buy.


Sun, 25 May 2003 03:00:00 GMT  
 Novice: Pointing to arrays in array of structures

Quote:

>Hi,

>Oops, I think I wrote this to a wrong forum... I'll try igain.

>I have an array of structures and whish to point to an array in each
>struct. I get the right results (I think, anyway), but I have not seen
>this done anywhere else- hence I am a bit nervous about it. Is the
>follow  example illegal/dangerous/stupid?

>#include headers

I hope the right ones :-)

Quote:

>struct particle{
>  float x[3];
>};

>typedef struct particle PA;

>void init(PA *ptr, int N, int M){
>  int n,m;

>  srand(129);

This will always produce the same sequence of numbers.

Quote:
>  for (n=0; n<N; n++){
>    for (m=0; m<M; m++)
>      (*(ptr+n)).x[m]=rand()/(RAND_MAX+1.0);

Doesn't look illegal. But the division is done in integer arithmetics,
so if you want your floats to hold other values than 0 and 1 cast one
of your operands to float. Note you can also use ptr[n].x[m] = ...

Quote:
>  }

>}

>int main(void){
>  int n,m;

You don't use them.

Quote:

>  PA sys[2];
>  init(&sys[0],2,3);

Not wrong but could be made less error-prone:

init (sys, sizeof sys / sizeof sys[0], sizeof sys[0].x / sizeof sys[0].x[0]);

Quote:

>  return 0;
>}

Good luck,
Stefan


Sun, 25 May 2003 03:00:00 GMT  
 Novice: Pointing to arrays in array of structures

Quote:

> I have an array of structures and whish to point to an array in each
> struct. I get the right results (I think, anyway), but I have not seen
> this done anywhere else- hence I am a bit nervous about it. Is the
> follow  example illegal/dangerous/stupid?

Not really.

Quote:
> #include headers

I'll assume that you've included all necessary ones, shan't I? Decent
compilers set to a decent warning level will warn you if you haven't,
anyway.

Quote:
> struct particle{
>   float x[3];
> };

It is better for the maintainability of your program if you use a
#defined constant instead of this bare 3. That way, you can use it again
in the call to init below, and you'll never make the mistake of typo-ing
4 instead; and if you ever want to change the size of the data, you can
do so by changing only the #define.

Quote:
> typedef struct particle PA;

> void init(PA *ptr, int N, int M){
>   int n,m;

>   srand(129);
>   for (n=0; n<N; n++){
>     for (m=0; m<M; m++)
>       (*(ptr+n)).x[m]=rand()/(RAND_MAX+1.0);

You can even use ptr[n].x[m]. You don't have to have an explicit array
if you use []; all you need is one pointer (possibly a decayed array,
which is the canonical case) to an object and one integer (not
necessarily int).

Quote:
>   }
> }

> int main(void){
>   int n,m;

>   PA sys[2];
>   init(&sys[0],2,3);

I'd use #defined constants here, too. From this snippet it is obvious
that you pass 2 and 3 to init because those are the sizes of your
arrays, but what if the program gets to be hundreds of lines long? Are
all 3s equal? If you use a #define, those become obvious.

Also, &sys[0] is completely equivalent to sys in all but a few contexts;
function calls are not one of those contexts, so you can replace &sys[0]
with sys here. (An example of where you couldn't: as an argument to
sizeof. sizeof sys == 2*sizeof(PA); sizeof &sys[0] == sizeof(PA *).)

Quote:
>   return 0;
> }

All in all, I think your program is correct, but could be made slightly
clearer. You jump through loops you don't have to jump through.

Richard



Sun, 25 May 2003 03:00:00 GMT  
 Novice: Pointing to arrays in array of structures

Quote:


> >      (*(ptr+n)).x[m]=rand()/(RAND_MAX+1.0);

> Doesn't look illegal. But the division is done in integer arithmetics,

It shouldn't; 1.0 is fp, so RAND_MAX + 1.0 is fp, so the division is fp
as well.

Richard



Sun, 25 May 2003 03:00:00 GMT  
 Novice: Pointing to arrays in array of structures

Quote:



>> >      (*(ptr+n)).x[m]=rand()/(RAND_MAX+1.0);

>> Doesn't look illegal. But the division is done in integer arithmetics,

>It shouldn't; 1.0 is fp, so RAND_MAX + 1.0 is fp, so the division is fp
>as well.

Sorry, you're right. I overlooked the .0 :-(

Stefan



Sun, 25 May 2003 03:00:00 GMT  
 Novice: Pointing to arrays in array of structures
Stefan and Richard

Thank you for your quick and very clear answers.

:-) Jesper

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 25 May 2003 03:00:00 GMT  
 Novice: Pointing to arrays in array of structures

Quote:

> I have an array of structures and whish to point to an array in each
> struct. I get the right results (I think, anyway), but I have not seen
> this done anywhere else- hence I am a bit nervous about it. Is the
> follow  example illegal/dangerous/stupid?

well no, it's ok to "point to an array", but it better to point to the
first element of an array to achieve that result. Unfortunatly (or
maybe it's what you intended) you arn't pointing at an array or the
first element of an array but you have embedded an array in a
structure. This too is fine but not quite the same thing.

Quote:
> struct particle{
>   float x[3];
> };

this struct contains an array it doesn't point to one.

Quote:
> typedef struct particle PA;

> void init(PA *ptr, int N, int M){
>   int n,m;

>   srand(129);
>   for (n=0; n<N; n++){
>     for (m=0; m<M; m++)
>       (*(ptr+n)).x[m]=rand()/(RAND_MAX+1.0);
>   }

> }

> int main(void){
>   int n,m;

>   PA sys[2];
>   init(&sys[0],2,3);

>   return 0;
> }

Sent via Deja.com http://www.deja.com/
Before you buy.


Mon, 26 May 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Novice: Pointing to arrays in array of structures

2. An array in a structure Or an Array Of Structures

3. Array of Structures in a Structure.

4. structure of array of structure : help needed

5. char array (array of array...) question

6. arrays of pointers - NOVICE QUESTION!( Be forwarned ! )

7. Help Novice on large arrays in DOS

8. Help Novice: char array

9. Novice Q: An array of structers with pointers..??

10. Novice:Arrays + Borland C++

11. novice c programmer's question regarding array

12. Point array in managed C++?

 

 
Powered by phpBB® Forum Software