i have two questions right now in my mind :

question - 1 :

please have a look at this code :

void function (int recd)
{
   printf ("%d", recd);
}
void main()
{
  int p=10;
  function(p);
}

here...when p is passed to function, the thing that actually takes place is :
a new variable recd is created and value of p is copied to recd (i mean a copy of p is created...consuming another 2 bytes)...

now please check the following code :

void function (int *recd)
{
   printf ("%d", *recd);
}
void main()
{
  int *p;
  *p=10;
  function(p);
}

does the same thing happen here?? i mean here too.. new pointer variable recd is created and value (an address) stored in pointer p is copied to recd???

void function (int *recd)
{
  printf ("%d", *recd);
}

void main()
{
   int p=10;
   function(&p);
}

and here too, does the same thing happens???
please throw some light on it... :)


question - 2:

void main()
{
   int **p;
   **p = 10;   
}

in the above code, where the hell is 10 stored???
what exactly happen in above code??
my guess is : its stored in *p...
i mean **p has a garbage which is taken as *p...and *p has a garbage which is taken as the address of 10....is this right or a garbage?? :P :)

In each of your examples pointer p was never initialized to point to any memory so what you get is a crashed program.

And Yes, in each of the cases you statef for Q 1 a new variable name is created and occupies stack space. If all you want to do is reduce stack size then using a pointer to pass an integer will do nothing to improve that because in most operating systems today the size of a pinter is the same as the size of an integer.

In Q2: its an invalid statement. Here is a better, and more frequently used, example. Note that the pointer **n is a pointer to a pointer.

void foo(int ** n)
{
    *n = malloc(16 * sizeof(int)); // allocate an array of 16 integers
    for(int i = 0; i < 16; i++)
       (*n)[i] = i;
}

int main()
{
     int* p= 0;
     foo( &p );

    return 0;
}

well...i duno what u r saying...
none of my program crashed...
instead both of them worked well and fine..
infact in the 2nd question it printed the values (i guess garbage) for *p and p...
so what say now??

>so what say now??
You got lucky. Just because it doesn't crash when you run it this time doesn't mean it won't crash next time, or when you're demoing your program for a client. Undefined behavior is like that.

so u ppl mean i shouldnt be using this type of weird and idiotic code...rite??
:D
okkk...thanx for help anyways... :)

>>instead both of them worked well and fine
Only because you were lucky.

A pointer must point to something before its used, or memory must be allocated to it as in my previous example. If you fail to set a pointer to something then you will have huge problems in some larger programs.

If you don't believe me, then what can I say except for you to read your textbook about pointers. One of our members WaltP has a very good signature

If you walk on broken glass expect to get cut

or something like that.

so u ppl mean i shouldnt be using this type of weird and idiotic code...rite??
:D
okkk...thanx for help anyways... :)

We aren't saying that at all. What we are saying is that you need to learn how to use pointers. What you have posted so far is misusing them. Pointers are a very important part of the C language, so you need to learn them.

hahaha...ohkkk..i got it friendsss... :)
was just kidding...
but thank you people for clearing my doubt...
i would surely come back with more doubts... :)

If you don't believe me, then what can I say except for you to read your textbook about pointers. One of our members WaltP has a very good signature
or something like that.

It's actually Salem:

If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Comments
Thanks for the correction :)
This article has been dead for over six months. Start a new discussion instead.