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

Unit 11 Pointers

Uploaded by

1umangrathod0028
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 11 Pointers

Uploaded by

1umangrathod0028
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Pointers

Prepared by: Swasti Patel


Why Pointers?
• They allow you to refer to large data structures in a compact way

• They facilitate sharing between different parts of programs

• They make it possible to get new memory dynamically as your


program is running

• They make it easy to represent relationships among data items.


Pointers
A pointer is a reference to another variable (memory
location) in a program
– Used to change variables inside a function (reference
parameters)
– Used to remember a particular member of a group
(such as an array)
– Used in dynamic (on-the-fly) memory allocation
(especially of arrays)
– Used in building complex data structures (linked lists,
stacks, queues, trees, etc.)
Pointer Variable Definitions and Initialization

Pointer definitions

* used with pointer variables

 int *myPtr;

Defines a pointer to an int (pointer of type int *)

Multiple pointers require using a * before each variable

 definition

 int *myPtr1, *myPtr2;

Can define pointers to any data type


A pointer contains the address of a variable that
has a specific value (indirect reference)
Pointer Variable Definitions and Initialization

Initialize pointers to 0, NULL, or an address


 0 or NULL – points to nothing

 0 is the only integer value that can be assigned directly to


a pointer variable.

 Initializing a pointer to 0 is equivalent to initializing a pointer


to NULL, but NULL is preferred

 NULL is a symbolic constant defined in the <stddef.h>


header and several other headers, e.g. <stdio.h>
Pointer Operator Precedence
Operators Associativity Type

() [] left to right highest

+ -
- ++
++ -- ! * & (type) right to left unary

* / left to right multiplicative

+ - left to right additive

< <= > >= left to right relational

== != left to right Equality

&& left to right logical and

|| left to right logical OR

?: right to left conditional

= += -= *= /= %= right to left assignment

, left to right comma


Pointer Expressions and Pointer Arithmetic

• Arithmetic operations can be performed on


pointers
Increment/decrement pointer (++ or --)
Add an integer to a pointer( + or += , - or -=)
Pointers may be subtracted from each other
Operations meaningless unless performed on an
array
Pointer Notation
Consider the declaration,
Int i=3;
This declaration tells the c compiler to:
(a) Reserve space in memory to hold the integer value.
(b) Associate the name i with this memory location.
(c) Store the value 3 at this location.
We may represent it's location in memory by the following
memory map.
A simple program of Pointer
#include <stdio.h>
void main(void)
{
int nRate = 10;
clrscr();
printf("variable name is nRate.\n");
printf("size of nRate (int) is %d bytes.\n",sizeof(nRate));
printf("size of short intis%dbytes.\n",sizeof(shortint));
printf("initial value stored at nRate is %d.\n", nRate);
printf("memory address of nRate is %p.\n", nRate);
getch();
}
CALL BY VALUE & CALL BY REFERENCE
• Call by value
• If data is passed by value, the data is copied from the variable
used in for example main() to a variable used by the function.
So if the data passed (that is stored in the function variable) is
modified inside the function, the value is only changed in the
variable used inside the function.

• Call by reference
• If data is passed by reference, a pointer to the data is copied
instead of the actual variable as is done in a call by value.
Because a pointer is copied, if the value at that pointers address
is changed in the function, the value is also changed in main().
• Call by value • Call by reference

1. Copy of variable is sent to 1. Send actual address of


function variable storage
2. Change in variable inside 2. Change in variable
function will not affect anywhere affect original
original value value
3. Scope is limited to 3. Scope is global
function only
Call by value Vs call by reference
Pointer v/s array

You might also like