0% found this document useful (0 votes)
5 views38 pages

Slide 8 - Pointer

The document provides a comprehensive overview of pointers in C programming, explaining their definition, declaration, and usage, including special operators like * and &. It covers pointer arithmetic, the relationship between pointers and arrays, and the concept of multiple indirection. Additionally, it highlights the importance of pointers in memory access, function value returns, and building complex data structures.

Uploaded by

shoumiksarker07
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)
5 views38 pages

Slide 8 - Pointer

The document provides a comprehensive overview of pointers in C programming, explaining their definition, declaration, and usage, including special operators like * and &. It covers pointer arithmetic, the relationship between pointers and arrays, and the concept of multiple indirection. Additionally, it highlights the importance of pointers in memory access, function value returns, and building complex data structures.

Uploaded by

shoumiksarker07
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/ 38

Pointers

Pointer Basics
 A pointer is a variable that holds the memory
address of another object.
 If a variable called p contains the address of another
variable called q, then p is said to point to q.
 Then if q is at location 100 (say) in memory, then p
would have the value 100.
Variable and Memory Add.
char q;
Variabl
RAM Value int a;
e
q 1024 A
unknown
q = ‘A’;
1025 unknown
1026 unknown a = 108;
a 108
Unknown
1027 unknown
1028 unknown
1029 unknown
How to declare?
 General form: type *var-name;
 Here, type is the base type of the pointer
 It specifies the type of the object that the
pointer can point to.
 The asterisk (*) tells the computer that a
pointer variable is being created.
 Example: int *p;
Variable and Memory Add.
char q;
Variabl
RAM Value char *p;
e
q 1024 A
unknown
q = ‘A’;
p 1025 unknown
1024
1026 unknown Want, p to point q
1027 unknown
1028 unknown
1029 unknown
Special Pointer Operator
 C contain two special pointer operator
 * and &
 The & operator return the address of the variable it
precedes.
 We can verbalize as address of
 The * operator returns the value stored at the
address that it precedes.
 We can verbalize as at address
Pointer - & operator
Variable and Memory Add.
char q;
Variabl
RAM Value Char *p;
e
q 1024 A
unknown
q = ‘A’;
p 1025 unknown
1024
P = &q;
1026 unknown
1027 unknown
1028 unknown
1029 unknown
We can verbalize & as address of
Example 1
We can verbalize * as at address

Defines two variable: p integer pointer and q is


integer
Assign q the value 199

Assign p the address of q

Print the value at address q

Print the value of p (it holding the address of q)


Example 2
Indirection
 When a variable’s value is referenced through a
pointer, the process is called indirection.

Location Contents
int *p, q; 100 (p) Unknown

104 (q) Unknown

Location Contents
p = &q; 100 (p) 104
p points to
104 (q) Unknown
q
Location Contents
*p = 100 (p) 104 p points to
199; 104 (q) 199 q
Importance
•Pointers provide direct access to memory

•Pointers provide a way to return more than one value to the functions

•Reduces the storage space and complexity of the program

•Reduces the execution time of the program

•Provides an alternate way to access array elements

•Pointers can be used to pass information back and forth between the
calling function and called function.

•Pointers helps us to build complex data structures like linked list, stack,
queues, trees, graphs etc.

•Addresses of objects can be extracted using pointers


Base type of a Pointer
 The base type of a pointer is very important.
 Though C allow any type of pointer to point
anywhere in memory, but the base type determine
how the object pointed to will be treated.
 The C compiler uses the base type to determine how
many bytes are in the object pointed to by the
pointer.
 This is how the compiler know how many bytes to
copy when an indirection assignment is made
Base type of a Pointer …
 Consider this fragment:
 not syntactically incorrect
 but wrong
 fp assign address of an integer
 this address try hold floating
point value
 ints are shorter than doubles
Pointer…
 If we attempt to use a pointer before it has been
assign the address of a variable - our program
probably crash.
Pointer…
 A pointer that contains a null value (zero) is assumed
to be unused and pointing at nothing.
 In C, a null is assumed to be an invalid memory
address.
Restrictions to Pointer
 In addition to “*” and “&” operators, it
supports only four other operators-

+ + - --
+
 We only can add or subtract integer
quantities.
Pointer Arithmetic
 Pointer arithmetic differs from “normal”
arithmetic.
 It is performed relative to the base type
Pointer Arithmetic

Variabl char *p;


RAM Value
e
p 1024 1025 char q;
q 1025 unknown p = &q;
1026 unknown
p++;
1027 unknown
1028 unknown p = ?;
Pointer Arithmetic

Variabl int *p;


RAM Value
e
p 1024 1028 int q;
q 1028 unknown p = &q;

p++;

p = ?;
Pointer Arithmetic

Variabl int *p;


RAM Value
e
p 1024 1028 int q;
q 1028 unknown p = &q;
P=p+
200;
p = ?;
Pointer Arithmetic

Variabl int *p;


RAM Value
e int q=10,
p 1024 1028
r=100;
q 1028 10 p = &q;
r 1032 100
*p++;

(*p)++;
*p++ vs ++*p vs *++p
 Precedence of prefix ++ and * is same. Associativity of
both is right to left.
 Precedence of postfix ++ is higher than both * and
prefix ++. Associativity of postfix ++ is left to right.
*p++ vs ++*p vs *++p
 The expression ++*p has two operators of same
precedence, so compiler looks for associativity.
Associativity of operators is right to left. Therefore the
expression is treated as ++(*p).
 The expression *p++ is treated as *(p++) as the
precedence of postfix ++ is higher than *.
 The expression *++p has two operators of same
precedence, so compiler looks for associativity.
Associativity of operators is right to left. Therefore the
expression is treated as *(++p).
Use Array with Pointer
 In C, pointers and arrays are closely related.

 They are often interchangeable

 This relationship between the two makes their


implementation both unique and powerful
Use Array with Pointer…
• Here what is being passed to gets() is not an
 When we use anbut
array array name without an
pointer
index, we are generating a pointer to the
• Actually we can’t pass an array to a
start of thefunction
array. in C we only pass a pointer
 gets(); • The gets() function uses the pointer to load
the array it points to with the characters we
enter at the keyboard (will see later)
Use Array with Pointer…
 Since the array name without an index is a pointer
to the start of the array.
 it is possible to assign that value to another
pointer and access the array using pointer
arithmetic.
 Multidimensional array?
Assign p the address of start of Similar as p =
a &q

This print the first, second and third elements of


array a

Print these element using array


index
Why?

 Here the parentheses in expressions such as *(p+1)


are necessary because the * has higher precedence
than the + operator
Pointer as Array!!!
 We can index a pointer as if it were an array

Keep one point firmly in your


mind:
We should index a pointer only
when that pointer points to an
array.
 We should index a pointer only when that pointer
points to an array.
 Consider the following fragment-

Wrong: Since ch is not an array. It


can't meaningfully indexed.
 We can use pointer arithmetic rather than array
indexing to access elements to the array.

• Remember! We can’t modify


the value of the pointer
generated by using an array
name.
• In this example str++ is
Array index or Pointer arithmetic?

 In some case C compiler can generate faster


executable code for an expression involving pointers
than for a comparable expression array.
Use pointer with String
 C allows string constants enclosed between double
quotes to be used in a program.
 Here the compiler store it in program’s string table
and generates a pointer to the string.
Use pointer with String…

Here we declare a character pointer


p.
This means that it may point to an array of characters.

It store the string in the program’s string


table and assigns to p the address of the
string table.
Create arrays of pointer
 int *p[2], q, z;
 int p[0] = &q;
 int p[1] = &z;
Multiple indirection
 When a pointer point to another pointer, it is called
multiple indirection.
 When a pointer points to another pointer, the first
pointer contain the address of the second pointer,
which is containing the location of the object.
 To declare a pointer to a pointer, an additional
asterisk is placed in front of the pointers name – char
**mp;
Multiple indirection

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy