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

Subject Name:Data Structure Using C: Content of This Presentation Has Been Taken From Book

This document summarizes key concepts about data structures using pointers in C. It discusses declaring and initializing pointers, accessing variables using pointers, and provides simple example programs. Specifically, it explains that pointers hold the address of another variable, and how the address and indirection operators work to retrieve and update values at those addresses. Two simple programs are presented to illustrate using pointers to print and modify variable values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Subject Name:Data Structure Using C: Content of This Presentation Has Been Taken From Book

This document summarizes key concepts about data structures using pointers in C. It discusses declaring and initializing pointers, accessing variables using pointers, and provides simple example programs. Specifically, it explains that pointers hold the address of another variable, and how the address and indirection operators work to retrieve and update values at those addresses. Two simple programs are presented to illustrate using pointers to print and modify variable values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

SUBJECT NAME :DATA STRUCTURE USING C

Content of this presentation has been


taken from Book
“DATA STRUCTURE USING C”
1. SATISH HUNNUR Published by SUDHA PUBLICATION
2. RAJESH HONGAL Published by Eastern book Promoter Belgaum
content
CONCEPT OF POINTER
DECLARING POINTERS
INITIALIZING POINTERS
ACCESSING VARIABLES USING POINTERS
SIMPLE PROGRAMS
CONCEPT OF POINTERS
Definition of Pointer—“Pointer is a special variable,
which holds the address of another variable or
function code”.
Pointers are used to point to variables of any data type,
arrays, functions, structures and union
How pointer works?--Pointers operate on the
concept of indirection. That is, a pointer provides an
indirect means of accessing or retrieving the value of a
particular variable.
continue
How pointer works?--Pointers operate on the
concept of indirection. That is, a pointer provides an
indirect means of accessing or retrieving the value of a
particular variable.
Declaration of Pointer

Like variables, pointers must also be declared in a


program before their use. Declaring a pointer variable
follows the same rules of identifiers.
The syntax of declaring pointers is
Datatype var1,var2
data type *var1,*var2……..,*var-n;
Each pointer variable must be prefixed with Here the
(asterisk).
continue
Examples of pointer declarations:

1) int *ptrl, *ptr2;

2) char *str;

3) float *x,*y;
Initialization of Pointers

A pointer variable can be initialized


at the time of its declaration
 or after declaration before using it.
The syntax of pointer initialization is:
data type *var=&variable;
Or
data type variable, *var;
var=&variable;
Continue--
Example:
int amount;
int *ptr=&amount;

Or
int amount=10,*ptr=&amount;
Here, the integer pointer variable ptr is initialized with
the address of integer variable amount.
1.3 ACCESSING VARIABLES USING POINTERS

The two pointer operators are


1) address operator (& ampersand)
ii) indirection operator (* asterisk)
Continue--
The address operator (&) gives the address of a
variable. To get the address of a variable, prefix an
operator &(ampersand) to the variable.
Int *ptr;
Int a=10;
Ptr=&a;
Indirection operator (*) The value of variables can
be accessed indirectly through the pointer using the
indirection operator. The indirection operator helps to
give the value at an address contained in the pointer.
Continue-
Consider the following statements:
int sum, *ptr;
sum=15;
ptr=∑ /* address of variable sum is stored in
pointer ptr */
Simple programs to illustrate concept of pointers :
1. Program to illustrate address operator and indirection
operator.
#include <stdio.h>
#include<conio.h>
int main()
{
int a=5, *ptr;
ptr=&a; /* ptr is initialized with address of a */
printf("ptr=%u\n",ptr);
printf("*ptr=%d\n", *ptr);
printf("a=%d\n", a);
getch();
}
2. Program where you can manipulate pointers to change the value
stored in a variable pointed to by a pointer.
#include <stdio.h>
#include<conio.h>
int main()
{
int k=10, *p; /* p is a pointer to int */
 
printf("Present value of k=%d\n", k);
p=&k; /* pointer p is initialized to point to variable k */
*p=20; /*store new value to location pointed to by p */
printf(" Latest value of k=%d\n", k);
printf("Pointed to value, "p=%d\n", *p);
getch();
}

You might also like