-6

Could you please tell me the difference between *p and &p in C programming language? cause I really have problem with this and I don't know whether *p or &p is ok!!!!

mercedes
  • 29
  • 1
  • 1
  • 2
  • 1
    The two operators are the opposite of each other. Please give us some context that confuses you. – NPE Dec 26 '14 at 09:33
  • Please refer this site, http://stackoverflow.com/questions/9661293/c-p-vs-p-vs-p – Chandru Dec 26 '14 at 09:34
  • `*p` derefernces the pointer `p` and `&p` gives the address of the pointer `p` – Spikatrix Dec 26 '14 at 09:35
  • 1
    Perhaps reading the fist couple of chapters on C would be a good idea – Ed Heal Dec 26 '14 at 09:36
  • In definitions, or in expressions? The difference is often confusing for beginners, particularly when initialisers are involved. – wildplasser Dec 26 '14 at 09:36
  • 2
    Take a deep breath, start reading about the operators from [here](https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B). – Sourav Ghosh Dec 26 '14 at 09:38
  • See [this question](http://stackoverflow.com/q/27484168/1382251), with several answers (including my own) that might help you grasp the fundamental principles of pointers. – barak manos Dec 26 '14 at 09:40
  • Did you get a compiler as a present? I wish people would be more responsible - compilers are for life, not just for Christmas. – Martin James Dec 26 '14 at 13:16

2 Answers2

4

Just take

int a =10;
int *p = &a;

a is a variable which holds value 10. The address in which this value is stored is given by &a.

Now we have a pointer p , basically pointer points to some memory location and in this case it is pointing to memory location &a .

*p gives you 10

This is called dereferencing a pointer.

p = &a /* Gives address of variable a */

Now let's consider

&p

Pointer is a also a data-type and the location in which p is stored is given by &p

Gopi
  • 19,784
  • 4
  • 24
  • 36
2

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The general form of a pointer variable declaration is:

type *var-name;

eg:

int    *ip;    /* pointer to an integer */
double *dp;    /* pointer to a double */
float  *fp;    /* pointer to a float */
char   *ch     /* pointer to a character */

Look at this program :

#include <stdio.h>

int main ()
{
   int  var = 20;   /* actual variable declaration */
   int  *ip;        /* pointer variable declaration */

   ip = &var;  /* store address of var in pointer variable*/

   printf("Address of var variable: %x\n", &var  );

   /* address stored in pointer variable */
   printf("Address stored in ip variable: %x\n", ip );

   /* access the value using the pointer */
   printf("Value of *ip variable: %d\n", *ip );

   return 0;
}

https://www.tutorialspoint.com/cprogramming/c_pointers.htm

Jacob
  • 187
  • 1
  • 7
Avnish Mehta
  • 99
  • 1
  • 8