Module 4 & 5
Module 4 & 5
1
Array in C
Array in C is one of the most used data structures in C programming.
It is a simple and fast way of storing multiple values under a single name.
int main()
{
return 0;
}
Syntax:
data_type array_name [size] = {value1, value2, ... valueN};
Example:
int main()
{
Example:
array_name [index];
Syntax of 1D Array in C
array_name [size];
In C, we store the words, i.e., a sequence of characters in the form of an array of characters terminated by a
NULL character. These are called strings in C language.
// printing string
int i = 0;
while (arr[i]) {
printf("%c", arr[i++]);
}
return 0;
}
Dept. of Mechanical Engineering 9
2. Multidimensional Array in C
Multi-dimensional Arrays in C are those arrays that have more than one dimension. Some of the
popular multidimensional arrays are 2D arrays and 3D arrays.
Syntax:
data_type array_name[rows][columns];
Example
int twodimen[4][3];
Initialization of an array:
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Example
1.#include<stdio.h>
2.int main(){
3.int i=0,j=0;
4.int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
5.//traversing 2D array
6.for(i=0;i<4;i++){
7. for(j=0;j<3;j++){
8. printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
9. }//end of j
10.}//end of i
11.return 0;
12.}
Syntax of 3D Array in C
Disadvantages of Array in C
1.Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked
list, an array in C is not dynamic.
2.Insertion and deletion of elements can be costly since the elements are needed to be rearranged after insertion
and deletion.
Dept. of Mechanical Engineering 18
Strings in C
A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C
String is stored as an array of characters. The difference between a character array and a C string is
that the string in C is terminated with a unique character ‘\0’.
char string_name[size];
C String Initialization
We can also assign a string character by character. But we should remember to set the end character as ‘\0’ which is a
null character.
#include <stdio.h>
#include <string.h>
int main()
{
// declare and initialize string
char str[] = "Geeks";
// print string
printf("%s\n", str);
int length = 0;
length = strlen(str);
return 0;
}
Dept. of Mechanical Engineering 22
C String Length
The length of the string is the number of characters present in the string except for the NULL character. We can
easily find the length of the string using the loop to count the characters from the start till the NULL character is
found.
int main()
{
// declare and initialize string
char str[] = "GeeksforGeeks";
return 0;
} Dept. of Mechanical Engineering 24
Difference between char array and string literal
There are two main differences between char array and literal.
•We need to add the null character '\0' at the end of the array by ourself whereas, it is appended internally
by the compiler in the case of the character array.
•The string literal cannot be reassigned to another set of characters whereas, we can reassign the
characters of the array.
int main(void)
{
char str[128];
return 0;
}
return 0;
} Dept. of Mechanical Engineering 30
A. Pointer Declaration
To declare a pointer, we use the (*) dereference operator before its name. In pointer declaration, we only declare the
pointer but do not initialize it.
B. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer variable. We use the (&) addressof
operator to get the memory address of a variable and then store it in the pointer variable.
Note: We can also declare and initialize the pointer in a single step. This is called pointer definition.
C. Pointer Dereferencing
Dereferencing a pointer is the process of accessing the value stored in the memory address specified in the pointer. We
use the same (*) dereferencing operator that we used in the pointer declaration.
•%p format specifier is used to print the address stored in pointer variables.
•Printing a pointer with %d format specifier may result in a warning or undefined behaviour because the size of a
pointer (usually 4 or 8 bytes) may not match that of an integer.
•The memory address format will always be in hexadecimal format(starting with 0x).
•C does not use the term “reference” explicitly (unlike C++), “referencing” in C usually refers to obtaining the address
of a variable using the address operator (&).
•Pointers are essential for dynamic memory allocation, providing control over memory usage with functions
like malloc, calloc, and free.
The struct keyword is used to define the structure in the C programming language.
The items in the structure are called its member and they can be of any valid data
type. Additionally, the values of a structure are stored in contiguous memory
locations
You can create a structure by using the struct keyword and declare each of its members
inside curly braces:
struct myStructure {
int myNum;
char myLetter;
};
int main() {
struct myStructure s1;
return 0;
}
int main() {
// Create a structure variable of myStructure called s1
struct myStructure s1;
// Print values
printf("My number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);
return 0;
} Dept. of Mechanical Engineering 39
Dept. of Mechanical Engineering 40