×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Pointers in C language

Pointers

The pointer is a special type of variable, which points (stores) address of another variable. It provides direct access to memory. Using the pointer variable you can store the address of another simple variable even you can access/edit the value of that variable.

Declaration of pointer variables

A pointer variable declares followed by an asterisk sign (*).

Syntax

data-type *pointer-variable;

---
int* a; 	//integer pointer variable
float* b; 	// float pointer variable
char* c;	// character pointer variable
struct student* std; // integer pointer variable

Each pointer variable takes 2 bytes (in 16 bytes compilers)/ 4 bytes (in 32 bytes compilers) in the memory, no matter what type of variable is it?

Initialization of a pointer variable

Each pointer variable must be initialized with the valid address of another variable. Since a pointer contains the address only, so you can not assign a value directly in it.

Syntax

// Initialization with declaration
data-type *pointer-variable= &another-variable;

// Initialization after declaration
data-type *pointer-variable;
pointer-variable= &another-variable;

---
int var1,var2;

//Initialization with declaration
int *a=&var1;

// Initialization after declaration
int *b;
b = &var2;

Example

#include <stdio.h>

int main()
{

    int var = 100;
    int* ptr; /*pointer declaration*/

    ptr = &var; /*pointer initialization*/

    printf("\nValue of var= %d", *ptr);
    printf("\nAddress of var= %X", ptr);
 
    return 0;
}

Output

Value of var= 100
Address of var= 94FE9C
 c language pointers

Advantages of using pointers

  • As we know a function can return a single value but using pointers we can return multiple values from the functions.
  • Generally, we declare memory to variables at the compile time but there is a problem either we need more memory or we occupy extra memory space. Using the pointers we can allocate memory to the variable at run time and of course, we can resize it while execution time.
  • Using pointers we can design complex data structures like Stack Queue, Linked List, and Tree, etc.
  • Generally, we can not change the value of a variable in a function (call by value) but using pointers (call by reference) we can change the actual values of the parameters.
  • And pointers allow direct memory access.

Disadvantages of using pointers

  • Pointers allow direct memory access, it can access protected memory locations.
  • If the pointer is uninitialized, it can be the cause of the segmentation fault.
  • These are slower than other simple variables.
  • Pointers need free dynamically allocated memory.


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.