0% found this document useful (0 votes)
7 views

C Pointers

C pointers are a derived data type that store the address of other variables or memory locations, allowing access and manipulation of data. The size of pointers is consistent across types, depending only on the system architecture, and the use of pointers involves declaration, initialization, and dereferencing. Pointers are essential in C for various functionalities, including passing arguments by reference, accessing array elements, and dynamic memory allocation.

Uploaded by

rajoanatahosin
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)
7 views

C Pointers

C pointers are a derived data type that store the address of other variables or memory locations, allowing access and manipulation of data. The size of pointers is consistent across types, depending only on the system architecture, and the use of pointers involves declaration, initialization, and dereferencing. Pointers are essential in C for various functionalities, including passing arguments by reference, accessing array elements, and dynamic memory allocation.

Uploaded by

rajoanatahosin
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/ 16

C Pointers

C Pointers
A pointer is defined as a derived data type that can store the address of
other C variables or a memory location. We can access and manipulate the
data stored in that memory location using pointers.
As the pointers in C store the memory addresses, their size is
independent of the type of data they are pointing to. This size of
pointers in C only depends on the system architecture.

2
Syntax of C Pointers

The syntax of pointers is similar to the variable declaration in C, but we


use the ( * ) dereferencing operator in the pointer declaration.

datatype * ptr;
where
•ptr is the name of the pointer.
•datatype is the type of data it is pointing to.
The above syntax is used to define a pointer to a variable. We can also
define pointers to functions, structures, etc.

3
How to Use Pointers?

The use of pointers in C can be divided into three steps:


1.Pointer Declaration
2.Pointer Initialization
3.Pointer Dereferencing

1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it. To declare a
pointer, we use the ( * ) dereference operator before its name.
Example
int *ptr;The pointer declared here will point to some random memory address as it is not
initialized. Such pointers are called wild pointers.
How to Use Pointers?

2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer variable. We
generally use the ( & ) addressof operator to get the memory address of a variable and then store
it in the pointer variable.

Example

int var = 10;


int * ptr;
ptr = &var;
We can also declare and initialize the pointer in a single step. This method is called pointer
definition as the pointer is declared and initialized at the same time.

Example

int *ptr = &var;


Note: It is recommended that the pointers should always be initialized to some value before
starting using it. Otherwise, it may lead to number of errors.
How to Use Pointers?

3. Pointer Dereferencing
Dereferencing a pointer is the process of accessing the value stored in the memory address
specified in the pointer. We use the same ( * ) dereferencing operator that we used in the pointer
declaration.
Size of Pointers in C

The size of the pointers in C is equal for every pointer type. The size of the pointer does not
depend on the type it is pointing to. It only depends on the operating system and CPU
architecture. The size of pointers in C is
•8 bytes for a 64-bit System
•4 bytes for a 32-bit System
The reason for the same size is that the pointers store the memory addresses, no matter what type
they are. As the space required to store the addresses of the different memory locations is the
same, the memory required by one pointer type will be equal to the memory required by other
pointer types.
How to find the size of pointers in C?
We can find the size of pointers using the sizeof operator as shown in the following program:
C Program to find the size of different pointer types.
#include <stdio.h>

// dummy structure
struct str {
};

// dummy function
void func(int a, int b){};

int main()
{
// dummy variables definitions
int a = 10;
char c = 'G';
struct str x;
C Program to find the size of different pointer types.
// pointer definitions of different types
int* ptr_int = &a;
char* ptr_char = &c;
struct str* ptr_str = &x;
void (*ptr_func)(int, int) = &func;
void* ptr_vn = NULL;

// printing sizes
printf("Size of Integer Pointer \t:\t%d bytes\n",
sizeof(ptr_int));
printf("Size of Character Pointer\t:\t%d bytes\n",
sizeof(ptr_char));
printf("Size of Structure Pointer\t:\t%d bytes\n",
sizeof(ptr_str));
printf("Size of Function Pointer\t:\t%d bytes\n",
sizeof(ptr_func));
printf("Size of NULL Void Pointer\t:\t%d bytes",
sizeof(ptr_vn));

return 0;
C Program to find the size of different pointer types.
Output
Size of Integer Pointer : 8 bytes
Size of Character Pointer : 8 bytes
Size of Structure Pointer : 8 bytes
Size of Function Pointer : 8 bytes
Size of NULL Void Pointer : 8 bytes
C Pointer Arithmetic
The Pointer Arithmetic refers to the legal or valid arithmetic
operations that can be performed on a pointer

// C program to illustrate Pointer Arithmetic

#include <stdio.h>

int main()
{

// Declare an array
int v[3] = { 10, 100, 200 };

// Declare pointer variable


int* ptr;

// Assign the address of v[0] to ptr


ptr = v;
C Pointer Arithmetic
The Pointer Arithmetic refers to the legal or valid arithmetic
operations that can be performed on a pointer

for (int i = 0; i < 3; i++) {

// print value at address which is stored in ptr


printf("Value of *ptr = %d\n", *ptr);

// print value of ptr


printf("Value of ptr = %p\n\n", ptr);

// Increment pointer ptr by 1


ptr++;
}
return 0;
}
C Pointer Arithmetic

Output
Value of *ptr = 10
Value of ptr = 0x7ffe8ba7ec50

Value of *ptr = 100


Value of ptr = 0x7ffe8ba7ec54

Value of *ptr = 200


Value of ptr = 0x7ffe8ba7ec58
C Pointers and Arrays

In C programming language, pointers and arrays are closely related. An array name acts like a
pointer constant. The value of this pointer constant is the address of the first element. For
example, if we have an array named val then val and &val[0] can be used interchangeably.
If we assign this value to a non-constant pointer of the same type, then we can access the
elements of the array using this pointer.
Example 1: Accessing Array Elements using Pointer with Array Subscript
C Program to access array elements using pointer
#include <stdio.h>

void geeks()
{
// Declare an array
int val[3] = { 5, 10, 15 };

// Declare pointer variable


int* ptr;

// Assign address of val[0] to ptr.


// We can use ptr=&val[0];(both are same)
ptr = val;

printf("Elements of the array are: ");

printf("%d, %d, %d", ptr[0], ptr[1], ptr[2]);

return;
}

// Driver program
int main()
{
C Program to access array elements using pointer
Output
Elements of the array are: 5 10 15

Uses of Pointers in C
The C pointer is a very powerful tool that is widely used in C
programming to perform various useful operations. It is used to achieve
the following functionalities in C:
1.Pass Arguments by Reference
2.Accessing Array Elements
3.Return Multiple Values from Function
4.Dynamic Memory Allocation
5.Implementing Data Structures
6.In System-Level Programming where memory addresses are useful.
7.In locating the exact value at some memory location.
8.To avoid compiler confusion for the same variable name.
9.To use in Control Tables.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy