Pointers to structures & Dereferencing 
Author Message
 Pointers to structures & Dereferencing

typedef struct {
        int a;
        int *p;

Quote:
} A;

typedef struct {
        int s;
        A *stptr;

Quote:
} B;

A *func1(int w);

main()
{

  A *Aptr;

  B *Bptr;

  Bptr = (B *)malloc(sizeof(B));

  /* Defining an array of 10 pointers to struct A */
  Bptr->stptr = (A *)malloc(10*sizeof(A *));  /* 1 */

  ...

Quote:
}

-------------

I am having trouble in accessing the "stptr" array inside struct B.
I was under impression that I can access the elements pointer to by
stptr as array elements but I have compilation problems. This is what I
do

  Bptr->stptr[0] = func1(w);

I did go thro the FAQ, but couldn't find anything similar to this. How
do I make this assignment?? What is it that I am doing wrong??

Thanks,

 -Sudhakar

--
Sudhakaran Ram          ext. 7035



Mon, 21 Jun 1999 03:00:00 GMT  
 Pointers to structures & Dereferencing


|> typedef struct {
|>   int a;
|>   int *p;
|> } A;
|>
|> typedef struct {
|>   int s;
|>   A *stptr;
|> } B;
|>
|> A *func1(int w);
|>
|> main()
|> {
|>   A *Aptr;
|>   B *Bptr;
|>   Bptr = (B *)malloc(sizeof(B));

You need to #include <stdlib.h> to ensure malloc() is properly declared.

|>   /* Defining an array of 10 pointers to struct A */
|>   Bptr->stptr = (A *)malloc(10*sizeof(A *));  /* 1 */

Think about what you've declared; within B, you've declared stptr to be of
type pointer-to-A.  But this isn't what you want.  You want an array of
pointer-to-A, not an array of A.  Rewrite B as

typedef struct {
    int s;
    A **stptr;  /* stptr is of type pointer-to-pointer-to-A */

Quote:
} B;

and then rewrite the above statement as

    Bptr->stptr = malloc(10 * sizeof (A *));

(Note that if <stdlib.h> has been properly #included, casting the return
value of malloc() is not necessary, and indeed, discouraged.)

|> I am having trouble in accessing the "stptr" array inside struct B.
|> I was under impression that I can access the elements pointer to by
|> stptr as array elements but I have compilation problems. This is what I
|> do
|>
|>   Bptr->stptr[0] = func1(w);

Again, your declaration of stptr was incorrect.  Originally, as a pointer-to
A, stptr[0] is of type A.  Once you add the extra level of indirection,
stptr[0] is of type (A *), which is what you want.

Once you make the above changes, you can do something like

    for (i = 0; i < 10; ++i) Bptr->stptr[i] = func1(i);

Regards,

--

EROS Data Center             -+-   Telephone:  (605) 594-6829
Sioux Falls, SD 57198        G|S   Fax:        (605) 594-6490
My opinions given here are not necessarily those of the USGS.



Mon, 21 Jun 1999 03:00:00 GMT  
 Pointers to structures & Dereferencing


Quote:
> typedef struct { int a; int *p; } A;
> typedef struct { int s; A *stptr; } B;
> ...
> Bptr->stptr = (A *)malloc(10*sizeof(A *));  /* 1 */
> ...
> I am having trouble in accessing the "stptr" array inside struct B.

Declare stptr to be:

        A       **stptr;

in your B structure. You need a pointer to a pointer if you want
to treat the value returned by malloc() as a vector of pointers to
A structures. Then change your malloc() line to read:

        Bptr->stptr = (A **)malloc(10 * sizeof(A *));

Maybe,
R.



Mon, 21 Jun 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. dereferencing pointer to a structure containing a pointer to int

2. Structure Pointer Dereferencing

3. Structure Pointer Dereferencing

4. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

5. pointers&&structures

6. functions, structures & pointers

7. Pointer to Structures & bsearch

8. structures, arrays & pointers

9. Dereferencing pointer to incomplete type

10. Problem with: dereferencing pointer to incomplete type

11. dereferencing NULL pointers in AIX

12. Dereferencing pointers...?

 

 
Powered by phpBB® Forum Software