0% found this document useful (0 votes)
12 views9 pages

Understanding C Pointers and Types

This document explains C pointers, which are variables that store memory addresses of other variables. It covers the creation, dereferencing, and various types of pointers including integer, array, structure, function, double, NULL, void, wild, constant pointers, and pointers to constants. Additionally, it discusses pointer arithmetic and the relationship between pointers and arrays in C programming.

Uploaded by

x48q4f4cdd
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)
12 views9 pages

Understanding C Pointers and Types

This document explains C pointers, which are variables that store memory addresses of other variables. It covers the creation, dereferencing, and various types of pointers including integer, array, structure, function, double, NULL, void, wild, constant pointers, and pointers to constants. Additionally, it discusses pointer arithmetic and the relationship between pointers and arrays in C programming.

Uploaded by

x48q4f4cdd
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

C Pointers

A pointer is a variable that stores the memory address of


another variable as its value.

A pointer variable points to a data type (like int) of the


same type, and is created with the * operator.

int myAge = 43; // an int variable

printf("%d", myAge); // Outputs the value of myAge


(43)
printf("%p", &myAge); // Outputs the memory address
of myAge (0x7ffe5367e044)

//with pointer

int myAge = 43; // An int variable


int* ptr = &myAge; // A pointer variable, with the
name ptr, that stores the address of myAge

// Output the value of myAge (43)


printf("%d\n", myAge);

// Output the memory address of myAge


(0x7ffe5367e044)
printf("%p\n", &myAge);

// Output the memory address of myAge with the


pointer (0x7ffe5367e044)
printf("%p\n", ptr);
Dereference
In the example above, we used the pointer variable to get the
memory address of a variable (used together with the &
reference operator).

You can also get the value of the variable the pointer points to,
by using the * operator (the dereference operator):

int myAge = 43; // Variable declaration

int* ptr = &myAge; // Pointer declaration

// Reference: Output the memory address of myAge with


the pointer (0x7ffe5367e044)
printf("%p\n", ptr);

// Dereference: Output the value of myAge with the


pointer (43)
printf("%d\n", *ptr);

Note :
Note that the * sign can be confusing here, as it
does two di erent things in our code:
• When used in declaration (int* ptr), it
creates a pointer variable.
• When not used in declaration, it act as a
dereference operator.
ff
Types of Pointers in C

Pointers in C can be classi ed into many di erent types


based on the parameter on which we are de ning their
types. If we consider the type of variable stored in the
memory location pointed by the pointer, then the pointers
can be classi ed into the following types:
1. Integer Pointers

As the name suggests, these are the pointers that point to


the integer values.
Syntax
int *ptr;

These pointers are pronounced as Pointer to Integer.


Similarly, a pointer can point to any primitive data type. It
can point also point to derived data types such as arrays
and user-de ned data types such as structures.
2. Array Pointer

Pointers and Array are closely related to each other. Even


the array name is the pointer to its rst element. They are
also known as Pointer to Arrays. We can create a pointer to
an array using the given syntax.
Syntax
char *ptr = &array_name;

Pointer to Arrays exhibits some interesting properties which


we discussed later in this article.
fi
fi
fi
fi
ff
fi
3. Structure Pointer

The pointer pointing to the structure type is called Structure


Pointer or Pointer to Structure. It can be declared in the
same way as we declare the other primitive data types.
Syntax
struct struct_name *ptr;

In C, structure pointers are used in data structures such as


linked lists, trees, etc.
4. Function Pointers

Function pointers point to the functions. They are di erent


from the rest of the pointers in the sense that instead of
pointing to the data, they point to the code. Let’s consider a
function prototype – int func (int, char), the function pointer
for this function will be
Syntax
int (*ptr)(int, char);

5. Double Pointers

In C language, we can de ne a pointer that stores the


memory address of another pointer. Such pointers are
called double-pointers or pointers-to-pointer. Instead of
pointing to a data value, they point to another pointer.
Syntax
datatype ** pointer_name;
6. NULL Pointer
fi
ff
The Null Pointers are those 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;
or
pointer_name = NULL

It is said to be good practice to assign NULL to the pointers


currently not in use.
7. Void Pointer

The Void pointers in C are the pointers of type void. It


means that they do not have any associated data type.
They are also called generic pointers as they can point to
any type and can be typecasted to any type.
Syntax
void * pointer_name;

8. Wild Pointers

The Wild Pointers are pointers that have not been initialized
with something yet. These types of C-pointers can cause
problems in our programs and can eventually cause them
to crash. If values is updated using wild pointers, they could
cause data abort or data corruption.
Example
int *ptr;
char *str;
9. Constant Pointers

In constant pointers, the memory address stored inside the


pointer is constant and cannot be modi ed once it is
de ned. It will always point to the same memory address.
Syntax
data_type * const pointer_name;
10. Pointer to Constant

The pointers pointing to a constant value that cannot be


modi ed are called pointers to a constant. Here we can
only access the data pointed by the pointer, but cannot
modify it. Although, we can change the address stored in
the pointer to constant.
Syntax
const data_type * pointer_name;

C Pointer Arithmetic
The Pointer Arithmetic refers to the legal or valid arithmetic
operations that can be performed on a pointer. It is slightly
di erent from the ones that we generally use for
mathematical calculations as only a limited set of
operations can be performed on pointers. These operations
include:
• Increment in a Pointer
• Decrement in a Pointer
• Addition of integer to a pointer
• Subtraction of integer to a pointer
• Subtracting two pointers of the same type
ff
fi
fi
fi
• Comparison of pointers of the same type.
• Assignment of pointers of the same type.

// 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;

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 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 rst
element. For example, if we have an array named val then
val and &val[0] can be used interchangeably.
fi
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 aiml()
{
// 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()
{
aiml();
return 0;
}

Example 2: Accessing Array Elements using Pointer


Arithmetic

// C Program to access array elements using pointers


#include <stdio.h>

int main()
{

// defining array
int arr[5] = { 1, 2, 3, 4, 5 };

// defining the pointer to array


int* ptr_arr = arr;

// traversing array using pointer arithmetic


for (int i = 0; i < 5; i++) {
printf("%d ", *ptr_arr++);
}
return 0;
}

You might also like