The C Pointer Arithmetic
An integer can be added to a pointer to get another address. For example,
int array[100]; int *ptr1 = array; int *ptr2; ptr2 = ptr1 + 5;
The integer 5 is depend on the variable type of ptr1. According to the above statements, ptr1 is an integer, the byte size of an integer is 4. Therefore 5 time 4 equals 20. The address of ptr1 plus 20 is the address for ptr2.
As the last post stated, a pointer is the address of first element of an array. This means that ptr1 is the address of first element of array and ptr2 is the 6th element of array:
ptr2 = &(ptr1[5]); // this is the same as below ptr2 = ptr1 + 5;
Here are the sample statements based on these two similar statements:
int x[10]; // or int *x; // The following two statements are completely interchangeable x[10]; *(x + 10); // or (these two are also interchangeable) &(x[10]); x + 10;
Note that the subtraction of an integer from a pointer is exactly the same as the addition which we talked about above.
Here is the sample code for the pointer arithmetic:
/*
*
* arithmetic.c
*
* Program to demonstrate the use of pointer arithmetic
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
#include <stdlib.h>
static void goodbye() {
while (getchar() != 'n') {}
printf("nPress ENTER to exit: ");
fflush(stdin);
getchar();
}
main() {
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr = array;
int element;
atexit(goodbye);
printf("Please enter which element you would like: ");
scanf("%d", &element);
// The output for the following statements are the same, all print out the value for the specified element of the array
printf("narray[%d] is %dn", element, array[element]);
printf("*(array + %d) is %dn", element, *(array + element));
printf("*ptr[%d] is %dn", element, ptr[element]);
printf("*(ptr + %d) is %dn", element, *(ptr + element));
// The following statements print out the memory address for the specified element of the array
printf("n&(ptr[%d]) is %dn", element, &(ptr[element]));
printf("ptr + %d is %dn", element, ptr + element);
// This prints out the address difference between two elements of the array through subtraction
printf("nThe ADDRESS difference between element 9 and element 1 is %dn", &(ptr[9]) - &(ptr[1]));
}
C Arrays
An array is a variable that represents a collection of the same variable types. For instance, a collection of doubles is an array. It can also declare one variable as an array. The size of an array depend on the memory size of the target computer. It can store larger array size if the memory size is bigger.
Below is the arrays declaration format:
typename arrayname[init_literal];
As you remember from previous posts, this format is the same as when declaring strings. A string is a collection of characters, which also is an array.
After an array was declared, each element in the array can be accessed individually. For example:
floatarray[5] = 3.56;
Like other programming languages, the first element of an array in C always starts with 0. Therefore for the above example, floatarray[5] refers to the sixth element of the array.
Arrays are often used with loops to operate with all elements sequentially, especially the for loop. When used inside a loop, an array of n elements always stars from 0 to n – 1. For instance, an array of 10 elements would start from 0 to 9 inside a loop.
Here is the sample code for the arrays:
/*
*
* array.c
*
* Program to illustrate the use of arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
float array[5];
int i, index;
for (i = 0; i < 5; ++i) {
printf("Please enter element number %d: ", i + 1);
scanf("%f", &array[i]);
while (getchar() != 'n' && getchar() != EOF)
printf("");
}
printf("Which element do you want to see? ");
scanf("%d", &index);
while (getchar() != 'n' && getchar() != EOF)
printf("");
printf("Element %d is %fn", index, array[index-1]);
fflush(stdin);
getchar();
}
The following sample code demonstrates how to use strings with arrays:
/*
*
* string.c
*
* Program to illustrate the makeup of a string
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char name[51];
int length;
printf("Please enter your name: ");
scanf("%[^n]", name);
while (getchar() != 'n' && getchar() != EOF)
printf("");
for (length = 0; name[length] != ''; length++) {}Â Â Â // do nothing
printf("The length of >>>%s<<< is %dn", name, length);
fflush(stdin);
getchar();
}
Notice the name[length] != ” statement. The slash zero inside the quotes represents a NULL character. It means that the loop will end if there is no more character to read.
Multi-dimensional Arrays
A multi-dimensional Array is an array made of multiple arrays. Here is an example:
/*
*
* multi.c
*
* Program to demonstrate multi-dimensional arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
int grid[8][3];Â Â Â // It contains 8 arrays and each array has 3 elements
int i, j;
for (i = 0; i < 8; i++) {
for (j = 0; j < 3; j++) {
grid[i][j] = (i + 1) * (j + 1);
}
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 3; j++) {
printf("%3d ", grid[i][j]);
}
printf("n");
}
fflush(stdin);
getchar();
}
And here is the sample code for the string usage in a multi-dimensional array:
/*
*
* multi2.c
*
* Program to demonstrate multi-dimensional arrays
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char strings[5][20];Â Â Â // It contains 5 strings and each has 20 characters long
int i;
for (i = 0; i < 5; i++) {
printf("Please enter string %d: ", i + 1);
scanf("%s", strings[i]);
while (getchar() != 'n' && getchar() != EOF) {}
}
for (i = 0; i < 5; i++) {
printf("String %d is %sn", i + 1, strings[i]);
}
fflush(stdin);
getchar();
}
Array Initialization
Arrays can be initialized at the beginning of the program when they are declared. During the initialization, it’s unneccessay to specify the size of the array. For instance, here is the declaration of an array:
int array[40];
And here is the initialization of an array when it is declared:
int array[] = {3, 6, 9, 12};
Note that it’s also possible to initialize the array when the size of the array is specified, but as stated above, it’s not necessary. For example:
int array[10] = {3, 6, 9, 12};
In the above example, the first four elements in the array got initialized, while the rest of the elements were set to 0.
Here is the sample code to show how the multi-dimensional arrays are initialized:
/*
*
* multi3.c
*
* Program to demonstrate initializing multi-dimensional arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char grid[8][8] =
{
{'r', 'h', 'b', 'k', 'q', 'b', 'h', 'r'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'r', 'h', 'b', 'k', 'q', 'b', 'h', 'r'}
};
int i, j;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
printf("%c", grid[i][j]);
}
printf("n");
}
fflush(stdin);
getchar();
}
And here is for the multi-dimensional arrays for strings:
/*
*
* multi4.c
*
* Program to demonstrate multi-dimensional arrays
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char name[50] = "Mark Virtue";
char strings[][20] = {"This", "is", "how", "to", "initialize"};
int i;
for (i = 0; i < 5; i++) {
printf("Please enter string %d: ", i + 1);
scanf("%s", strings[i]);
}
for (i = 0; i < 5; i++) {
printf("String %d is %sn", i + 1, strings[i]);
}
}
Note that strings in the multi-dimensional arrays can only have one set of arrays that has non-specified size, notably the arrays in the first dimension. The size of arrays in other dimensions needs to be specified.