C Programming

How to Create an Array of Strings Using Malloc() in C Programming

C programming language is one of the famous structured languages that includes many basic components, and arrays are one of them. Arrays are referred to as a collection of similar types of items stored in contiguous memory blocks. These are of two types: static array and dynamic array. In the static array, the size of the array is fixed. But in a dynamic array, memory is allocated dynamically.

This post will demonstrate:

Prerequisite: Install C Compiler

C Compiler is utilized to transform source code into binary or object code or to execute the C program. Therefore, to install the C compiler on Windows, go check out our other dedicated article.

What is “malloc()” in C Programming?

The array in C programming has a defined size. However, occasionally the array size can be insufficient or excessive, which wastes memory. To overcome array limitations, developers use several functions to allocate memory dynamically to the array. Various C library functions, such as free(), calloc(), realloc(), and malloc(), are used to assign memory dynamically. More specifically, the malloc() is a memory allocation function used to reserve memory for a specified number of bytes.

Syntax

pointer = (cast-type*) malloc(size of byte)

How to Create an Array of Strings Using “malloc()” in C Programming?

To create an array of strings and assign it a memory block through the “malloc()” function, look at the provided example.

Step 1: Create an Array of String Using “malloc()” Function

To create an array of strings using the C standard library function “malloc()”, first, open the Visual Studio Code Editor on Windows and paste the provided code into a program file having the “.c” extension:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main(void)
{
int size,i;
printf("Please type the size of array: \n");
scanf("%d",&size);
char *str_array[size];
char array_i[size];
printf("Enter the strings:\n");
for(i=0;i<size;i++)
{

scanf("%s", array_i);
str_array[i]= (char*)malloc(strlen(array_i)*sizeof(char));
strcpy(str_array[i],array_i);
}

printf("Your strings were: \n");
for(i=0;i<size;i++)
 {
  printf("%s\n", str_array[I]);
 }
return 0;
}

The description of the above-mentioned code is given below:

  • The “h” library is used for memory allocation, the “string.h” is utilized for performing the operation related to string manipulation, and “stdio.h” is used for including the input/output stream.
  • Next, we have defined the “int main(void)” for declaring the function without any argument. Here, the “void” parameter specifies that no return value is required.
  • Then, we have declared two variables. The “size” is used to get the total number of string entries, and “i” is used for loop iteration. Both variables are of “int” data type.
  • The “scanf” is used to get or read input stream from the keyboard.
  • Then, we initialized two arrays of the “char” type. The first array is a pointer array used to point and store strings. The second array is a simple “char” array used in the “malloc” function to accept a string from the user.
  • The “strcpy” function is utilized to copy the string. It is included in the program through the “h” library.
  • We have applied the “malloc()” function of the “char*” type cast. It is used to allocate memory dynamically. The “malloc()” function multiplies the character’s size by a number of character bytes to allocate sufficient memory blocks.
  • The pointer array “str_array[i]” is used to point and store strings in the array.
  • Lastly, the “for” loop is used to iterate the array. After that, we printed the array using the “printf” function.

Step 2: Open Terminal

Next, launch the terminal from the Visual Studio menu bar:

Step 3: Compile C Program

Next, utilize the “gcc” C compiler to compile the program using the mentioned command:

>gcc Malloc.c

Here, “Malloc.c” is the C program for which we have provided instructions in step 1:

Upon doing so, the compiler will create an executable file to run the C program as shown below:

Step 4: Execute Compiled Program

Next, run the C program by executing the “a.exe” file:

> ./a.exe

It can be observed that we have successfully created a dynamic array of strings using the “malloc()” function.

Conclusion

To create an array of strings using the “malloc()” C standard function, first create a simple C program and declare two arrays, one of which is a pointer array. After that, utilize the “malloc()” function by using the “pointer-array = (cast-type*) malloc(input-array*size of char)” syntax. Next, copy the string arrays using the “strcpy” function. This post has demonstrated the method for creating a string array using “malloc()” in C programming.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.