• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

pointer/struct question.

Hello group,



I was looking at the following code and one thing I don't know how to explain to myself is why the object derived frm the structure works as a pointer?
because the ptrs is not declared as a pointer but as an instance of the struct.

I haven't done C programming for a long time so I'm doing a fast review.

Thanks for your help.
ak



      #include <stdio.h>
      main()   /* Illustrating structures containing pointers */
      {
            struct  int_pointers {  int  *ptr1, *ptr2;  };
            struct int_pointers  ptrs;
            int  i1 = 154, i2;

            ptrs.ptr1 =  &i1;
            ptrs.ptr2 =  &i2;
            *ptrs.ptr2 =  -97;
            printf("i1 = %d, *ptrs.ptr1 = %d\n", i1, *ptrs.ptr1);
            printf("i2 = %d, *ptrs.ptr2 = %d\n", i2, *ptrs.ptr2);
      }
0
akohan
Asked:
akohan
2 Solutions
 
Anthony2000Commented:
*ptrs.ptr2 =  -97;

the * is not making the ptrs object look like a pointer, it is dereferencing the member ptr2 of the struct int_pointers.


Do you understand?
0
 
ozoCommented:
*ptrs.ptr1 is *(ptrs.ptr1) you declared the ptr2 member of the int_pointers stricture to be a pointer to int
 int  *ptr1, *ptr2;
0
 
akohanAuthor Commented:

oh I see! so the ptrs is not the pointer but ptr1 and ptr2 are the pointers and ptrs is used just to access thsoe members. Right?

Thanks,
ak
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ozoCommented:
ptrs is a struct, as you declared
prts.ptr1 is a member of the struct
0
 
akohanAuthor Commented:

Now , I understand it. I was thinking that * could only be behind a variable defined as pointer. Now, I know that it could be behind a struct as well.

0
 
ozoCommented:
It can be in front of prts.ptr1 which is an expression defined as a pointer
0
 
akohanAuthor Commented:


Thank you so much for your response and time. Just as the last question do you know by chance a very good/free online C/C++ tutorial ?



Regards,
ak
0
 
Infinity08Commented:
>> I was thinking that * could only be behind a variable defined as pointer. Now, I know that it could be behind a struct as well.

Jusr realize that ptrs.ptr2 IS a pointer. Look at it as one identifier (the ptr2 member of the ptrs struct) that has an int* type, and can be dereferenced.


>> Just as the last question do you know by chance a very good/free online C/C++ tutorial ?

I like this one :

        http://www.cplusplus.com/doc/tutorial/
0
 
akohanAuthor Commented:


Thank you so much.

Regards,
ak
0
 
akohanAuthor Commented:


Thanks Ozo.

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now