top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C Programming: Pointers Basic

+1 vote
398 views

A pointer is a variable whose value is the address of another variable or in simple words a variable which points to another variable.

int *myvar;

Here, myvar is a pointer type and can point to a integer variable. The asterisk * you used to declare a pointer is the same asterisk that you use for multiplication.

Few sample declaration

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

Sample Program

#include<stdio.h>

int main()
{
  int x = 5;
  int *ptr,**pptr;
  ptr = &x;
  pptr = &ptr;
  return(0);
}

Here x is an integer and ptr is a pointer pointing to the x. pptr is another pointer which is pointing to the ptr. x is having a value as 5 and support the address of x is 100 then value of ptr would be 100. Now assume value of ptr is 500 then pptr would have value as 500.

Null Pointers in C

It is a good practice to assign a NULL value to a pointer variable in case you do not have exact address to be assigned. Pointer that is assigned NULL is called a null pointer.

main ()
{
   int  *ptr = NULL; // ptr is a null pointer here
   printf("The value of ptr is : %x\n", ptr  );
}

Use Case To check for a null pointer one can use if statement as follows:

if(ptr)     /* succeeds if p is not null */
if(!ptr)    /* succeeds if p is null */

Address Operator (&) and pointers

It is denoted by & and used as a prefix of variable and returns the address of variable. It can be used only with variables not with literals (&5 is a invalid statement. Check the following program for the use case -

#include <stdio.h>

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

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

   printf("Address of variable i: %x\n", &i);
   printf("Address stored in variable ip: %x\n", ip);
   printf("Value of *ip variable: %d\n", *ip);

   return 0;
} 
posted May 27, 2014 by Salil Agrawal

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

There are three ways in which a constant can be correlated with pointer -
1. constant pointer
2. pointer to a constant
3. constant pointer to a constant

1. Constant Pointers
A constant pointer is a pointer that cannot change the address its holding. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.

Sample Declaration: int * const ptr;

Sample code

int main()
{
  int num[2] = {1,2};
  int* const ptr = &num[0];

  printf("%d",*ptr); // This will print 1

  *ptr = 5;
  printf("%d",*ptr); // This will print 5

  ptr++; //This is an compilation error 
}

Note: We must initialize the pointer at the time of declaration as following will give the error.

int* const ptr;
ptr = &num[0];

2. Pointer to a Constant
A pointer through which cannot change the value of variable it points is known as a pointer to constant. These type of pointers can change the address they point to but cannot change the value at that address.

Sample Declaration: const int* ptr;

Sample Code

int main(void)
{
    int var1 = 0;
    const int* ptr = &var1;
    *ptr = 1; // Invalid operation as ptr is pointer to a constant
}

3. Constant Pointer to a Constant
This type is a pointer which is mix of first two which means that a pointer can not point to another variable neither change the value of the variable it point to.

Sample Declaration: const int* const ptr;

Sample Code

int main()
{
  int num[2] = {1,2};
  const int* const ptr = &num[0];

  printf("%d",*ptr); // This will print 1

  *ptr = 5; // This is a compilation error

  ptr++; //This is an compilation error 
} 
READ MORE

In this article I will discuss about the various arithmetic operations in C. Lets discuss them under the following three sub topics -

1. Increment/Decrement And Addition/Subtraction
2. Difference & Comparison
3. Invalid operations

1. Increment/Decrement And Addition/Subtraction

Increment or decrement of a pointer is dependent on the size of the variable on which we are applying the increment/decrement operation.

new value of the pointer =  current value (address) +  i * size_of(data type) 
new value of the pointer =  current value (address) -  i * size_of(data type)  

Behavior with the array
When pointer is pointing to the array and if we increase the pointer then the new value (address) contains would be

New Address of Pointer = Address of Pointer + i*(Size of Data Type)*(Size of Array)

Check the following example

#include<stdio.h>

int main(){

 float var[5]={1.01, 2.02, 3.03, 4.04, 5.05};
 float(*ptr)[5];

 ptr=&var;
 printf("Value inside ptr : %u",ptr);

 ptr=ptr+1;
 printf("Value inside ptr : %u",ptr);

 return 0;
}

Sample Output

Value inside ptr : 100 (if)
Value inside ptr : 120 

One should always remember that only integer can be added to a pointer variable. But for subtraction we can subtract one pointer from another if they are of same type.

2. Difference & Comparison

Two pointer can be subtracted if they hold same memory space and same type.

ptr2 - ptr1 = (Address of ptr2 - Addres of ptr1) / sizeof(*ptr1 or *ptr2)

Comparison of two pointer is perfectly valid in C even if they hold different type of variable, see the following example

int main()
{
 int *ptr1;
 float *ptr2;

 ptr1 = (int *)1;
 ptr2 = (float *)2;

 if(ptr2 > ptr1)
   printf("Ptr2 is far from ptr1");

 return(0);
}

3. Invalid operations

Following operations are invalid operations with pointers
1. Addition of two pointers
2. Multiplication of two pointers/one pointer and one variable(int/float etc)
3. Division between two pointers/One pointer and one variable (int/float etc)
4. Modulo operation on any pointer
5. Any Bitwise operation, negate operation

READ MORE
...