0% found this document useful (0 votes)
31 views20 pages

L24 Pointer Basics

The document discusses pointers in C programming. It defines pointers as variables that store memory addresses and can be used to indirectly access other variables. It explains that pointers must be declared with a data type and can be initialized by using the address-of operator (&) on another variable. Pointers can then be dereferenced using the asterisk (*) operator to access and modify the value of the variable being pointed to. Examples are provided to demonstrate declaring, initializing, and accessing variables through pointers.

Uploaded by

Rugved Joshi
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)
31 views20 pages

L24 Pointer Basics

The document discusses pointers in C programming. It defines pointers as variables that store memory addresses and can be used to indirectly access other variables. It explains that pointers must be declared with a data type and can be initialized by using the address-of operator (&) on another variable. Pointers can then be dereferenced using the asterisk (*) operator to access and modify the value of the variable being pointed to. Examples are provided to demonstrate declaring, initializing, and accessing variables through pointers.

Uploaded by

Rugved Joshi
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/ 20

PointersL24

11/06/2023 CSE 1001 Department of CSE 1


Objectives
• To learn and appreciate the following concepts:

• Pointers – declaration and initialization


• To access a variable through its pointer

11/06/2023 CSE 1001 Department of CSE 2


Session outcome
• At the end of session one will be able to:

Understand the overall ideology of pointers

11/06/2023 CSE 1001 Department of CSE 3


Pointers - Concept
• Consider the following statement

int Quantity =50;

- Compiler will allocate a memory location for Quantity


and places the value in that location. Suppose the
address of that location is 5000, then

11/06/2023 CSE 1001 Department of CSE 4


Pointers - Concept
 During Execution of the program, the system always
associates the name quantity with the address 5000.

 We may have access to the value 50 by using either the


name of the variable quantity or the address 5000.

 Since memory addresses are simply numbers, they can be


assigned to some variables which can be stored in memory,
like any other variable.

11/06/2023 CSE 1001 Department of CSE 5


Pointer

-A memory location or a variable which stores the


address of another variable in memory

-Commonly used in C than in many other languages (such


as BASIC, Pascal, and certainly Java, which has no
pointers).

11/06/2023 CSE 1001 Department of CSE 6


Declaring and initializing pointers
Syntax:
data_type * pt_name;

This tells the compiler 3 things about the pt_name:

 The asterisk(*) tells the variable pt_name is a


pointer variable.
 pt_name needs a memory location.
 pt_name points to a variable of type data_ type

11/06/2023 CSE 1001 Department of CSE 7


Accessing the address of a variable
int Quantity=50 ;
• To assign the address 5000 (the location of quantity) to a
variable p, we can write:
int *p = &Quantity ;
Such variables that hold memory addresses are called
Pointer Variables.

11/06/2023 CSE 1001 Department of CSE 8


Pointers Concept

The Address-of Operator &

• To find the address occupied by a variable

11/06/2023 CSE 1001 Department of CSE 9


Program to illustrate the address of operator
Output:
#include <stdio.h>
int main() 0x29feec
{ 0x29fee8
0x29fee4
int var1 = 11;
int var2 = 22;
int var3 = 33;

//print the addresses of these variables


printf(“%x”,&var1);
printf(“%x”,&var2);
printf(“%x”,&var3);

return 0;
}
11/06/2023 CSE 1001 Department of CSE 10
Declaring and initializing pointers
Example:
int *p; //declares a variable p as a pointer variable
that points to an integer data type.

float *x; //declares x as a pointer to floating point


variable.

• Once a pointer variable has been declared, it can be made


to point to a variable using an assignment statement :

int quantity = 10;


p = &quantity; // p now contains the address of quantity.
This is known as pointer initialization.

11/06/2023 CSE 1001 Department of CSE 11


Summary till now …

int v; //defines variable v of type int


int* p; //defines p as a pointer to int

p = &v; //assigns address of variable v to pointer p

Now…
v = 3; //assigns 3 to v

11/06/2023 CSE 1001 Department of CSE 12


To be taken care …
 Before a pointer is initialized, it should not be used.
 We must ensure that the pointer variables always
point to the corresponding type of data.
 Assigning an absolute address to a pointer variable is
prohibited. i.e p=5000
 A pointer variable can be initialized in its declaration
itself.
Example:
int x, *p=&x; //declares x as an
integer
variable and then initializes
p to the address of x.

11/06/2023 CSE 1001 Department of CSE 13


To be taken care …
The statement

int *p = & x, x; not valid.

i.e target variable ‘x’ must be declared first.

11/06/2023 CSE 1001 Department of CSE 14


Accessing variable through a pointer
• A variable’s value can be accessed by its pointer using
unary operator *(asterisk) known as indirection operator.

Consider the following statements:


int quantity, *p, n; // 2 int variables & 1 integer pointer
quantity =50; // assigns value 50 to quantity
p=&quantity; // assigns the address of quantity to p
n=*p; // contains the indirection operator *

* Operator - value at address operator

11/06/2023 CSE 1001 Department of CSE 15


Example – Accessing variable through a pointer
#include <stdio.h>
Output :
int main() 11
{ 22
int var1 = 11; //two integer variables
int var2 = 22;
int *ptr; //pointer to integer

ptr = &var1; //pointer points to var1


printf(“%d”,*ptr); //print contents of pointer (11)

ptr = &var2; //pointer points to var2


printf(“%d”,*ptr); //print contents of pointer (22)

return 0;
11/06/2023
} CSE 1001 Department of CSE 16
Example - Accessing via pointers.
#include <stdio.h>

int main()
{

int var1, var2; //two integer variables


int *ptr; //pointer to integers

ptr = &var1; //set pointer to address of var1


*ptr = 37; //same as var1=37 ( Dereferencing)
var2 = *ptr; //same as var2=var1
printf(“%d”, var2); //verify var2 is 37

return 0;
}
11/06/2023 CSE 1001 Department of CSE 17
Reference and dereference operators

 & is the ‘reference’ operator and can be read as "address


of"

 * is the ‘dereference’ operator and can be read as “value


at address” or "value pointed by"

11/06/2023 CSE 1001 Department of CSE 18


Example- understanding pointers
#include <iostream>
using namespace std; Output :
int main() firstvalue is 10
{ secondvalue is 20
int firstvalue = 5, secondvalue = 15;
int * p1, * p2;
p1 = &firstvalue; // p1 = address of firstvalue
p2 = &secondvalue; // p2 = address of secondvalue
*p1 = 10; // value pointed by p1 = 10
*p2 = *p1; // value pointed by p2 = value pointed by p1
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // value pointed by p1 = 20
Printf(“firstvalue is %d”, firstvalue);
Printf(“secondvalue is %d”, secondvalue);

return 0;
} 11/06/2023 CSE 1001 Department of CSE 19
Summary
• Pointer concept

• Reference operator &

• Dereference operator *

11/06/2023 CSE 1001 Department of CSE 20

You might also like