Pointers, Arrays, Multidimensional Arrays

Pointers, Arrays, Multidimensional Arrays

Pointers versus arrays

Lots of similarities

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

Pointers, Arrays, Multidimensional Arrays

Review: Pointers

Pointers are variables that store memory addresses

int a = 15;
int *b = &a;
printf("%p %p %d\n", b, &b, *b);
				
AddressMemoryName
0x7ffd80b3ff7c15a
0x7ffd80b3ff800x7ffd80b3ff7cb
Pointers, Arrays, Multidimensional Arrays

Using Pointers in scanf function

int a;
int* p = &a;
printf("Enter an integer: ");
scanf("%d", &a); //Read & as "at"
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);
			
Pointers, Arrays, Multidimensional Arrays

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));
}
			
Pointers, Arrays, Multidimensional Arrays

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
Pointers, Arrays, Multidimensional Arrays

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));
}
			
Pointers, Arrays, Multidimensional Arrays

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, %d", list, &list[0], *list);
			
Pointers, Arrays, Multidimensional Arrays

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];
pirntf("%d\n", *p);//prints the value "1"
			
Pointers, Arrays, Multidimensional Arrays

Pointer and []

Array pointer to a block of memory can use the [] symtax, 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", [2]);//prints 3
			
Pointers, Arrays, Multidimensional Arrays

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));
			
Pointers, Arrays, Multidimensional Arrays

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

Pointers, Arrays, Multidimensional Arrays

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);
			
Pointers, Arrays, Multidimensional Arrays

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

Pointers, Arrays, Multidimensional Arrays

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)); 
			
Pointers, Arrays, Multidimensional Arrays

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);
}
			
Pointers, Arrays, Multidimensional Arrays

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);
}
			
Pointers, Arrays, Multidimensional Arrays

Pointers, Arrays, Multidimensional Arrays

Pointers, Arrays, Multidimensional Arrays