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

Intro To Pointer

Uploaded by

202251013
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

Intro To Pointer

Uploaded by

202251013
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/ 12

◦ A pointer is a variable just like other

variable.
◦ The only difference from other
variables is that it stores the
memory address other variables.
◦ This variable may be of type
int, char, array, structure, function
or any other.
Normal variables
◦ Contain a specific value (direct reference)

Pointer variables
◦ Contain memory addresses as their values

countPtr count count


7 7
Pointer declarations
◦ ‘*’ used with pointer variables
int *myPtr;
float *Ptr;
Char *strPtr;
◦ Multiple pointers require using a * before each
variable declaration
int *myPtr1, *myPtr2;
Initialize pointers to:
◦ 0 or NULL,
 points to nothing (NULL preferred)
◦ an address
 points to a certain place/address in the memory

int y = 5;
int *yPtr;
yPtr = &y; // yptr is the address of y
// yPtr “points to” y
a=5;
* ptr;
ptr=&a;

1. Name of variable : a
4. Name of variable: ptr
2. Value of variable: 5
5. Value of variable: 1025
3. Address: 1025 (assume)
6. Address: 5000 (assume)
Call by reference with pointer arguments
◦ Pass address of argument using & operator
◦ Arrays are not passed with & because the array
name is already a pointer
* operator
◦ Used as nickname for variable inside of function

void double( int *number )


{ *number = 2 * ( *number ); }
1 /* Example (2)
2 Notice that the function prototype takes
Cube a variable using call-by-reference Outline
a pointer to an integer (int *).
3 with a pointer argument */
4 Function prototype
5 #include <stdio.h>
6 Notice how the address of number
7 void cubeByReference( int * ); is given
/* - cubeByReference
prototype */
8 expects a pointer (an address of a
variable).
9 int main()
10 {
11 int number = 5; 1 Initialize variables
12
13 printf( "The original value of number is %d", number );
14 cubeByReference( &number ); 2. Call function
15 printf( "\nThe new value of number is %d\n", number );
16 Inside cubeByReference, *nPtr is
17 return 0;
used (*nPtr is number).
18 }
19
20 void cubeByReference( int *nPtr )
21 { 3. Define function
22 *nPtr = *nPtr * *nPtr * *nPtr; /* cube number in main */
23 }

The original value of number is 5


The new value of number is 125 Program Output
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int *ptr=( int *)1000; double *p=(double *)1000;
ptr=ptr+1; p=p+3;
printf(" %d",ptr);
printf(" %d",p);
return 0;
return 0;
}
}

Output: 1004
Output: 1024
#include<stdio.h> Difference= 2
int main() #include<stdio.h>
{ int main()
int *p=(int *)1000; {
int *temp; float *p=(float *)1000;
temp=p; float *q=(float *)2000;
p=p+2; printf("Difference= %d",q-p);
printf("%d %d\n",temp,p); return 0;
printf("difference= %d",p- }
temp); Output: Difference= 250
return 0;
}
Output: 1000 1008
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
operation Description

p++, p-- Increment (decrement) p to point to the next element,


it is equivalent to p+=1 (p -=1)
p+i (p-i) Point to i-th element beyond (in front of) p but value
of p is fixed
p[i] Equivalent to p + i

p + n (integer) n must be an integer, its meaning is offset

p-q Offset between pointer p and pointer q

p+q, p*q, p/q, p%q invalid

Relational operator of valid, including p > q, p < q, p == q,


two pointers p, q p >= q, p <= q, p != q
Pointer comparison ( <, == , > )
◦ See which pointer points to the higher numbered
array element (index)
◦ Also, see if a pointer points to 0

You might also like