Arrays and Strings in C

An array is an indexed list of variables of the same type that are referred to by a shared name. A specific variable in the array is accessed via its index number. In C, all arrays consist of contiguous memory locations, which means that we can also access array elements via pointer arithmetic. The lowest address stores the first array element, and the highest address stores the last element. Suffice to say, arrays and pointers are closely related.

One common type of array is the string, which is a char array terminated by the null character.

As with other variables, arrays must be declared so that the compiler can allocate space for them in memory.

#include <stdio.h>

int main(void){
    
    
    //declare double type array
    double bubble[10];
    
    //declare int type array
    int intArray[50];
    
    bubble[0] = 7.976;
    bubble[1] = 2.352;
    
    printf("bubble[0] = %f\n", bubble[0]);
    printf("bubble[1] = %f\n", bubble[1]);
    
    return 0;
    
}

As we have seen, an element is accessed by indexing the array name, which is done by placing the number of the element’s location in the array within square brackets after the name of the array. Note that in C, all arrays have 0 as the index of their first element.

#include <stdio.h>

int main(void){
    
    int ints[50];
    
    int i;
    
    //assign values to
    //every element in ints array
    for(i = 0; i < 50; i++){
        ints[i]=i+1;
    }
    
    //display contents of array
    for(i=0; i < 50; i++){
        printf("%d\t", ints[i]);
    }
    
    putchar('\n');
    
    return 0;
    
}

Note that C has no bounds checking on arrays.

The amount of memory required to hold an array is directly related to its type and size. For a single-dimension array, the total size in bytes is the length of the array multiplied by the size of the base type of the array.

We can create a pointer to the first element in an array by specifying the array name without any sort of index.

#include <stdio.h>

int main(void){
    
    int i = 0;
    int *ptr;
    int sample[25];
    
    ptr = sample;
    
    sample[0] = 1;
    for(i = 1; i < 25; i++){
        sample[i] = i + sample[i-1];
    }
    
    i = 0;
    
    //use pointer arithmetic
    //to access array elements
    while(i++ < 25){
        printf("%d\n", *ptr++);
    }
    
    return 0;
}

We cannot pass an entire array as an argument to a function. We can, however, pass a pointer to an array by specifying the array’s name without an index. We can declare the array as a function parameter in three different ways: as a pointer, as a  sized array, or as an unsized array.

#include <stdio.h>
#define SIZE 50

//two void functions
//one to assign values
//one to print values
void assignValues(double array[SIZE]);
void printValues(double *arrayPtr);

//one void function to modify values
void modifyValues(double array[]);

int main(void){
    
    double trouble[SIZE];
          
    assignValues(trouble);
    
    printValues(trouble);
    
    printf("\nafter modifying values in array...\n");
                
    modifyValues(trouble);
    
    printValues(trouble);
    
    return 0;
    
}

void assignValues(double array[SIZE]){
    
    int i;
    double var = 0.0;
    
    for(i = 0; i < SIZE; i++){
            var = i / 1000.0;
            array[i] = (i-(SIZE/2)) + var;
        }
}

void printValues(double *arrayPtr){
    int i = 0;
    while(i < SIZE){
        printf("%.8f\n", *(arrayPtr + i));
        i++;
    }
}

void modifyValues(double array[]){
    int i;
    for(i = 0; i < SIZE; i++){
        array[i] = array[i] / (i+1) + i;
    }
}

One dimensional arrays are often used as strings. A string is a null-terminated character array. Note that when declaring a character array that will hold a string, we must declare it to be one character longer than the largest string that it will hold.

#include <stdio.h>


int main(void){
    
    int i;
    char lowerCase[27];
    char upperCase[27];
    
    lowerCase[0] = 'a';
    upperCase[0] = 'A';
    
    for(i = 1; i < 26; i++){
        lowerCase[i] =  lowerCase[0] + i;
        upperCase[i] = upperCase[i-1] + 1;
    }
    
    //assign null terminator
    //to ends of the string
    lowerCase[26] = '';
    upperCase[26] = '';
    
    //print strings using
    // %s conversion specifier
    printf("Lower Case Letters: %s\n", lowerCase);
    printf("Upper Case Letters: %s\n", upperCase);
    
    
    return 0;
}

We can use the fgets() function to acquire a string from standard input. The fgets() function takes three arguments: the char array, the size of the char array, and where to get the input from. Since we will be taking the input from the user, we will specify stdin as the source of input.

#include <stdio.h>

int main(void){
    
    const int buffer = 256;
    
    char string1[buffer];
    char string2[buffer];
    
    printf("Enter a string: ");
    
    fgets(string1, buffer, stdin);
    
    printf("Enter another string: ");
    
    fgets(string2, buffer, stdin);
    
    printf("%s", string1);
    
    printf("%s", string2);

    return 0;
    
}

Interested in learning more?

Take a look at this book on C: http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M/

Advertisements