Skip to main content

Pointers


A pointer variable is a variable that holds the memory address of another variable. They are called pointers for the simple reason that, by storing an address, they point to a particular location in memory.
At the moment when a variable is declared, it must be stored in a concrete memory location. The programs do not decide where the variable is to be placed. That is done automatically by the compiler and the operating system at run time.
The declaration  int x=3  tells the C compiler to
  •        Reserve space in memory to hold the integer value.(location/address is decided by the       compiler/os)
  •        Associate the name x with this memory location.
  •         Store the value 3 in this location.
The space allocated can be obtained with
printf(“%d”,sizeof(x));

The value stored can be obtained with
printf(“%d”,x);

The memory location or address can be obtained with &
printf(“%p”,&x); // &x  is the address of the variable x.

The pointer variable is declared with *(indirection) operator.
Eg:
int *p; creates a pointer variable p which can store the address of an integer variable.
p=&x ; now p contains address of integer variable x ie; p points to variable x.

Because a pointer holds an address rather than a value, it has two parts.
The pointer itself holds the address.
The address points to a value.

printf(“%p”,p) will print the address it holds
printf(“%d”,*p) will  print the value it points to

pointers can be used to
  • Build faster and efficient code.
  •  Alternate/efficient way of accessing information stored in variables and arrays.
  •  Pass arrays and strings more conveniently from one function to another.
  •  Return more than one value from a function
  •  Dynamic memory allocation.
  •  Build complex data structures.( linked lists, trees etc..)
In C programming, the & (address-of) and * (dereference) operators play important roles when working with pointers:

Address-of Operator (&):
The & operator is used to obtain the memory address of a variable.
It returns the memory address where the variable is stored in the computer's memory.
Example: &variable returns the address of the variable variable.

Dereference Operator (*):
The * operator is used to access the value stored at the memory address pointed to by a pointer.
It is also used to declare pointers.
Example: *pointer accesses the value stored at the memory location pointed to by pointer

NULL Pointer
Pointers that do not point to any memory location. They can be created by assigning a NULL value to the pointer. A pointer of any type can be assigned the NULL value.
Syntax
data_type *pointer_name = NULL;

Void Pointer / Generic Pointer
Pointers of type void. 
They do not have any associated data type. 
Syntax:
void * pointer_name;

Constant Pointers
The memory address stored inside the pointer is constant and cannot be modified once it is defined. It will always point to the same memory address.
Syntax
data_type * const pointer_name;

Pointer to Constant
The pointers pointing to a constant value that cannot be modified are called pointers to a constant. 
Syntax
const data_type * pointer_name;

Dangling pointer
A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer.

The following program will illustrate the pointer indirection operator and dereferencing
#include <stdio.h>
int main()
{
int num=5;
int *ptr=&num;
printf(“the value and address of num is %d %p\n”,num,&num);
printf(“the value and address of num using pointer ptr is %d %p\n”,*ptr,ptr);
*ptr=15;                     // indirection, which stores value in variable num using the pointer ptr.
printf(“the value of num after indirect initialization is %d\n”,num);

the value and address of num is 5 0x7ffe6246e2fc
the value and address of num using pointer ptr is 5 0x7ffe6246e2fc
the value of num after indirect initialization is 15
The following program uses a call by reference mechanism to modify a variable value using pointers
#include <stdio.h>
void f(int *x) // x is a pointer to n
{
*x=*x+1; //accessing the actual content
}
int main()
{
int n=10;

f(&n); //calling the function with address/reference
printf("n in main =%d\n",n);
//note..... value of n changed in main

}
The following program use pointers to add two numbers using a  function add
int add(int *p1,int *p2)
          return(*p1+*p2);
 }
#include <stdio.h>
int main()
{
    int a=10,b=12,sum;
    sum=add(&a,&b);
    printf("Sum=%d",sum);
}

Comments

Popular posts from this blog

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboar

Arrays in C-single and multi dimensional arrays- list and matrix

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list(vector); A two dimensional array is like a table(matrix). We can have more dimensions. Always, Contiguous (adjacent) memory locations are used to store array elements in memory. Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element. Declaring  Single Dimensional Arrays Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for mentioning dimension of the array.  Dimensions used when declaring arrays in C must be positive integral constants or constant expressions.  In C99, dimensions must still be positive integers, but variables can be used, so long as the variable has a positive value at the time the array is declared. ( Space is allo