Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help on pointers (http://www.go4expert.com/forums/help-on-pointers-t23766/)

debabrata 4Nov2010 11:46

Help on pointers
 
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.:)

xpi0t0s 5Nov2010 16:28

Re: Help on pointers
 
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 10Nov2010 15:10

Re: Help on pointers
 
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

xpi0t0s 10Nov2010 15:26

Re: Help on pointers
 
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.

debabrata 10Nov2010 16:28

Re: Help on pointers
 
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.

xpi0t0s 10Nov2010 18:57

Re: Help on pointers
 
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;

debabrata 11Nov2010 15:06

Re: Help on pointers
 
Does '.' operator works ,when we are using addresses or we have to use the '->' operator, which one is better?

xpi0t0s 11Nov2010 15:54

Re: Help on pointers
 
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.


All times are GMT +5.5. The time now is 23:19.