C programming: Strings, arrays, and pointers.
Maxwell Githinji
Posted on May 20, 2024
In this read, we are going to explore strings, arrays, and pointers. Additionally, we are going to write a block of C code script to help visualize these concepts.
Strings
Unfortunately, unlike other programming languages like Python, Ruby, JavaScript, etc. C does not offer a direct approach to initializing a string. This would ideally mean that it does not have a data type string, instead, it has a data type character. This means that a string in C is mostly initialized as an array of characters or directly using pointers. To initialize a strings, you can follow the below guide or example:
// using Array to initialize an Array
char students[ ] = { "Max", "Brian", "Steve" };
// using Array to initialize a pointer
char *students = { "Max", "Brian", "Steve" };
Arrays
In C, an array can simply be defined as a set of elements of the same data type. Examples of how to initialize a C array can be done as follows:
data_type array_name [size] = {value1, value2, value3, ...};
// example, let's initialize an array with the name num and it holds 10 numbers
int num[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// example, let's initialize the same array named num but without determining the array size.
int num[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// example, let's initialize an array with float data types
float temperatures[5] = {98.6, 99.5, 100.2, 97.8, 101.3};
// example, let's initialize an array of characters.
char name[3] = "Max";
char nameTwo = "Brian";
// example, let's initialize an array with the name students and it holds names of students as 2D array of characters.
char students[3][10] = {"Brian", "Max", "Steve"};
To access elements in an array is important as it allows you to decide what you want to do with the data stored in these arrays - this may range from displaying the data to mutating the data or the array as a whole. For this article, we will not delve into using loops to access values or data used in arrays, instead, the example below will offer a basic example that shows how to access an array.
#include <stdio.h>
int main() {
// Declare an array of integers with 5 elements
int myArray[5] = {10, 20, 30, 40, 50};
// Access and print elements of the array
printf("Element 0: %d\n", myArray[0]); // Output: Element 0: 10
printf("Element 1: %d\n", myArray[1]); // Output: Element 1: 20
printf("Element 2: %d\n", myArray[2]); // Output: Element 2: 30
printf("Element 3: %d\n", myArray[3]); // Output: Element 3: 40
printf("Element 4: %d\n", myArray[4]); // Output: Element 4: 50
return 0;
}
Pointers
In C, pointers are used to store other variables. These variables can have different data types ranging from integers to floats to char ( characters ). Apart from variables, pointer can also hold arrays, function, and other pointer as well.
To initialize poiters to store various data variables as well as an array, you can follow the examples below:
void intPointers(void) {
// Initialize an integer, float, and char variable
int num = 10;
float myFloat = 10.1;
char character = "A";
char name[] = "Max";
int arr[3] = {1, 2, 3};
// Initialize pointers to store the addresses of the variables
int *numPointer = # // Pointer to an integer
float *myFloatPointer = &myFloat; // Pointer to a float
char *myCharacterPointer = character; // Pointer to a character
char *myNamePointer = name; // Pointer to a char array (string)
char *myArrayPointer = arr; // Pointer to an int array
// Initialize an array of strings as a pointer directly
char *students = { "Max", "Brian", "Steve" };
}
If you would like to initialize a pointer that holds to call to an already initialized function, you can follow the example below:
void helloWord(void) {
printf("Hello world");
}
int main(void) {
void (*func_ptr)() = helloWorld; // Pointer to a function
func_ptr(); // Calling the function through the pointer
return 0;
}
Lastly, to store another pointer within another pointer, you can approach it as follows:
void intPointers(void) {
// Initialize an integer, float, and char variable
int num = 10;
// Initialize pointer for the variable num
int *numPointer = # // Pointer to an integer
// Initialize pointer for the pointer
int *pointerForMyPointer = &numPointer;
}
Posted on May 20, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.