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

Pointers

The document explains pointers in C, detailing their definition, syntax, and usage, including declaration, initialization, and dereferencing. It also illustrates the relationship between pointers and arrays, demonstrating how array names act as pointers to their first elements and how to manipulate array values using pointers. Examples are provided to clarify these concepts through code snippets and their outputs.

Uploaded by

yvansh681
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)
8 views

Pointers

The document explains pointers in C, detailing their definition, syntax, and usage, including declaration, initialization, and dereferencing. It also illustrates the relationship between pointers and arrays, demonstrating how array names act as pointers to their first elements and how to manipulate array values using pointers. Examples are provided to clarify these concepts through code snippets and their outputs.

Uploaded by

yvansh681
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/ 6

Pointer in C

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.

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;

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.

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.

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.

C Pointer Example
// C program to illustrate Pointers
#include <stdio.h>
void geeks()
{
int var = 10;

// declare pointer variable


int* ptr;
// note that data type of ptr and var must be same
ptr = &var;

// assign the address of a variable to a pointer


printf("Value at ptr = %p \n", ptr);
printf("Value at var = %d \n", var);
printf("Value at *ptr = %d \n", *ptr);
}

// Driver program
int main()
{
geeks();
return 0;
}

Output
Value at ptr = 0x7fff1038675c
Value at var = 10
Value at *ptr = 10

C Pointers and Arrays


Example
int myNumbers[4] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {


printf("%d\n", myNumbers[i]);
}

Result:
25
50
75
100
Instead of printing the value of each array element,
let's print the memory address of each array element:
Example
int myNumbers[4]={25, 50, 75, 100};
int i;

for (i= 0;i< 4; i++){


printf("%p\n",&myNumbers[i]);
}

Result:

0x7ffe70f9d8f0

0x7ffe70f9d8f4

0x7ffe70f9d8f8

0x7ffe70f9d8fc

How Are Pointers Related to Arrays


In C, the name of an array, is actually a pointer to the first element of the
array.

The memory address of the first element is the same as the name of the
array:

Example
int myNumbers[4]={25, 50, 75, 100};

//GetthememoryaddressofthemyNumbersarray
printf("%p\n",myNumbers);

//Getthememoryaddressofthefirstarrayelement
printf("%p\n", &myNumbers[0]);
Result:
0x7ffe70f9d8f0
0x7ffe70f9d8f0
Since myNumbers is a pointer to the first element in myNumbers, you can
use the * operator to access it:

Example
int myNumbers[4]={25, 50, 75, 100};
// Get the value of the first element in myNumbers
printf("%d", *myNumbers);

To access the rest of the elements in myNumbers, you can increment the
pointer/array (+1, +2, etc):

Example
int myNumbers[4]={25, 50, 75, 100};

//Get the value of the second element in myNumbers


printf("%d\n",*(myNumbers+ 1));

//Get the value of the third element in myNumbers


printf("%d",*(myNumbers+ 2));

// and so on..
Result:

50

75

Or loop through it:


int myNumbers[4]={25, 50, 75, 100};
int *ptr=myNumbers;
int i;

for (i= 0;i< 4;i++){


printf("%d\n",*(ptr+i));
}
25

50

75

100

It is also possible to change the value of array elements with pointers:

Example
int myNumbers[4]={25, 50, 75, 100};

// Change the value of the first element to 13


*myNumbers= 13;

// Change the value of the second element to 17


*(myNumbers+1)= 17;
// Get the value of the first element
printf("%d\n",*myNumbers);

// Get the value of the second element


printf("%d\n", *(myNumbers + 1));
Result:

13

17

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