0% found this document useful (0 votes)
21 views3 pages

POINTERS

The document discusses pointers in C. Pointers are a fundamental concept in C and allow accessing and modifying data in memory. The document explains what pointers are, how they are declared and initialized, and how the address and dereference operators work. It provides examples of declaring pointer variables and assigning addresses to them.

Uploaded by

Shashank Katti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

POINTERS

The document discusses pointers in C. Pointers are a fundamental concept in C and allow accessing and modifying data in memory. The document explains what pointers are, how they are declared and initialized, and how the address and dereference operators work. It provides examples of declaring pointer variables and assigning addresses to them.

Uploaded by

Shashank Katti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

POINTERS

Introduction:
• The concept of pointers in C is the prime power of the language.
• Pointers are data, it can access other variable data.
• Pointer data is special because you can use them to access other data.

There are several reasons why we use pointers, and few are listed below.
• A pointer variable can be used to access the array without explicit indexing, especially
for character strings.
• Array of pointers to refer to other blocks of data.
• Dynamic memory allocation is possible only through pointers.
• A function can be called dynamically using pointers (pointer to a function).

Disadvantages of using pointers are:


• Unless defined and initialized properly use of pointers may lead to disastrous
situations or lead dangerous.
• Using pointers may sometimes be confusing than ordinary methods.

Understanding the C’s pointer notation.


The & and * operators - These are the most important operators in pointers.

& - the address operator


- The expression &variable_name returns the address of the variable_name.

* - the dereferencing or indirection operator


- Value at address operator, it returns the value stored at a particular address.

Example: consider the declaration: int i = 3;

This declaration tells the C compiler to (at the time of compilation)

• Reserve space in memory to hold the integer value.


• Associate the name i with this memory location.
• Store the value 3 at this location.

Memory map: i
3
65524
i – variable / location name
3 – value of location
65524 – location number (address of the vriable)

We see that the compiler has selected memory location 65524 as the place to store the value 3.
Program: 1

#include<stdio.h>
#include<conio.h>

void main()
{
int i=3;

clrscr();

printf("Value of i = %d", i);


printf("\n Value of i = %u", i);
printf("\n Address of i = %u", &i);

getch();
}

Output:

Understanding pointers
• When a pointer variable is declared, it is ready to hold a valid memory address.
• This memory address in turn will be having some data value.
Example:

Heap
i
3
ptr 65524 65524
Stack

Here the pointer variable ptr points to memory address 65524, which contains data 3.

• Generally, C compiler allocates memory address to each of the variable declared in


the program at the time of compilation.
• However, we can’t alter the address of i by any means.
• But pointer variable can be assigned any valid address during run-time.
• The size of pointer variable is always 2 bytes (it depends on machine and what data
type it points to) because pointer variables hold only addresses and therefore point to
int or float or char…. etc.,

Declaring and initializing pointer variables.


Syntax for declaring a pointer variable is:

data_type *variable_name ;

data_type – is any primitive or user defined data type in C including void.


*variable_name – indirection operator and this informs the compiler that it is a pointer
variable.

int *int_ptr;
Example:
float *float_ptr;

• After declaring a pointer variable of particulars type, "it is the responsibility of the
programmer to assign a valid memory address to that variable".

How to assign a valid address?


There are two ways:
(1) Declare an ordinary variable and put that address to the pointer variable.
Example: int a;
int *p = &a;

(2) Getting the address dynamically using DMA (Dynamic Memory Allocation: malloc(),
calloc(), realloc() functions ).
Example: 1 int *p;
p = ( int * ) malloc ( sizeof ( int ) ) ;
2 bytes (int) of memory and type casting (int *) done

Example: 2
float *p;
p = ( float *) malloc ( sizeof ( float ) ) ;

4 bytes of memory and type casting done

You might also like