Fundamentals of C Programming Simple data structures Pointers
Transcript of Fundamentals of C Programming Simple data structures Pointers
![Page 1: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/1.jpg)
Fundamentals of C Programming
Simple data structures
Pointers
![Page 2: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/2.jpg)
Simple Data Structures
Arrays
Structures
Unions
![Page 3: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/3.jpg)
EEL 3801 – Lotzi Bölöni
Simple Data Structures
It would be limiting to have to express all
data as variables.
It would be desirable to be able to group
data into sets of related data.
This can be done two main ways:
–arrays (all data of the same type)
–structures (data may be of different types).
![Page 4: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/4.jpg)
EEL 3801 – Lotzi Bölöni
Type Definitions
Special data types designed by the
programmer can be defined through the typedef keyword.
For example, if we want to define a data
type that is to be defined only once and
then used thereafter:
typedef unsigned long int Myvar
![Page 5: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/5.jpg)
EEL 3801 – Lotzi Bölöni
Type Definitions
So, Myvar can now be used to indicate an
unsigned long int wherever used.
Myvar n;
is the same as:
unsigned long int n;
It can be used as a form of code
customization and aids in making it more
meaningful and readable.
![Page 6: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/6.jpg)
EEL 3801 – Lotzi Bölöni
Arrays
Left-most symbol indicates the name of the
array. This is common for all its elements.
Individual data identified by distance from
first one in array.
Within square brackets is the cell number
(how many cells away from the first one).
Individual cells can be used as regular
variables.
![Page 7: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/7.jpg)
EEL 3801 – Lotzi Bölöni
Arrays
-45
6
0
72
1543
-89
0
62
-3
c[0]
c[1]
c[2]
c[3]
c[4]
c[5]
c[6]
c[7]
c[8]
For array c:
![Page 8: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/8.jpg)
EEL 3801 – Lotzi Bölöni
Declaring Arrays
The declaration allows the compiler to set
aside sufficient contiguous memory for
the size of array
The type of data to be stored must be
identified so that sufficient space is
allocated.
Arrays allocated statically - remain the
same size throughout program execution.
![Page 9: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/9.jpg)
EEL 3801 – Lotzi Bölöni
Declaring Arrays
int c[12];
float a[100];
char b[15];
Can be automatic or external.
Size typically done through a macro.
#define SIZE 10
![Page 10: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/10.jpg)
EEL 3801 – Lotzi Bölöni
Initializing Arrays
Not automatically initialized. Can be
initialized during declaration or within the
program in a loop.
int n[10] = {32,27,64,18,95,14,90,70,60};
If more elements than initialized, others = 0.
If less elements than initialized - error.
int n[] = {32,27,64,18,95,14,90,70,60,37};
![Page 11: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/11.jpg)
EEL 3801 – Lotzi Bölöni
Passing Arrays to Functions
Arrays passed by reference - actual variable
address passed. The called function can
modify the original array’s values.
Pass name without brackets.
Include the size of the array as a passed
value.
Function header and prototype must indicate
that an array is being passed.
![Page 12: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/12.jpg)
EEL 3801 – Lotzi Bölöni
Passing Arrays to Functions
#define SIZE 5
void function1(int [],int);
void function2(int);
main()
{
int a[] = {0, 1, 2, 3, 4};
function1(a,SIZE);
function2(a[3]);
}
![Page 13: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/13.jpg)
EEL 3801 – Lotzi Bölöni
Multi-dimension Arrays
Arrays can have an arbitrary number of
dimensions.
Indicated by multiple bracket pairs.int a[5][10];
int b[10][12][20];
Can be called in same way as vector arrays.
First bracket is the row script
Second is the column script.
![Page 14: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/14.jpg)
EEL 3801 – Lotzi Bölöni
Initializing Multi-dim. Arrays
Initialization by row in braces.
First brace equates to first row, 2nd to 2nd,.
int c[2][2] = {{1,2} {3,4}};
Initializes b[0][0]=1, b[0][1]=2, b[1][0]=3, and b[1][1]=4.
But what if int c[2][2] = {{1} {3,4}};
Initializes b[0][0]=1, b[0][1]=0 , b[1][0]=3, and b[1][1]=4.
![Page 15: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/15.jpg)
EEL 3801 – Lotzi Bölöni
Arrays and Strings
Strings are in reality arrays of characters
Each element contains one character.
Each cell is one byte in size.
More about strings and string operations
later.
![Page 16: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/16.jpg)
EEL 3801 – Lotzi Bölöni
Structures
A collection of related, but dissimilar
variables under one name.
Provides great flexibility that an array does
not.
Used to define records to be stored in files.
Also used to form dynamic data types such
as linked lists, linked stacks and linked
queues.
![Page 17: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/17.jpg)
EEL 3801 – Lotzi Bölöni
Structure Definitions
Declared as follows: struct planet {
char *name;
int nummoons;
double dist_from_sun;
float dist_from_earth;
};
This creates a definition of the structure.
planet is the structure tag.
![Page 18: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/18.jpg)
EEL 3801 – Lotzi Bölöni
Structures Components
The variables are called members.
They can be accessed individually using:
–The structure member operator (also called the
dot operator).
–The structure pointer operator (also called the
arrow operator).
See Figure 10.2, page 411 of Paul and
Harvey Deitel “C How to program” book,
7th edition .
![Page 19: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/19.jpg)
EEL 3801 – Lotzi Bölöni
Structure Operators
The dot operator accesses the contents of
the member using the member name and
the structure variable name.
planet.nummoons
directly accesses the contents of the member nummoons
Can be used as a regular integer variable.
![Page 20: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/20.jpg)
EEL 3801 – Lotzi Bölöni
Structure Operators
The arrow operator accesses the contents of
the member using a pointer to the structure
variable and the member name.
planet_ptr->nummoons
directly points to the contents of the member nummoons
equal to (*planet_ptr).nummoons
![Page 21: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/21.jpg)
EEL 3801 – Lotzi Bölöni
Structure Variables
The struct keyword defines a “model” of
the desired structure.
It is not a real variable per se.
A real variable is created by creating an
instance of the structure model.
Also referred to as “instantiating”.
![Page 22: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/22.jpg)
EEL 3801 – Lotzi Bölöni
Structure Variables
To make instances of the definition:
–Instance name(s) can be added after the
definition.
–Can be defined as a data type to be instantiated
separately.
–The struct keyword can be used along with
the tag to instantiate.
See examples next.
![Page 23: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/23.jpg)
EEL 3801 – Lotzi Bölöni
Structure Variables
Instances added after the definition:
struct planet {
char *name;
int nummoons;
double dist_from_sun;
float dist_from_earth;
} earth, mars, solar[9], *ptr;
solar[9] is an array of 9 structures of type
planet. ptr is a pointer to a planet type.
![Page 24: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/24.jpg)
EEL 3801 – Lotzi Bölöni
Structure Variables
The tag is optional. The following code is
equivalent to the one in the last slide:
struct {
char *name;
int nummoons;
double dist_from_sun;
float dist_from_earth;
} earth, mars, solar[9], *ptr;
Only way to instantiate is in the definition.
![Page 25: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/25.jpg)
EEL 3801 – Lotzi Bölöni
Structure Variables
Defined as a datatype:
typedef struct planet Planet;
Planet earth, mars;
Planet *ptr;
Planet solar[9];
This assumes that the structure definition
is as before.
![Page 26: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/26.jpg)
EEL 3801 – Lotzi Bölöni
Structure Variables
Can also be done directly in the definition:
typedef struct planet {
char *name;
int nummoons;
double dist_from_sun;
float dist_from_earth;
} Planet;
The planet tag is not necessary in
this case.
![Page 27: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/27.jpg)
EEL 3801 – Lotzi Bölöni
Structure Variables
The struct keyword can also be used to
instantiate.
struct planet {
char *name;
int nummoons;
double dist_from_sun;
float dist_from_earth;
};
struct planet earth;
![Page 28: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/28.jpg)
EEL 3801 – Lotzi Bölöni
Initializing Structure Members
Like in arrays.
Use values inside braces.
Only when variable being instantiated.
struct planet earth = {earth,1,1.0e+6,0}
If less values than members, then only the
first few are initialized. Others = 0.
Must be constant values or expressions.
![Page 29: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/29.jpg)
EEL 3801 – Lotzi Bölöni
Structures and Functions
Structures can be passed to functions as:
–Individual structure members.
–An entire structure variable.
–Pointer to a structure variable.
Passed by value if the individual structure
member or the entire structure is passed.
Passed by reference if a pointer to the
structure is passed.
![Page 30: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/30.jpg)
EEL 3801 – Lotzi Bölöni
Structures
Arrays can be assigned to a structure
member.
There can be arrays of structures.
Structure members can be other
structures.
Structure members can be self-
referencing structures - pointers that point
to similar structures as itself.
![Page 31: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/31.jpg)
EEL 3801 – Lotzi Bölöni
Unions
Same as structures, except members
share same storage space.
Saves space when some members are
never used at the same time.
Space for a member must be large enough
to accommodate the largest of the data
types to be stored in that member.
![Page 32: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/32.jpg)
EEL 3801 – Lotzi Bölöni
Unions
Unions are declared and defined in a way
similar to structures.
The keyword union replaces the keyword
struct.
Not highly recommended except when
memory management is critical.
![Page 33: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/33.jpg)
EEL 3801 – Lotzi Bölöni
Enumeration Constants
Allows a set of integer constants to be
represented by identifiers.
Symbolic constants whose value can be
set automatically.
Values start with 0 (unless otherwise
noted by programmer) and are
incremented by 1.
Uses the enum keyword for definition.
![Page 34: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/34.jpg)
EEL 3801 – Lotzi Bölöni
Enumeration Example
#include <stdio.h>
enum months {JAN=1, FEB, MAR, APR, MAY,
JUN, JUL, AUG, SEP, ACT, NOV, DEC}
main()
{
enum months month;
char *monthName[] = {“”,
“January”,..};
for(month=JAN;month<=DEC;month++)
printf(……….monthName[month];
}
![Page 35: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/35.jpg)
Pointers
![Page 36: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/36.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Variables
Conventional variables contain values.
Pointer variable contains memory address
of variable that contains values (or
pointers)
Allows call by reference.
Permits creation of dynamic data
structures.
Permits dynamic allocation of memory.
Difficult to understand and use.
![Page 37: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/37.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Variables
Conventional variable names directly
reference a value.
Pointer variables indirectly reference a
value
Referencing a value through a pointer
variable is called indirection.
Pointer variables = pointers
![Page 38: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/38.jpg)
EEL 3801 – Lotzi Bölöni
Declaration of Pointer Variables
Pointers must be declared like regular
variables.
It must be stated which type of variable
they point to.
Declarations use * to indicate
“pointerhood”
int *ptr;
pointer ptr points to an integer variable.
![Page 39: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/39.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Variables
Pointers should be initialized.
Can be set to NULL or to 0, but NULL is
preferred.
NULL is a symbolic constant defined in
<stdio.h>
Pointers assigned a value of 0 actually have
the value 0 and not an address.
![Page 40: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/40.jpg)
EEL 3801 – Lotzi Bölöni
Address-of Pointer Operator
Address-of operator (&) is a unary
operator returning the address of its
operand.
The basic operator used to assign values
to pointers.int y = 5;
int *ptr;
ptr = &y;
ptr points to y (contains its address).
![Page 41: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/41.jpg)
EEL 3801 – Lotzi Bölöni
Indirection Pointer Operator
Indirection operator (*), or dereferencing
operator is also unary and returns the value
of the variable pointed at by the pointer.
In the previous example:
y = 5
*ptr = 5
Not to be confused with the declaration
operator - very confusing!!!.
![Page 42: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/42.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Example
main()
{
int a;
int *aptr;
a = 7;
aptr = &a;
printf(“The address of a =%d“,&a);
printf(“The value of aptr =%d“,
aptr);
printf(“The value of a = %d“,
*aptr);
}
![Page 43: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/43.jpg)
EEL 3801 – Lotzi Bölöni
Call by Value
Data passed by value is– copied from the variable used in main() to a
variable used by the function
– it’s stored in the local variable inside the
function
– it’s value is only changed in the variable used
inside the function modified inside the function
![Page 44: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/44.jpg)
EEL 3801 – Lotzi Bölöni
Call by Value - Example
void value_funct(int);
main()
{
int n = 5;
printf(“Original value = %d\n“, n);
value_funct(n);
printf(“New value = %d\n“, n);
}
void value_funct(int n);
{
n = n * n;
}
![Page 45: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/45.jpg)
EEL 3801 – Lotzi Bölöni
Call by Value - Example
Original value = 5
New value = 5
The call to function value_funct did not
change the original variable number in
main().
![Page 46: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/46.jpg)
EEL 3801 – Lotzi Bölöni
Call by Reference with Pointers
By passing a variable’s address to a
function, we give that function the ability
to modify the value of the original value.
This is referred to as a call by reference.
![Page 47: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/47.jpg)
EEL 3801 – Lotzi Bölöni
Call by Reference - Example
void value_funct2(int *);
main()
{
int number = 5;
printf(“Original value =“, number);
value_funct(&number);
printf(“New value =“, number);
}
void value_funct(int *nptr);
{
(*nptr) = (*nptr) * (*nptr);
}
![Page 48: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/48.jpg)
EEL 3801 – Lotzi Bölöni
Call by Reference - Example
Original value = 5
New value = 25
The call to function value_funct
changed the original variable number in
main().
A similar effect can be obtained by value_funct returning a value to main()
![Page 49: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/49.jpg)
EEL 3801 – Lotzi Bölöni
Functions Returning Pointers
Functions can also return pointers to
variables.
int* function1(int, int);
is the prototype for a function that returns
a pointer to an integer variable.
Is easily done by simply returning the
value of a pointer variable - an address.
![Page 50: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/50.jpg)
EEL 3801 – Lotzi Bölöni
The const and Pointer Passing
The const qualifier tells the compiler that
the variable following it is not to be
changed by any program statements.
Provides a measure of security when
passing addresses of variables whose
values are not to be modified (for
example, arrays).
When passing pointers, 4 possibilities
exist:
![Page 51: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/51.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Passing
Non-constant pointer to non-constant data
–Declaration does not include const in any way.
–Data can be modified through the pointer.
–Pointer can be modified to point to other data.
Highest level of data access to called
function.
This is what we have been doing up to
now.
![Page 52: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/52.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Passing
Non-constant pointer to constant data:
–Pointer can be modified to point to any data.
–Data that it points to cannot be modified
–May be used to protect the contents of a
passed array.
–Read as “a is a pointer to an integer constant”
void funct(const int *a)
![Page 53: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/53.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Passing
Constant pointer to non-constant data:
–Pointer always points to same memory
location.
–Data that it points to can be modified.
–Default value for a passed array.
–Pointer must be initialized when declared.
–Read “aptr is a constant pointer to an integer ”
int x;
int * const aptr = &x;
![Page 54: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/54.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Passing
Constant pointer to constant data:
–Pointer always points to same memory
location.
–Data that it points to cannot be modified.
–Read “aptr is a constant pointer to an integer
constant” - right to left
int x = 5;
const int* const aptr = &x;
![Page 55: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/55.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Arithmetic
Pointers are valid operands in
mathematical operations, assignment
expressions and comparison operations.
But not all operators are valid with
pointers.
Operators that are do not always work the
same way.
![Page 56: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/56.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Arithmetic
A pointer can be incremented (++).
A pointer can be decremented (--).
An integer may be added to, or subtracted
from a pointer (+, +=, -, -=).
One pointer may be subtracted from
another.
But this can be misleading.
![Page 57: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/57.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Arithmetic
When adding integers to pointers, the
value of the integer added is the number
of memory elements to be moved.
The actual answer depends on the type of
memory element being pointed to by the
pointer.
Assuming int = 4 bytes (32 bits):
![Page 58: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/58.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Arithmetic
int *yptr = 3000;
yptr += 2;
In reality, yptr = 3008, because 2*4=8 bytes.
In other words, the pointer moved two
integer data “spaces” away from its original
address.
Since an integer data space is 4 bytes, it
moved 8 bytes.
![Page 59: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/59.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Arithmetic
Since character variables are 1 byte in
size, the arithmetic will be normal for
pointers that point to characters.
The ++ and -- operators work the same
way.
They add one data space to the address.
int *ptr = 3000;
ptr++;
ptr = 3004, assuming integer takes 4
bytes.
![Page 60: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/60.jpg)
EEL 3801 – Lotzi Bölöni
Pointer Arithmetic
Subtraction works the same way.
int x;
x = v1ptr - v2ptr;
where v1ptr=3008 and v2ptr=3000;
==> x = 2 if int is 4 bytes.
![Page 61: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/61.jpg)
EEL 3801 – Lotzi Bölöni
Pointers and Arrays
The name of an array is in reality a pointer
to its first element.
Thus, for array a[] with, for instance, 10
elements, a = &(a[0]).
This is why when an array is passed to a
function, its address is passed and it
constitutes call by reference.
![Page 62: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/62.jpg)
EEL 3801 – Lotzi Bölöni
Pointers and Arrays
a[3] can be also referenced as *(a+3).
The 3 is called the offset to the pointer.
Parenthesis needed because precedence
of * is higher than that of +.
Would be a[0]+3 otherwise.
a+3 could be written as &a[3].
See Fig. 7.20, page 284 in textbook.
![Page 63: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/63.jpg)
EEL 3801 – Lotzi Bölöni
Pointers and Arrays
The array name itself can be used directly
in pointer arithmetic as seen before.
Pointer arithmetic is meaningless outside
of arrays.
You cannot assume that a variable of the
same type will be next to a variable in
memory.
![Page 64: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/64.jpg)
EEL 3801 – Lotzi Bölöni
Pointers and Strings
Strings are really pointers to the first
element of a character array.
Array is one character longer than the
number of elements between the quotes.
The last element is “\0” (the character with
the ASCII code zero).
![Page 65: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/65.jpg)
EEL 3801 – Lotzi Bölöni
Arrays of Pointers
Arrays may contain nearly any type of
variable.
This includes pointers.
Could be used to store a set of strings.
char *suit[4] = {“hearts”, “diamonds”, “spades”,
“clubs”};
The char * says that the elements of the
array are pointers to char.
![Page 66: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/66.jpg)
EEL 3801 – Lotzi Bölöni
Arrays of Pointers
H e a r t s \0
D i a m o n d s \0
C l u b s \0
S p a d e s \0
Suit[0]
Suit[1]
Suit[2]
Suit[3]
![Page 67: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/67.jpg)
EEL 3801 – Lotzi Bölöni
Pointers to Functions
Contains address of the function in
memory.
This is now addressing the code segment.
Can be
–passed to functions
–returned from functions
–stored in arrays
–assigned to other function pointers
![Page 68: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/68.jpg)
EEL 3801 – Lotzi Bölöni
Pointers to Functions
Pointer contains the address of the first
instruction that pertains to that function.
Commonly used in menu-driven systems,
where the choice made can result in
calling different functions.
Two examples follow:
![Page 69: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/69.jpg)
EEL 3801 – Lotzi Bölöni
Example 1
Writing a sorting program that orders an
array of integers either in ascending or
descending order.
main() asks the user whether ascending or
descending order, then calls the sorting
function with the array name, its size and
the appropriate function (ascending or
descending).
See Fig. 7-26, page 292 in textbook.
![Page 70: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/70.jpg)
EEL 3801 – Lotzi Bölöni
Example 1 - continued
int ascending(int,int);
int descending(int,int);
void sort(int *, const int,
int (*)(int,int));
main()
{
. . .
sort(array,10,ascending); or
sort(array,10,descending);
. . .
}
![Page 71: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/71.jpg)
EEL 3801 – Lotzi Bölöni
Example 1 - continued
void sort(int *arr, const int size, int
(*compare_func) (int, int));
{
if ((*compare_func)(arr[i], arr[i+1]))
do something;
}
int ascending(const int a, const int b)
{
return b < a;
}
![Page 72: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/72.jpg)
EEL 3801 – Lotzi Bölöni
Example 1 - continued
main() calls sort() and passes to it the
array, its size, and the function to be used.
sort() receives the function and calls it
under a pointer variable compare_func,
with two arguments.
The arguments are elements of the array, arr[i] and arr[i+1].
compare_func returns 1 if true,0 if false.
![Page 73: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/73.jpg)
EEL 3801 – Lotzi Bölöni
Example 2
Functions are sometimes represented as an
array of pointers to functions.
The functions themselves are defined as
they would normally.
An array of pointers is declared that
contains the function names in its elements.
Functions can be called by dereferencing a
pointer to the appropriate cell.
![Page 74: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/74.jpg)
EEL 3801 – Lotzi Bölöni
Example 2 - continued
void function1(int);
void function2(int);
void function3(int);
main()
{
void (*f[3])(int) = {function1,
function2, function3};
}
“f is an array of 3 pointers to
functions that take an int as an argument and return void”
![Page 75: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/75.jpg)
EEL 3801 – Lotzi Bölöni
Example 2 - continued
Such functions can be called as follows:
(*f[choice]) (choice));
Can be interpreted as calling the contents of the address located in the choice cell of
array f, with an argument equal to the value
of the integer variable choice.
The parenthesis enforce the desired
precedence.
![Page 76: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/76.jpg)
EEL 3801 – Lotzi Bölöni
Double Pointers
Double pointers are commonly used when
a call by reference is desired, and the
variable to be modified is itself a pointer.
A double pointer is a pointer to a pointer
to a variable of a particular type.
Declared as int **ptr
Read as a pointer to a pointer to an
integer.
![Page 77: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/77.jpg)
EEL 3801 – Lotzi Bölöni
Double Pointers
int
ptr
![Page 78: Fundamentals of C Programming Simple data structures Pointers](https://reader033.fdocuments.in/reader033/viewer/2022050104/626c8dde07222f5c6925fc94/html5/thumbnails/78.jpg)
EEL 3801 – Lotzi Bölöni
Double Pointers
Deferencing a double pointer results in an
address.
Derefencing it again results in the value of
the ultimate variable
var = *(*dbl_ptr);