Help on pointers

debabrata's Avatar, Join Date: Sep 2010
Light Poster
Hi, being an student of M.C.A 1st sem ,I personally request all the members to help me on the said tropic.
THANK YOU ALL.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Code:
int x=5;
int *ptr_to_x = &x;
printf("x is %d\n", *ptr_to_x);
Does that help? If not, what are your specific questions?
debabrata like this
0
debabrata's Avatar, Join Date: Sep 2010
Light Poster
Thanks,xpi0t0s.
Actually I need some help in accessing structures using pointers.
For example:- write a program to access structures , by creating a set of structures using Dynamic Memory Allocation
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Ok, so where exactly are you stuck? Do you know how to define a structure? To allocate memory dynamically? Make a start and see how far you get; you might surprise yourself and be able to do it all on your own. The chances are that you've already got the information you need.
0
debabrata's Avatar, Join Date: Sep 2010
Light Poster
Ya I know how to define a structure & allocate memory dynamically, but I can't understand how to access those structures.

For ex:-

struct sname *p;
p=(struct sname *)malloc(n*(sizeof(struct sname)));

now I have allocated memory for 'n' structures. Now kindly help me in using them.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
It's very simple. Suppose you had an automatic version:
struct sname M;
and let's say that struct sname contains an integer j

you would access this as M.j, and assign to it with, e.g., M.j=10. I assume you already know this.

With the code you've given above, array semantics will work, so p[5] is the 6th element of p, which will point to p+5*sizeof(struct sname) (in bytes). The compiler knows that p is a pointer to type struct sname so operations like p++ will use the size of the structure, so if p=1000 and sizeof(struct sname) is 10, then p++ will set p to 1010, not to 1001.

[[ So this is why I added "(in bytes)", because if you actually do p+5*sizeof(struct sname) then this will evaluate to 1000+(5*10)*10=1500. ]]

So to access the structure members just replace M with p[5]. Assign a value with p[5].j=10;, just as in the automatic version. Copy this to another variable with, e.g. int x=p[5].j;
0
debabrata's Avatar, Join Date: Sep 2010
Light Poster
Does '.' operator works ,when we are using addresses or we have to use the '->' operator, which one is better?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
It's not a case of one being better than the other; these are different syntaxes for different situations. . accesses the members of an automatic/static structure, and -> accesses members via a pointer. So:
struct sname A, *B;

defines A as a struct sname and B as a pointer to a struct sname, to access member j in each, you need A.j or B->j. (Note however that B doesn't point to anything at present, so B->j is going to give you undefined behaviour.)

A->j won't work because A isn't a pointer, and B.j won't work because B _is_ a pointer. Both these will throw compiler errors.

B->j is effectively a shortcut of, and neater than, (*B).j.