Pointer and Array

Pointer and Array

Pointers versus arrays

Lots of similarities

How to deal with 2D, 3D, multidimensional arrays (for storing matrices and other 2D or 3D data!)

Pointer and Array

Review: Pointers

Pointers are variables that store memory addresses

int a = 15;
int *b = &a;
printf("%p %p %d %p %d\n", b, &b, *b, &a, a);
				
AddressMemoryName
0x7ffd80b3ff7c15a
0x7ffd80b3ff800x7ffd80b3ff7cb
Pointer and Array

Using Pointers in scanf function

int a;
int* p = &a;
printf("Enter an integer: ");
scanf("%d", &a); //Read & as "at"
printf("a = %d, *p = %d\n", a, *p);
printf("Enter another integer: ");
scanf("%d", p); //Don't have to put & before p. It's alreadly an "at"
printf("a = %d, *p = %d\n", a, *p);
			
Pointer and Array

Passing pointers (addresses) to functions

int multiply(int *a, int factor){
	return (*a) * factor;
}
void main(){
	int number = 3;
	int *p = &number;
	printf("1: %d\n", multiply(&number, 2));
	printf("2: %d\n", multiply(p, 3));
}
			
Pointer and Array

Review: Arrays

An array is a contiguous chunk of memory to store multiple values

int grades[] = { 74, 59, 95, 85, 71, 45, 99, 82, 76 };

Address in memory0x7f3400x7f3440x7f3480x7f34c0x7f3500x7f3540x7f3580x7f35c0x7f360
Grades745995857145998276
index012345678
Pointer and Array

Passing arrays to functions

int sum(int list[], int size){
	int s = 0;
	for(int i = 0; i < size; i++){
		s += list[i];
	}
	return s;
}
void main(){
	int list[] = {1, 2, 3, 4};
	printf("Sum = %d\n", sum(list, 4));
}
			
Pointer and Array

Array Name

The array name is a pointer to the first element of the array

0x7f3440x7f3480x7f34c0x7f350
list →1234
[0][1][2][3]
int list[] = {1, 2, 3, 4};
printf("%p %p, %p, %d", &list, list, &list[0], *list);
			
Pointer and Array

Pointers and Arrays

You can use a pointer to access the array

int *p;
int list[] = {1, 2, 3, 4};
p = list; //equivalent to p = &list[0];
printf("%d\n", *p);//prints the value "1"
			
Pointer and Array

Pointer and []

Any pointer to a block of memory can use the [] syntax, even if it is not declared as an array

int *p;
int list[] = {1, 2, 3, 4};
p = list;/*int *v, and int v[]; //mean the same thing */
printf("%d\n", p[2]);//prints 3
			
Pointer and Array

Array indexing []

Indexing an array is just a way finding a particular address in that block

int list[] = {1, 2, 3, 4};
printf("%d", list[2]);
//this is equivalent to
printf("%d", *(list + 2));
			
Pointer and Array

Pointer Arithmetic

When we add to a pointer, such as (p + 1), we don't literally add 1 to the pointer address

Instead we add one "address" to the pointer

int list[] = {1, 2, 3, 4};
int *p = list; //same as p = &list[0];
printf("%p", p);
p = p + 1; //p increase by 4
printf("%p\n", p);
			

Think of pointer arithmetic as add 1 "location" instead of one byte or address

Pointer and Array

Pointer Arithmetic

double list[] = {1.0, 2.0, 3.0 };
double *p = list;
printf("%p\n", p);
p = p + 1; //p increases by 8 bytes
printf("%p\n", p);
			
Pointer and Array

Pointer Arithmetic on Arrays

*(list+1) references the next element in the array (equivalent to list[1])

Be careful: *(++list) works too but now we have lost out pointer to the beginning of the array!!!

Equivalent to: list = list + 1, *list

Pointer and Array

sizeof() operator

returns the number of bytes need to store a variable or a data type

int i;
int* pi = &i;
int a1[] =  {1, 2, 3, 4, 5};
double j;
double *pj = &j;
double a2 = {1.0, 2.0, 3.0, 4.0, 5.0 };
printf("Sizeof integer is %d bytes\n", sizeof(int));
printf("Sizeof double is %d bytes\n", sizeof(double)); 
printf("Sizeof i is %d bytes\n", sizeof(i)); 
printf("Sizeof pointer for i is %d bytes\n", sizeof(pi)); 
printf("Sizeof j is %d bytes\n", sizeof(j)); 
printf("Sizeof pointer for j is %d bytes\n", sizeof(pj)); 
printf("Sizeof a1 is %d bytes\n", sizeof(a1)); 
printf("Sizeof a2 is %d bytes\n", sizeof(a2)); 
			
Pointer and Array

When we pass an array

When we pass an array, we are passing the array address

int sumArray(int a[], int n){
	int s = 0;
	for(int i = 0 ; i < n; i++)
		s += a[i];
	return s;
}
void main(){
	int list[] = {1, 2, 3, 4};
	sumArray(list, 4);
}
			
Pointer and Array

When we pass an array

This will work too (because array name is a pointer to the beginning of the array)

int sumArray(int *a, int n){
	int s = 0;
	for(int i = 0 ; i < n; i++)
		s += a[i];
	return s;
}
void main(){
	int list[] = {1, 2, 3, 4};
	sumArray(list, 4);
}
			
Pointer and Array

When we pass an array

But this does not work

int sumArray(int *a, int n){
	int s = 0;
	for( int i = 0; i < n; i++){
		s += a[i];
	}
	return s;
}
void main(){
	int *list = {1, 2, 3, 7};
	...
	sumArray(list, 4);
	...
}
			
Pointer and Array

Indexing an array is just a way of finding a particular address in that block

int list[] = {3, 3, 1, 5};
printf("%d\n", list[2]);
//This is equivalent to
printf("%d\n", *(list + 2));
			
Pointer and Array

2-D arrays

int array[arraySize][arraySize];
			
Column
0123
Row01234
15678
29101112
313141516
Pointer and Array

2-D arrays

int array[arraySize][arraySize]= { 
	{1, 2, 3, 4}, 
	{5, 6, 7, 8}, 
	{9, 10, 11, 12}, 
	{13, 14, 15, 16} 
};
				
Column
0123
Row01234
15678
29101112
313141516
Pointer and Array

Physically, in one block of memory

int array[arraySize][arraySize]= { 
	{1, 2, 3, 4}, 
	{5, 6, 7, 8}
};
				
ffe2de0cffe2de10ffe2de14ffe2de18ffe2de1cffe2de20ffe2de24ffe2de28
12345678
row 1row 2

Array elements are stored in row major order

Row 1 first, followed by row2, row3, and so on

Pointer and Array

2D Array Name and Addresses

int array[arraySize][arraySize]= { 
	{1, 2, 3, 4}, 
	{5, 6, 7, 8}
};
				
ffe2de0cffe2de10ffe2de14ffe2de18ffe2de1cffe2de20ffe2de24ffe2de28
12345678
row 1row 2

array: pointer to the first element of the 2D array

array[0]: pointer to the first row of the 2D array

array[1]: pointer to the second row of the 2D array

*array[1] is the address of element array[1][0]

Pointer and Array

Accessing 2D Array Elements

int array[arraySize][arraySize]= { 
	{1, 2, 3, 4}, 
	{5, 6, 7, 8}
};
				
ffe2de0cffe2de10ffe2de14ffe2de18ffe2de1cffe2de20ffe2de24ffe2de28
12345678
row 1row 2

Indexing: array[i][j] is same as:

*(array[i] + j)
(*(array+i))[j]
*((*(array+i)))+j
*(&array[0][0] + 4 *i + j)