To convert an array of strings to an array of integers in C, you can use the `atoi` function from the `stdlib.h` library to parse each string element into an integer.
In this example,
strArray
containing string representations of integers.intArray
with the same length as strArray
.strArray
into an integer using the atoi
function.intArray
to demonstrate the conversion.#include <stdio.h>
#include <stdlib.h>
int main() {
char *strArray[] = {"10", "20", "30"};
int intArray[sizeof(strArray) / sizeof(strArray[0])];
for (int i = 0; i < sizeof(strArray) / sizeof(strArray[0]); i++) {
intArray[i] = atoi(strArray[i]);
}
for (int i = 0; i < sizeof(intArray) / sizeof(intArray[0]); i++) {
printf("%d ", intArray[i]);
}
return 0;
}
10 20 30
In this tutorial, we learned How to Convert an Array of Strings to an Array of Integers in C language with well detailed examples.