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

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
Anas
  • 35
  • 5
  • 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]); – Anas Jun 17 '21 at 12:40
  • 1
    1) Strings need to be null terminated. 2) atoi translates the whole string to an integer, not digit by digit. – Lundin Jun 17 '21 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. – LSerni Jun 17 '21 at 12:42
  • Thank you Mr.LSerni and Mr.Lundin for your time – Anas Jun 17 '21 at 12:54

3 Answers3

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]);
    }
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 1
    I think that 60k user should look for the dupe first. – 0___________ Jun 17 '21 at 12:46
  • Thank you Mr.Mike for your time it actually worked out perfectly when I tested. – Anas Jun 17 '21 at 12:54
  • 1
    @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. – Adrian Mole Jun 17 '21 at 13:10
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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 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] – Anas Jun 17 '21 at 13:05
  • @AnasMunir It is a multibyte character. Its representation is implementation defined. – Vlad from Moscow Jun 17 '21 at 14:31
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!

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 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] – Anas Jun 17 '21 at 13:02
  • @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. – Adrian Mole Jun 17 '21 at 13:07
  • 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 – Anas Jun 17 '21 at 13:15
  • @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. – Adrian Mole Jun 17 '21 at 13:20
  • When I am declaring char *parray[] = = { '-7', '1', '4','5' ,'2'} Its giving 2 errors mainly : error 1 : multi-character character constant [-Werror=multichar] char* parray[LENGTH] = { '-7', '1', '4','5' ,'2'}; error 2: error: initialization makes pointer from integer without a cast – Anas Jun 17 '21 at 13:30
  • 1
    @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. – Adrian Mole Jun 17 '21 at 13:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233896/discussion-between-anas-munir-and-adrian-mole). – Anas Jun 17 '21 at 13:39