3
#include<stdio.h>

#include<stdlib.h>

#define LENGTH 5 

void convert ( char parray[] ,int array[] )

{

    int i; 
    for (i=0; i< LENGTH; i++)
    {
        array[i] = atoi(&parray[i]);
        printf(" The converted array is %d\n" , array[i]);
    }
}

int main ()

{

    char parray[LENGTH] = { '7', '1', '4','5' ,'2'};

    int iarray[LENGTH];

    convert(parray, iarray);
}

******** Output *********

The converted array is 71452

The converted array is 1452

The converted array is 452

The converted array is 52

The converted array is 2

But I want the following output like this

The converted array is 7

The converted array is 1

The converted array is 4

The converted array is 5

The converted array is 2

It should store value of 7 in array[0] , 1 in array[1] !!!! Please help

4
  • 2
    I tried it gives me error Mr.LSerni : error: passing argument 1 of ‘atoi’ makes pointer from integer without a cast [-Werror=int-conversion] array[i] = atoi(parray[i]); Commented Jun 17, 2021 at 12:40
  • 1
    1) Strings need to be null terminated. 2) atoi translates the whole string to an integer, not digit by digit. Commented Jun 17, 2021 at 12:41
  • Sorry, I had not looked carefully. I thought it was an array of a different nature. The correct answer has already been given by MikeCAT. Commented Jun 17, 2021 at 12:42
  • Thank you Mr.LSerni and Mr.Lundin for your time Commented Jun 17, 2021 at 12:54

3 Answers 3

4

atoi() is for converting strings (sequences of characters terminated by a null-character) to integers. To convert single character to an integer, you can subtract '0' (the character code of 0) from the character because it is guaranteed in C specification that character codes for decimal digits are continuous.

void convert ( char parray[] ,int array[] )

{

    int i; 
    for (i=0; i< LENGTH; i++)
    {
        array[i] = parray[i] - '0';
        printf(" The converted array is %d\n" , array[i]);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think that 60k user should look for the dupe first.
Thank you Mr.Mike for your time it actually worked out perfectly when I tested.
@0___________ Although I can understand why you selected that dupe target (and I'm not going to dispute it), note that it does not address the use of the atoi function in this case.
2

You are confusing single characters with character strings. The latter are sequences of characters (arrays) terminated with a nul (zero value) character, and that is what the atoi function expects as its input.

To convert a single character digit to its numerical value, you just need to subtract the value of the zero digit ('0') from that character's value (the values of the numerical digits are guaranteed by the Standard to be contiguous).

So, rather than:

array[i] = atoi(&parray[i]);

use:

array[i] = parray[i] - '0'; // Will work if (and only if) parray[i] is a digit.

What is happening in your code is that (by chance) there is a zero byte immediately after the end of your 5-character array (but you can not rely on this), so each atoi(&parray[i]) call is passing a character string starting with, respectively, the '7', '1', '5', '4' and '2' characters, and ending only after the '2'. Thus, you are getting values that represent the numbers formed by the concatenation of your individual array digits. But I repeat: you cannot rely on there being a zero-value character after your array!

7 Comments

char parray[LENGTH] = { '-7', '1', '4','5' ,'2'}; What I have multiple characters like -7 and store it in array [0] = -7 ! Its giving error when I give -7 in parray. error: multi-character character constant [-Werror=multichar]
@AnasMunir I think that using '-7' to initialize a char variable puts you in undefined behaviour territory. On my system, that represents an int constant of value 11575 but how that is then interpreted as a char will likely vary.
Basically I am asking that is there a way to convert multiple characters like -7. My program only works for single characters like 1 4 9. But not for 14 or -7. Is there a way I can modify my function in order to convert multiple characters as well ? Mr.Adrian
@AnasMunir You would need to declare your parray as an array of strings (const char* parray[] = {"-7", "1", "4", "5", "2"}; - even the 'single digit' values will be arrays of 2 characters, because of the added nul-terminator) and then call atoi(parray[i]) (no & symbol) on each of those strings.
@Anas - Use double quotes for string literals, single quotes for single character literals. '2' is a character (actually, an int); "2" is a string consisting of the digit, 2 and a nul terminator.
|
2

This statement

array[i] = atoi(&parray[i]);

invokes undefined behavior because the character array parray does not contain a string: a sequence of characters terminated with the zero character '\0'. And the function atoi expects a string as its argument.

Instead you could write

array[i] = parray[i] - '0';

Character '0' through up to '9' have an increased by 1 sequence of codes. So for example if to write '3' - '0' you will get the integer number 3.

2 Comments

char parray[LENGTH] = { '-7', '1', '4','5' ,'2'}; What I have multiple characters like -7 and store it in array [0] = -7 Its giving error when I give -7 in parray. error: multi-character character constant [-Werror=multichar]
@AnasMunir It is a multibyte character. Its representation is implementation defined.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.