C Programming Arrays Strings Pointers
C Programming Arrays Strings Pointers
Introduction
This guide covers Arrays (1D & 2D), Strings, and Pointers in C programming, explained in a simple
way with examples from basic to hard, to help you score full marks in exams.
1D Arrays
An array is a collection of similar data types stored in contiguous memory locations.
Example:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
2D Arrays
A 2D array is an array of arrays, similar to a matrix.
Example:
#include <stdio.h>
int main() {
int matrix[2][2] = {{1, 2}, {3, 4}};
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Strings
A string in C is an array of characters ending with a null character '\0'.
Example:
#include <stdio.h>
int main() {
char str[] = "Hello";
printf("%s", str);
return 0;
}
Pointers
A pointer is a variable that stores the address of another variable.
Example:
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("Value: %d, Address: %p", *p, p);
return 0;
}