|
|
Consider the previous example on accessing a (dynamic) array:
#include <stdio.h> #include <stdlib.h> int main( int argc, char *argv[] ) { int *A; int i; A = malloc( 10*sizeof(int) ); // Create a dynamic array for ( i = 0; i < 10; i++ ) *(A + i) = i*i; // Assigns i^2 to A[i] for ( i = 0; i < 10; i++ ) printf("%d\n", *(A + i) ); } |
We can re-write the example using the [ ] operator as follows:
#include <stdio.h> #include <stdlib.h> int main( int argc, char *argv[] ) { int *A; int i; A = malloc( 10*sizeof(int) ); // Create a dynamic array for ( i = 0; i < 10; i++ ) A[i] = i*i; // Assigns i^2 to A[i] for ( i = 0; i < 10; i++ ) printf("%d\n", A[i] ); } |
Now the dynamic array looks like an "ordinary" array !!!
Consider a static array B[ ]:
#include <stdio.h> #include <stdlib.h> int main( int argc, char *argv[] ) { int B[5] = {3, 5, 1, 2, 4}; int i; for ( i = 0; i < 5; i++ ) printf("%d\n", B[i] ); } |
Let's store the base address of array B[ ] in the reference variable A:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
int B[5] = {3, 5, 1, 2, 4};
int *A;
int i;
for ( i = 0; i < 5; i++ )
printf("%d\n", B[i] );
A = &B[0]; // A has the base address of array B[ ]
}
|
Result: A[i] is an alias for B[i] for any value i:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
int B[5] = {3, 5, 1, 2, 4};
int *A;
int i;
for ( i = 0; i < 5; i++ )
printf("%d\n", B[i] );
A = &B[0]; // A has the base address of array B[ ]
for ( i = 0; i < 5; i++ )
A[i] = i*i; // A[i] ≡ B[i] !!
for ( i = 0; i < 5; i++ )
printf("%d\n", B[i] );
}
|
|
In the previous example, instead of &B[0], we can use B :
#include <stdio.h> #include <stdlib.h> int main( int argc, char *argv[] ) { int B[5] = {3, 5, 1, 2, 4}; int *A; int i; for ( i = 0; i < 5; i++ ) printf("%d\n", B[i] ); A = &B[0]; // A has the base address of array B[ ] for ( i = 0; i < 5; i++ ) A[i] = i*i; // A[i] ≡ B[i] !! for ( i = 0; i < 5; i++ ) printf("%d\n", B[i] ); } |
Resulting program:
#include <stdio.h> #include <stdlib.h> int main( int argc, char *argv[] ) { int B[5] = {3, 5, 1, 2, 4}; int *A; int i; for ( i = 0; i < 5; i++ ) printf("%d\n", B[i] ); A = B ; // A has the base address of array B[ ] for ( i = 0; i < 5; i++ ) A[i] = i*i; // A[i] ≡ B[i] !! for ( i = 0; i < 5; i++ ) printf("%d\n", B[i] ); } |
Recall how we defined functions with array as parameter: ( click here )
#include <stdio.h> int sumArray( int A[], int n /*array length*/ ) { int i, s; s = 0; for (i = 0; i < n; i++) s += A[i]; return(s); } int main(int argc, char *argv[]) { int B[10] = {1, 3, 5, 7, 9, 1, 1, 2, 1, 2}; int r; r = sumArray(B, 10); printf("r = %d\n", r); } |
We are passing B which has the type reference to int
An array parameter defined as int A[ ] in C can also be defined as int *:
#include <stdio.h> int sumArray( int *A , int n /*array length*/ ) { int i, s; s = 0; for (i = 0; i < n; i++) s += A[i]; // A[i] ≡ *(A+i) !! return(s); } int main(int argc, char *argv[]) { int B[10] = {1, 3, 5, 7, 9, 1, 1, 2, 1, 2}; int r; r = sumArray(B, 10); printf("r = %d\n", r); } |
So when you see a int * typed parameter, it could also be an array parameter in C!!!