passing adress of second element in array of structs 
Author Message
 passing adress of second element in array of structs

Hi,
I have a question concerning the passing of the adress of a second
element in an array of structs.

The following code works as I would expect, i.e., it outputs

1 2.2
1 2.2

=========BEGIN OF EXAMPLE============

#include <stdio.h>
#define NUM_STRUCTS 2
void dosomething( struct Some *s );

struct Some {
        int a;
        double b;

Quote:
} somestruct[NUM_STRUCTS];

void dosomething( struct Some *s )
{
        s->a=1;
        s->b=2.2;

Quote:
}

int main(void)
{
        unsigned char message[10];
        int i;

        for (i=0;i<NUM_STRUCTS;i++) {
                dosomething(&somestruct[i]);
                printf("%d %g\n", somestruct[i].a, somestruct[i].b);
        }

        return 0;

Quote:
}

===============END OF EXAMPLE============

Just to be sure, did I use a correct way of referencing the second
element in the array of struct in the line
"dosomething(&somestruct[i]);"?

Regards,
Jasper



Sat, 25 Jun 2005 00:08:05 GMT  
 passing adress of second element in array of structs

Quote:
> Hi,
> I have a question concerning the passing of the adress of a second
> element in an array of structs.
> The following code works as I would expect, i.e., it outputs
> 1 2.2
> 1 2.2
> =========BEGIN OF EXAMPLE============
> #include <stdio.h>
> #define NUM_STRUCTS 2
> void dosomething( struct Some *s );
> struct Some {
>    int a;
>    double b;
> } somestruct[NUM_STRUCTS];
> void dosomething( struct Some *s )
> {
>    s->a=1;
>    s->b=2.2;
> }
> int main(void)
> {
>    unsigned char message[10];

You never use this variable for anything at all.

Quote:
>    int i;

>    for (i=0;i<NUM_STRUCTS;i++) {
>            dosomething(&somestruct[i]);
>            printf("%d %g\n", somestruct[i].a, somestruct[i].b);
>    }

>    return 0;
> }
> ===============END OF EXAMPLE============
> Just to be sure, did I use a correct way of referencing the second
> element in the array of struct in the line
> "dosomething(&somestruct[i]);"?

Yes. You might also have simply used dosomething(somestruct+i).

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Life without ostriches is like coffee with milk."
   - Mika P. Nieminen



Sat, 25 Jun 2005 00:26:21 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. qsort on array of struct element

2. ? grouping elements of an array of structs

3. Two dimensional array with struct elements - trouble

4. Passing an array with unknown number of elements to a function

5. passing array elements to function ch8ex21-22 kelley/pohl

6. sizeof (struct) ! = sizeof struct elements

7. Acces the 11th element of a 10 element array

8. making pointers to structure elements using array elements

9. checkout compilers (Was: Acces the 11th element of a 10 element array)

10. array of Struct passing into a function

11. Array of struct pass to function

12. How to pass a struct to an unsigned char array

 

 
Powered by phpBB® Forum Software