Pointers in c


A pointer is an unsigned variable that is used to store the address of another variable.

 int i=10;

above shown is an integer type of variable which has been assigned with value 10.

The value 10 is stored in the memory of the computer which of a particular address.


pointer-c

                                                      fig.1.

Note: The address given to memory location which is 6000 is just my assumption. The value the of address may be any number. 

Now how do we get to know the address of the variable?

We can print the address of the variable in the following way

printf(“%u”, &i);

Output:: 6000.

Explanation:

  •   &i returns the address of the variable i.
  • %u is the format specifier for printing the unsigned integer.
  • %p is also a format specifier used for printing the unsigned integer but, in hexadecimal format.

What is a pointer?

It is not necessary that value 10 will be stored in address 6000, because some other time computer may choose another location for storing the value 10.

So the important point here is the address.

We have to store the address of integer "i" in another variable, by using that another variable we can change the value 10 or modify it.

So we can manipulate the value or data inside the "i" variable by using the address of the variable "i" which will be stored in another variable.

That another variable is the pointer variable given by pointer operator ‘*’, called as value at address operator.

It gives the value stored at a particular address.

Pointer syntax:

data_type *var_name;

Example:

int *p;

pointer-c

                                                                 fig.2.

Computer memory

Integer variable holds 2bytes of memory in 32-bit compilers(eg. turbo-c) and 4bytes of memory in 64-bit compilers(eg. GCC).

Here, in below fig.3. we have considered 2bytes.

The pointer variable always has a memory the same as an integer variable. Hence, the pointer variable will also occupy 2bytes.

16bit=1byte

32bit=2bytes

The pointer variable might be belonging to any of the data types such as primitive data type like int, float, char, double, short, etc. or derived data types like an array, structure, union, enum, or user-defined data type like function, pointer, etc.

note:( The pointer variable holding the address of a particular data type must have the same data type.

Eg. float i;

      float *p;

The size of any data type pointer is always the same as integer size.

Above, the size of a float is 4byte(32-bit compiler) but the size of the pointer variable belonging to float data type is 2byte(32-bit compiler).).

pointer-c

                                                          fig.3.

  •   p is the unsigned pointer integer variable.
  •   p holds the address of  the "i" variable. 
  •   *p points to the value to the address which p is holding.  i.e., an address which the p holds, *p points to the value located under that address location. Observe fig.4.
pointer-c

                                                               fig.4.

In above fig.4. The pointer variable "p" holds the address of "i" and points to the value associated with "i".

*p=value of i

p=address  of i.

let's perform some operations on the above example.


1.    printf(“%u “, &i);
       output:: 6000.
       Explanation: address of i is 6000
 
2.     printf(“%u”,p);
       Output:: 6000
        Explanation: The value under p is  6000, which is an address. Hence, format specifier %u is used.
 
3.     printf(“%u”,&p);
       Output:: 7000
       Explanation: address of p is 7000
 
4.    printf(“%d”,*p);
       Output:: 10
      Explanation: The value of i is 10, which is stored under location address 6000.
       Address 6000 is stored in pointer variable p. hence *p is pointing to the value 10.
     
5.    printf(“%u”,&(*p));
       Output:: 6000
       Explanation: now let us understand &(*p). when you will try to read this it will seem as “address  of pointer p”. *p means pointing to the address value which is 10 and & of 10 is 6000. Hence, the output is 10. You just have to cut & and *, at last remains p. The value of "p" is 6000 which is an address that is why a format specifier "%u" is used.
 
6.     printf(“%u”,*(&p));
       Output:: 6000
      Explanation: same as &(*p). &(address) of p is 7000. 7000 is pointing to the value located at 7000 which is  6000.
 
7.     printf(“%u”,**(&p);
       Output:: 10
      Explanation: & of p is 7000. 7000 is pointing to the value located at 7000 which is 6000. 6000 is the address at p pointing to the value located at 6000 which is 10.
 
8.     printf(“%d”,i);
       Output:: 10.

 

 Demo program

 print the table of 12 using the pointer

pointers-c

 output::

pointers-c


input is taken as runtime which is 12. if you want to print the table of any other number then enter the input as that number. 


see also::

pointer to pointer in c

call by value and call by reference