Saturday, June 30, 2007

Typecasting with Pointers!

What is tpecasting?
Typecasting, as its name suggests, is changing a variable's type into another.

How do we do it in C?
In C, typecasting can be automatic as well as manual. Automatic in a way that a variable of a narrower type can be converted into one of a wider one just by mere assignment .
Ex:
int a;
double q,m;
a=q;//a is converted into one of double tpes directly by assignment.
m=sqrt(a);//sqrt can take only doubles as auguments.

It pays to note that q=a, does not do the opposite, because of the earlier specifie reason.(Narrower to wider)

Now, we move on to an interesting question.
How do we change the type of pointers.

Ex: How do we convert an integer ptr to a double ptr.

My guess is that it can be done using the malloc() function.

int *p;
p=(double *) malloc(sizeof(double));

now p will point to a double. Malloc takes the sizeof a data type as an augument and returns a void ptr which can be casted using the "(type)" feature...

Having written this much, i have a doubt if the foll code will do the same operation...Pls comment if i am wrong...

int *p,i;
double q;
p=&i;
i=q;

now is p now a pointer to a double?


May be not, because my conjecture says wen u declare a pointer of a type the num of bytes reqd for the type is allocted and remains const.

Wednesday, June 27, 2007

Even more tuff assignments

These series of assignments tell you how tough pointers can become if not understood properly.....

int **x; //this is a pointer to a pointer that ponts to an onteger..lets imagine..we have a box from where there is an arrow to an other box.Now an arrow from that arrow to a box that stores integer..Thats how it is...seems difficult....then better stay here

int (*x)[5]; //this is as discussed in the previuos posts.....pointer to an integer array of 5 elements..so there is a box now with space for 5 elements and now x points to that box...
int *x[5]..this is similar to int* x[5];

The second asignment makes life imple...thats how the compiler groups it without a parenthesis...so it is array of 5 integer pointers....there is a box now and each element inside the box points to an integer.simple...

int *compute(); ///what can this be....the * goes with the int and compute a function that returns a pointer to integer

int (*compute)(); //compute is a pointer to a function returning int.these are simple ones...but it can get tougher now.....

char *(*(compute()))(); // as we can see here ther is a function returning pointer to character ..lets break it up... char* (*(compute())) () ;so lets remove the outer function..what remains

(*(compute())) //here we can see an inner function compute .There is one extra set of parenthesis that can be removed....so it is now (*compute()).this is actually a function that returns a pointer now.what is that pointer.it is actually a function that returna pointer to char

Therefore compute if a function that returns a pointer to a function returning a pointer to char //so the definition is aline and a half...now even tougher one..take a breath


int (*(*(x[5])))[5]; //as we can see no functions here all arrays...not easy..wait..

so we have as stated (*(x[5])) points to that box of 5 elements....lets break it up even further.....
applying the & operator we get that x[5] stores the starting address of the box.

so x is an array with 5 elemsts where each element stores the address of integer arrays of length 5 each.

Such are the pointer assignments in the C programming language ..there can be even more tuff assignments though such stuffs are rarely used while programing..ppl give such assignments as commenst with or without solutions

Thanks,
Prasanna

so constructing it back......

Multi dimensional arrays and pointers to pointers

C does not really support Multi dimensional arrys.what we code as Multi dimensional arrays are arrays of arrays.Simple to tell but thers more to it

int x[5][6]; //this is an 5 array of 6 element array each
int *x[6]; //this is a pointer to the 6 element array

Both are pointers here.x at both places points to x[0][0].x[2][3] at both places mean the same.But the difference is that the first assignment allocates memory for 5*6 30 integer elements in a contuguous manner..But the second assignment as we know just creates a pointer that ponts to nothing.It has to later point to something else and as already stated the pointer x in first is static and thee later pointer is not it can point to any memory location later provided that is also an integer array


Now more in to the depth

int cal[12][31]; //suppose a declaration and values have been assigned
int (*month)[31]; // month is a pointer to a 31 element integer array...fine??

now we can say

month = cal; // here month points to the first month out of the 12 months.diagramatically its like this.asume that there are 12 boxes each of lenth 31
cal points to the first box always.this is fixed since its an array and its static.

month also points to the same box now

cal+1 , cal+2 , manoth+1 and month +2 etc will point o subsequent boxes

cal[1] and cal[2] what about these..lets aply same rule...cal[2] = *(cal + 2) as said

so cal[2] will point to the first element in the thirs box....simple..since here as already said * operator will dereference the object that we point to.....

so cal[4][7] = *(cal[4] + 7) = *(*(cal + 4) + 7)

we get this by applying the referencing operators repeatedly

Some dereferencong now.......

&cal[5] //this points to as we know the 6th Box.since cal[5] points to the first element of 6th box and the & operator will get the address of that box

&(*month)[31] // Month is a pointer to an integer array that is a box...so *month will point to the firts element of that array...(*month)[31] is the value of the last element as array subscript points to the value......

&(*month)[31] gets the pointer to the last element of the month array box ultimately

Thanks,
Prasanna

some intriguing assignments

Lets go thru a series of assignments here using pointers and array and see how far can we crack it

int *p; //simplke one - p is a pointer to an integer variable but does not point to anything now

int i; //straight

*p = i; //p now points to i that is the address of i is the value of p

p = &i; //this tells the story more clearly

int x[5]; //x is an array we know but it points to the first element.That is x is a ponter to x[0].so *x is the value of x[0].

x+1 // his points to X[1].so *(x+1) is the value x[1] and similarly....infact the array indices starts from 0 because the implementation is like this the n+1th elemsnt is *(x + n) ..this is x[n]..that is how it is computed

So arrays are implicit pointers.......now

p++; //will move the pointer p to the next integer variable in the memeory that is the pointer will be shifted by either 2 or 4 bytes depending upon the compiler

x++; //error this cannot happen arrays are static pointers implicitly

The * and the &

In pointers the 2 operators * and & are important.* is the dereferencing operator that is ,it always gets the object that the pointer is pointing to and the & is the referncing operatot it tries to get the address of a variable.Simple ones

Thanks,
Prasanna

what are Pointers in "C"

Here lets go about learning and arguing about pointers and arrays in the C Programming language

Pointer is nothing but a type that points to a particular data type
Suppose i say int *p;
Here p is a pointer to a int datatype and int alone
So p will store the address of the memory location of a integer variable and any integer variable(as of now our p stores nothing).
Diagramatically p will have an arrow pointing to an integer variable.Simle rite.yes things are cool till now

Lets go on with some assignments :
int *p;(we now know what happens here)
int i = 25;(we know what happens here as well - if not this is not the place for you......oh!!i meant u better learn the basics and come)


p = &i; // this rtaher creates an arrow from i and puts it with p...programatically & gets the address of that variable.so now p has the address of the integer variable i.

Thus things should be fine now..thats it with the basics of what is pointer.see the other osts for more advanced stories on pointers

Thanks,
Prasanna

Hey ppl - Pointers for you

Pointers in any language might be a little confusing.Not any more.This blog is there to explain u in detail the concepts of pointers and arrays and to discuss issues regarding anything and everything related to pointers.So lets get started up

With cheers,
Prasanna