pointer and nested structure.

M

manish sahu

#include<stdio.h>
#include<conio.h>
int main()
{
struct phone
{
long int city_code;
long int ph_no;
}ph;

struct person
{
char name[15];
char address[20];
struct phone ph;
};
struct person p={"abcdefg","swsw",12345,23432340};
struct person *ptr;
ptr=&p;


printf("\n %s %s %ld %ld",(*ptr).name,(*ptr).address,
(*ptr).ph.city_code,(*ptr).ph.ph_no);
printf("\n %s %s %ld %ld ",ptr->name,ptr->address,ptr-
ph.city_code,ptr->ph.ph_no);
}

This is my program here i am accessing the
struct person with a structure pointer ptr
and for accessing the members of phone structure i am using ph
variable of phone structure type(Defined inside struct person)
What I have to do is that
instead of taking variable ph of type struct phone
i want to use a phone structure pointer.
like
struct phone *p1;
and after that i want to access the members of struct person and
member of struct phone with a struct person pointer
struct person *ptr.
Plz tell me HOw to access
ThankYou
 
U

Ulrich Eckhardt

manish said:
#include<stdio.h>
#include<conio.h>

This is not a standard header and it is not portable.
int main()
{
struct phone
{
long int city_code;
long int ph_no;
}ph;

struct person
{
char name[15];
char address[20];
struct phone ph;
};
struct person p={"abcdefg","swsw",12345,23432340};
struct person *ptr;
ptr=&p;

There is no reason to use a pointer here, except for learning how to use
pointers.
printf("\n %s %s %ld %ld",(*ptr).name,(*ptr).address,
(*ptr).ph.city_code,(*ptr).ph.ph_no);

The following two are synonymous

(*x).y
x->y

Generally, the first involves three different steps for evaluation while the
second uses just one. For the computer that doesn't matter, but most humans
will actually prefer the second form:
printf("\n %s %s %ld %ld ",ptr->name,ptr->address,
ptr->ph.city_code,ptr->ph.ph_no);


What I have to do is that instead of taking variable ph of
type struct phone i want to use a phone structure pointer.
like
struct phone *p1;
and after that i want to access the members of struct person and
member of struct phone with a struct person pointer
struct person *ptr.

One thing up front: You have two 'ph'! One is the instance of struct phone
declared in main(), the other is the one inside the instance of struct
person. The former is completely unused, I hope you are not confusing those
two!

Now, in order to make 'p1' point to the member of 'p', you need to take its
address:

p1 = &p.ph;

Alternatively, you can of course use these, too:

p1 = &ptr->ph;
p1 = &(*ptr).ph;

Again, I don't see a reason for the third syntax.

Uli
 
B

Barry Schwarz

#include<stdio.h>
#include<conio.h>
int main()
{
struct phone
{
long int city_code;
long int ph_no;
}ph;

struct person
{
char name[15];
char address[20];
struct phone ph;

Your request below is not completely clear. On the off chance you
want your struct person to contain a pointer to struct phone rather
that the struct itself, you would change this to
struct phone* ph_ptr;
};
struct person p={"abcdefg","swsw",12345,23432340};

and you would move the last two initialization values from this
definition of p up to the definition of ph
struct person *ptr;
ptr=&p;

and you would assign the desired value to ph_ptr with
ptr->ph_ptr = &ph
or
p.ph_ptr = &ph
printf("\n %s %s %ld %ld",(*ptr).name,(*ptr).address,
(*ptr).ph.city_code,(*ptr).ph.ph_no);

and every ph. needs to become ph_ptr-> as in
(*ptr).ph.city_code becomes (*ptr).ph_ptr->city_code
printf("\n %s %s %ld %ld ",ptr->name,ptr->address,ptr-

here also.

While both the (*ptr). and ptr-> notations are functionally
equivalent, I think almost everyone here finds the latter easier to
read (and also easier to type).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top