Learning C by Example (2015)
3. Array and Pointer
This chapter explains how to work with array and Pointer.
3.1 Array
In this section, we build an array using C. For illustration, we develop single and multi dimensional array.
3.1.1 Defining An Array
We can define an array using [] syntax. For instance, we define array of int and char.
int numbers[5];
char chars[10];
We also can construct array from struct.
struct employee{
int id;
char name[10];
char country[5];
};
struct employee list[5];
3.1.2 Basic Array Operations
After declared an array, we can set and get data on array. For illustration, create a file, called arraydemo.c, and write this code.
#include <stdio.h>
struct employee{
int id;
char name[10];
char country[5];
};
int main() {
// define array
int numbers[5];
char chars[10];
struct employee list[5];
// insert data
int i;
for(i=0;i<5;i++){
numbers[i] = i;
list[i].id = i;
sprintf(list[i].name,"usr %d",i);
sprintf(list[i].country,"DE");
}
sprintf(chars,"hello c");
// display data
for(i=0;i<5;i++){
printf("%d %c\n",numbers[i],chars[i]);
printf("struct. id: %d, name: %s, country: %s \n",
list[i].id,list[i].name,list[i].country);
}
printf("%s\n",chars);
return 0;
}
Save this code and try to compile and run.
$ gcc -o arraydemo arraydemo.c
$ ./arraydemo
A sample output can be seen in Figure below.
3.2 Multidimensional Array
We can create multidimensional array, for instance two dimensional, we can use [][].
For testing, you can create a file, called multiarray.c and write this code.
#include <stdio.h>
int main() {
// define Multidimensional demenarray
int matrix[3][5];
// insert data
int i,j;
for(i=0;i<3;i++){
for(j=0;j<5;j++){
matrix[i][j] = i+j;
}
}
// display data
for(i=0;i<3;i++){
for(j=0;j<5;j++){
printf("%d ",matrix[i][j]);
}
printf("\n");
}
return 0;
}
Save this. Then, try to compile and run.
$ gcc -o multiarray multiarray.c
$ ./multiarray
A sample output can be seen in Figure below.
3.3 Pointer
A pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its address. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer (source: http://en.wikipedia.org/wiki/Pointer_(computer_programming) ).
To obtain memory address of a variable, we can use & syntax. For instance, create a file, called address.c, and write this code.
#include <stdio.h>
int main(int argc, const char* argv[]) {
int n;
n = 10;
printf("value n: %d \n",n);
printf("address n: %x \n",&n);
return 0;
}
Save this file. Then, try to compile and run.
$ gcc -o address address.c
$ ./address
If success, we obtain memory address of variable n. A sample output of program can be seen in Figure below.
3.3.1 Basic Pointer
To declare a pointer of a specific data type, we can use * syntax. This variable consists of memory address of our pointer variable.
For illustration, create a file, called pointer.c, and write this code.
#include <stdio.h>
int main(int argc, const char* argv[]) {
int n;
int* nPtr;
n = 10;
nPtr = &n;
printf("value n: %d \n",n);
printf("address n: %x \n",(unsigned int)&n);
printf("value nPtr: %x \n",(unsigned int)nPtr);
printf("address nPtr: %x \n",(unsigned int)&nPtr);
printf("value pointer nPtr: %d \n",*nPtr);
return 0;
}
Save this file. Then, compile and run this file.
$ gcc -o pointer pointer.c
$ ./pointer
You can see a value of nPtr is memory address of variable n. If n is set value 10, then value of pointer nPtr which we declare as *nPtr is the same value with value of variable n.
A sample of program output is shown in Figure below.
3.3.2 Dynamic Array
In this section, we will create a dynamic array using pointer. In general, we can declare an array using [] with size. Now we can declare our array with dynamic size.
Basically, when we add a new value, we allocate memory and then attach it to array. For illustration, we define array with size 10. Then, we add some values to this array.
Create a file, called dpointer.c, and write this code.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char* argv[]) {
// define dynamic array of pointer
int *numbers; // single array pointer
// a number of array
int N = 10;
// allocate memory
numbers = malloc( N * sizeof(int));
// set values
int i;
for(i=0;i<N;i++){
numbers[i] = i+3;
}
// display values
for(i=0;i<N;i++){
printf("%d ",numbers[i]);
}
printf("\n");
// free memory
free(numbers);
return 0;
}
Save this code. Try to compile and run it.
$ gcc -o dpointer dpointer.c
$ ./dpointer
A program output can be seen in Figure below.
Another sample, we also define dynamic array with multidimensional. For instance, we create two dimensional dynamic array. Create a file, called dtwopointer.c, and write this code.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char* argv[]) {
// define dynamic array of pointer
int **matrix; // two dimensional array pointer
// a number of array
int M = 3;
int N = 5;
// allocate memory
matrix = malloc( M * sizeof(int*));
// set values
int i,j;
for(i=0;i<M;i++){
matrix[i] = malloc( N * sizeof(int));
for(j=0;j<N;j++){
matrix[i][j] = i + j;
}
}
// display values
for(i=0;i<M;i++){
for(j=0;j<N;j++){
printf("%d ",matrix[i][j]);
}
printf("\n");
}
// free memory
free(matrix);
return 0;
}
Save this code. Compile and run this file.
$ gcc -o dtwopointer dtwopointer.c
$ ./dtwopointer
A sample output is shown in Figure below.