0% found this document useful (0 votes)
74 views17 pages

Pointers Introduction

Here are three C programs demonstrating pointers: 1. Basic pointer declaration: #include <stdio.h> int main() { int var = 10; int *ptr; ptr = &var; return 0; } 2. Handling pointers: #include <stdio.h> int main() { int var = 10; int *ptr; ptr = &var; printf("Address of var: %p\n", &var); printf("Value of ptr: %p\n", ptr); return 0; } 3. Using & and * operators: #include <stdio.h>

Uploaded by

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

Pointers Introduction

Here are three C programs demonstrating pointers: 1. Basic pointer declaration: #include <stdio.h> int main() { int var = 10; int *ptr; ptr = &var; return 0; } 2. Handling pointers: #include <stdio.h> int main() { int var = 10; int *ptr; ptr = &var; printf("Address of var: %p\n", &var); printf("Value of ptr: %p\n", ptr); return 0; } 3. Using & and * operators: #include <stdio.h>

Uploaded by

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

POINTERS

Memory Address
• When a variable is created in C, a memory
address is assigned to the variable.
• The memory address is the location of where
the variable is stored on the computer.
• When we assign a value to the variable, it is
stored in this memory address.
• To access it, use the reference operator (&),
and the result represents where the variable is
stored:
• The memory address is in hexadecimal form
(0x..).
• &myAge is often called a "pointer".
• A pointer basically stores the memory address of
a variable as its value.
• To print pointer values, we use the %p format
specifier.
datatype * ptr;

Creating 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.
• The address of the variable you are working with is
assigned to the pointer:
• datatype * ptr;
– where
– ptr is the name of the pointer.
– datatype is the type of data it is pointing to.
How to Use Pointers?

• The use of pointers can be divided into three


steps:
– Pointer Declaration
– Pointer Initialization
– Dereferencing
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.
• int *ptr;
• The pointer declared here will point to some
random memory address as it is not initialized.
Such pointers are called wild pointers.
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.
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.
• 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.
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 program to illustrate Pointers
#include <stdio.h>

void Ptr_pgm()
{
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);
}

int main()
{
Ptr_pgm();
return 0;
• Write a program in C to show the basic
declaration of a pointer.
• Write a program in C to demonstrate how to
handle pointers in a program.
• Write a program in C to demonstrate the use of the
&(address of) and *(value at address) operators.

You might also like