Pointers and Arrays
4th session
Example
Suggest three different ways
to make an infinite loop
which way is the best ?!
Get your hands dirty
Think OF
Swap Function
void swap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
Pointers
Memory
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
// *y = 13;
y = x;
*y = 13;
}
Pointers
• Variable whose value is a memory address
• Pointers are represented internally as unsigned int on most
systems
• %p (ANSI C)
Pointer Fun
https://www.youtube.com/watch?v=5VnDaHBi8dM
void swap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
Think oF
scanf("%d", &x)
why?
<data type> *ptr = <address>
Referencing and dereferencing
referencing :
&<variable name>
dereferencing:
*<pointer name>
Variables VS Pointers
Arrays
Arrays
• Series of elements of one data type
• int grades[50];
• student[index] = 85;
Arrays Initialization
• int arr[4] = {11, 2, 4, 6};
• int arr[4];
• int arr[4] = {11, 2}; // " The rest are Zeros "
• int arr[ ] = {11, 2, 4, 6};
• int arr[150] = {[48] = 2, 4}; // " The rest are Zeros " C99
• int arr[ ] = {2, [7] = 3, 9};
How to get the size of an array
sizeof(array) / sizeof(array[0])
Often LOOPS for assigning arrays
Make sure you are within bounds
Function call
All this data is pushed into
the stack when calling
Function call
Memory addresses goes
downwards
Function call
All this data is pushed into
the stack when calling
Specifying an Array Size
• <Data Type> <Array Name>[int literal];
• int arr[<variable>]; //Not allowed before C99
Multidimensional Arrays
Two-dimensional Arrays
• double x[5] [7];
• An array of arrays
• int arr[2][3] = {
{ 1, 3, 5 },
{ 4, 7, 8 }
}
Arrays and Pointers
Pointers and Arrays
• Pointers and arrays are tightly coupled
• An array name is actually the address of the first element of the
array
• *(arr + i) is what is really happening
Functions with Arrays as inputs
Pointer Operations
• Assignment
• referencing and dereferencing
• Adding, subtracting an int , increment and decrement
• &ptr
• (ptr1 - ptr2)
• Comparisons
Cautions
• Exceeding the array boundary
• Dereferencing an uninitialized pointer
Pointer Arithmetic
Protecting Data Passed to Functions
Complicated Declarations
• int *arr [ ] ;
• float rain [5] [12];
• const double *p;
• double * const p;
• const double * const p;
Pointer to constants
Vs
Constant Pointers
Pointers and Const
• Only The addresses of non-constant data can be assigned to
regular pointers
• with const you can create a pointer to constant, a constant
pointer and a constant pointer to constant
Thanks !

Pointers_c