Arrays


    
    

Table of Contents

  1. Arrays
  2. Structures
  3. Strings

Arrays

Arrays are used to store multiple values in a single variable.

Define an Array

Syntax1 : array_type array_name[] = {content1, content2, content3};

int myNumber[] = {1,2,3,4,5};

Syntax2: array_type array_name[array_size];

int yourNumber[6];


    
Access and Set the Values of Elements of an Array

To access an array's element, we need to refer to its index number. Index number 0 is for the first element, index number 1 is for the second element, and so on.

Syntax: array_name[index_number] = new_value;
Example:

myNumber[0] = 10;

If the array's elements are still not defined like in the yourNumber array in syntax2 in defining array, then the system will automatically set the values according to the new value.

Example:

#include <stdio.h> int main() { int myNumber[] = {25, 50, 75, 100}; int yourNumber[4]; //yourNumber doesn't contain any value //you can't change the size of the array after creation myNumber[0] = 30; //change the value 25 to 30 //myNumber will contain 30,50,75,100 now yourNumber[0] = 40; yourNumber[1] = 50; yourNumber[2] = 45; yourNumber[3] = 55; //yourNumber will contain 40,50,45,55 now return 0; }
Loop through an Array

You can loop through the array elements with the for loop. for loop will be explained in the conditional and loops page.

Example:

#include <stdio.h> int main() { int myNumbers[] = {25, 50, 75, 100}; int i; for (i = 0; i < 4; i++) { printf("%d\n", myNumbers[i]); } return 0; }

Structures

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. The variables in struct can have several different data types.

To access the structure, you must create a variable of it. Use the struct keyword inside the main() method, followed by the name of the structure and then the name of the structure variable:

Syntax:

struct struct_name{ Variable_type1 variable_name1; Variable_type2 variable_name2; … }; int main(){ Struct struct_name structure_variable_name; }

    

To access the members of a structure, you have to use dot (.) in int main() after you declare the name of the structure variable.

Syntax: structure_variable_name.variable_name1 = value;

For string, as it is an array of characters, when we want to access them, we have to use strcpy instead of just assigning it with the operator =. To use the function strcpy(), we need to include the header file <string.h>.

Syntax:

#include <string.h> strcpy(structure_variable_name.variable_name1, "text");

We can also assign values to multiple variables at once with this syntax.

Syntax: struct struct_name structure_variable_name = {value1, value2, …, valuen};

We can also assign the values of a structure variable to another structure variable.

Example:

struct struct_name structure_variable_name1 = {value1, …}; struct struct_name structure_variable_name2; structure_variable_name2 = structure_variable_name1;

Example:

#include <stdio.h> struct Phone { int price; char brand[10]; int year; }; int main() { struct Phone phone1 = {5000, "Aster", 2019}; struct Phone phone2 = {3000, "Bianca", 2022}; struct Phone phone3 = {2000, "Charlie", 2021}; struct Phone phone4; phone4 = phone3; //assign the value of phone3 to phone4 phone4.year = 2022; //change phone4's year to 2022 printf("%d %s %d\n", phone1.price, phone1.brand, phone1.year); printf("%d %s %d\n", phone2.price, phone2.brand, phone2.year); printf("%d %s %d\n", phone3.price, phone3.brand, phone3.year); printf("%d %s %d\n", phone4.price, phone4.brand, phone4.year); return 0; }

    

Output:

5000 Aster 2019 3000 Bianca 2022 2000 Charlie 2021 2000 Charlie 2022

Strings

C does not have a string type variable, so we have to use array of characters when we want to make a string variable.

Example:

char variable_name1[] = "text"; char variable_name2[] = {'t', 'e', 'x', 't', '\0'};

Every C string has a null character inside them so the size of the string “some text” is not 9, but instead 10. We can use sizeof(variable_name) in the printf statement to know the array size. If we assign it like how we assign variable_name2, then we have to assign a null character too, so the first way is easier.

Because a string in C is an array of characters, to modify strings, you can use the same method when we modify the array earlier. You can also loop through strings with for loops because they are arrays.

Syntax:

char variable_name1[] = "some text"; Variable_name1[index_number] = 'value';

    

Example:

#include <stdio.h> int main(){ char string1[] = {'s', 'o', 'm', 'e', ' ', 't', 'e', 'x', 't', '\0'}; char string2[] = "some text"; string2[1] = 'a'; //changing o in some to a printf("%s\n", string1); for (int i = 0; i<sizeof(string2)-1; ++i) { printf("%c,", string2[i]); } printf("\nSize of string1 is %lu\n", sizeof(string1)); printf("Size of string2 is %lu\n", sizeof(string2)); return 0; }

Output:

some text s,a,m,e, ,t,e,x,t, Size of string1 is 10 Size of string2 is 10