Pointers versus arrays
Lots of similarities
How to deal with 2D, 3D, multidimensional arrays (for storing matrices and other 2D or 3D data!)
Pointers are variables that store memory addresses
int a = 15; int *b = &a; printf("%p %p %d\n", b, &b, *b);
Address | Memory | Name |
---|---|---|
0x7ffd80b3ff7c | 15 | a |
0x7ffd80b3ff80 | 0x7ffd80b3ff7c | b |
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);
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)); }
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 memory | 0x7f340 | 0x7f344 | 0x7f348 | 0x7f34c | 0x7f350 | 0x7f354 | 0x7f358 | 0x7f35c | 0x7f360 |
Grades | 74 | 59 | 95 | 85 | 71 | 45 | 99 | 82 | 76 |
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
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)); }
The array name is a pointer to the first element of the array
0x7f344 | 0x7f348 | 0x7f34c | 0x7f350 | |
list--> | 1 | 2 | 3 | 4 |
[0] | [1] | [2] | [3] |
int list[] = {1, 2, 3, 4}; printf("%p, %p, %d", list, &list[0], *list);
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"
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
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));
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
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);
*(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
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));
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); }
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); }