Convert char array to int array (C) [closed] The Next CEO of Stack OverflowCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?PHP: Delete an element from an arrayHow do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Why is char[] preferred over String for passwords?For-each over an array in JavaScript?

From jafe to El-Guest

Point distance program written without a framework

Why don't programming languages automatically manage the synchronous/asynchronous problem?

Raspberry pi 3 B with Ubuntu 18.04 server arm64: what chip

Man transported from Alternate World into ours by a Neutrino Detector

Can I board the first leg of the flight without having final country's visa?

IC has pull-down resistors on SMBus lines?

Is there a way to save my career from absolute disaster?

Is there such a thing as a proper verb, like a proper noun?

What flight has the highest ratio of timezone difference to flight time?

Is it ever safe to open a suspicious HTML file (e.g. email attachment)?

Strange use of "whether ... than ..." in official text

Lucky Feat: How can "more than one creature spend a luck point to influence the outcome of a roll"?

Spaces in which all closed sets are regular closed

Can this note be analyzed as a non-chord tone?

Does the Idaho Potato Commission associate potato skins with healthy eating?

What steps are necessary to read a Modern SSD in Medieval Europe?

Is French Guiana a (hard) EU border?

Aggressive Under-Indexing and no data for missing index

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

how one can write a nice vector parser, something that does pgfvecparseA=B-C; D=E x F;

Film where the government was corrupt with aliens, people sent to kill aliens are given rigged visors not showing the right aliens

What happened in Rome, when the western empire "fell"?

What difference does it make using sed with/without whitespaces?



Convert char array to int array (C) [closed]



The Next CEO of Stack OverflowCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?PHP: Delete an element from an arrayHow do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Why is char[] preferred over String for passwords?For-each over an array in JavaScript?










-1















I have a char array "2468" that I want to convert to an int array for easy mathematic manipulations. But when I run the project it just says the 'Core dumped' error. I don't know what could cause that...



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

int main()
int i = 0;
char charArray[] = "2468";
int intArray[20];
for (i = 0; i < strlen(charArray); ++i)
intArray[i] = atoi(charArray[i]);
printf("%d", intArray[i]);

return 0;










share|improve this question













closed as off-topic by Antti Haapala, ekad, M-M, Vega, dgknca Mar 22 at 7:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, ekad, M-M, Vega, dgknca
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 2





    atoi takes a const char*, not a char as argument: en.cppreference.com/w/c/string/byte/atoi - you should turn on all compiler warnings and treat warnings as error, then your compiler wouldn't even compile this code

    – UnholySheep
    Mar 21 at 19:13







  • 1





    Also, prefer strtol over atoi. It is more robust and can actually handle errors.

    – Christian Gibbons
    Mar 21 at 19:15











  • Please learn to read the compiler output. No mention about the diagnostics messages that are mandatory from any compliant compiler.

    – Antti Haapala
    Mar 21 at 20:26











  • with gcc or clang you should have something like -Wall -Wextra -Werror (turns on lots and lots of warnings, and treat them as errors).

    – CoffeeTableEspresso
    Mar 21 at 21:10















-1















I have a char array "2468" that I want to convert to an int array for easy mathematic manipulations. But when I run the project it just says the 'Core dumped' error. I don't know what could cause that...



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

int main()
int i = 0;
char charArray[] = "2468";
int intArray[20];
for (i = 0; i < strlen(charArray); ++i)
intArray[i] = atoi(charArray[i]);
printf("%d", intArray[i]);

return 0;










share|improve this question













closed as off-topic by Antti Haapala, ekad, M-M, Vega, dgknca Mar 22 at 7:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, ekad, M-M, Vega, dgknca
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 2





    atoi takes a const char*, not a char as argument: en.cppreference.com/w/c/string/byte/atoi - you should turn on all compiler warnings and treat warnings as error, then your compiler wouldn't even compile this code

    – UnholySheep
    Mar 21 at 19:13







  • 1





    Also, prefer strtol over atoi. It is more robust and can actually handle errors.

    – Christian Gibbons
    Mar 21 at 19:15











  • Please learn to read the compiler output. No mention about the diagnostics messages that are mandatory from any compliant compiler.

    – Antti Haapala
    Mar 21 at 20:26











  • with gcc or clang you should have something like -Wall -Wextra -Werror (turns on lots and lots of warnings, and treat them as errors).

    – CoffeeTableEspresso
    Mar 21 at 21:10













-1












-1








-1








I have a char array "2468" that I want to convert to an int array for easy mathematic manipulations. But when I run the project it just says the 'Core dumped' error. I don't know what could cause that...



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

int main()
int i = 0;
char charArray[] = "2468";
int intArray[20];
for (i = 0; i < strlen(charArray); ++i)
intArray[i] = atoi(charArray[i]);
printf("%d", intArray[i]);

return 0;










share|improve this question














I have a char array "2468" that I want to convert to an int array for easy mathematic manipulations. But when I run the project it just says the 'Core dumped' error. I don't know what could cause that...



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

int main()
int i = 0;
char charArray[] = "2468";
int intArray[20];
for (i = 0; i < strlen(charArray); ++i)
intArray[i] = atoi(charArray[i]);
printf("%d", intArray[i]);

return 0;







c arrays char atoi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 21 at 19:10









Mathieu GouinMathieu Gouin

173




173




closed as off-topic by Antti Haapala, ekad, M-M, Vega, dgknca Mar 22 at 7:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, ekad, M-M, Vega, dgknca
If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Antti Haapala, ekad, M-M, Vega, dgknca Mar 22 at 7:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, ekad, M-M, Vega, dgknca
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 2





    atoi takes a const char*, not a char as argument: en.cppreference.com/w/c/string/byte/atoi - you should turn on all compiler warnings and treat warnings as error, then your compiler wouldn't even compile this code

    – UnholySheep
    Mar 21 at 19:13







  • 1





    Also, prefer strtol over atoi. It is more robust and can actually handle errors.

    – Christian Gibbons
    Mar 21 at 19:15











  • Please learn to read the compiler output. No mention about the diagnostics messages that are mandatory from any compliant compiler.

    – Antti Haapala
    Mar 21 at 20:26











  • with gcc or clang you should have something like -Wall -Wextra -Werror (turns on lots and lots of warnings, and treat them as errors).

    – CoffeeTableEspresso
    Mar 21 at 21:10












  • 2





    atoi takes a const char*, not a char as argument: en.cppreference.com/w/c/string/byte/atoi - you should turn on all compiler warnings and treat warnings as error, then your compiler wouldn't even compile this code

    – UnholySheep
    Mar 21 at 19:13







  • 1





    Also, prefer strtol over atoi. It is more robust and can actually handle errors.

    – Christian Gibbons
    Mar 21 at 19:15











  • Please learn to read the compiler output. No mention about the diagnostics messages that are mandatory from any compliant compiler.

    – Antti Haapala
    Mar 21 at 20:26











  • with gcc or clang you should have something like -Wall -Wextra -Werror (turns on lots and lots of warnings, and treat them as errors).

    – CoffeeTableEspresso
    Mar 21 at 21:10







2




2





atoi takes a const char*, not a char as argument: en.cppreference.com/w/c/string/byte/atoi - you should turn on all compiler warnings and treat warnings as error, then your compiler wouldn't even compile this code

– UnholySheep
Mar 21 at 19:13






atoi takes a const char*, not a char as argument: en.cppreference.com/w/c/string/byte/atoi - you should turn on all compiler warnings and treat warnings as error, then your compiler wouldn't even compile this code

– UnholySheep
Mar 21 at 19:13





1




1





Also, prefer strtol over atoi. It is more robust and can actually handle errors.

– Christian Gibbons
Mar 21 at 19:15





Also, prefer strtol over atoi. It is more robust and can actually handle errors.

– Christian Gibbons
Mar 21 at 19:15













Please learn to read the compiler output. No mention about the diagnostics messages that are mandatory from any compliant compiler.

– Antti Haapala
Mar 21 at 20:26





Please learn to read the compiler output. No mention about the diagnostics messages that are mandatory from any compliant compiler.

– Antti Haapala
Mar 21 at 20:26













with gcc or clang you should have something like -Wall -Wextra -Werror (turns on lots and lots of warnings, and treat them as errors).

– CoffeeTableEspresso
Mar 21 at 21:10





with gcc or clang you should have something like -Wall -Wextra -Werror (turns on lots and lots of warnings, and treat them as errors).

– CoffeeTableEspresso
Mar 21 at 21:10












3 Answers
3






active

oldest

votes


















1














You should read the warnings. They are there to help you:



b.c: In function ‘main’:
b.c:11:37: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast [-Wint-conversion]
intArray[i] = atoi(charArray[i]);
~~~~~~~~~^~~
In file included from b.c:4:
/usr/include/stdlib.h:104:30: note: expected ‘const char *’ but argument is of type ‘char’
extern int atoi (const char *__nptr)
~~~~~~~~~~~~^~~~~~


But apart from that, why complicate things? Since you want to copy digits rather than numbers, just do like this:



for (i = 0; i < strlen(charArray); ++i) 
intArray[i] = charArray[i] - '0';



If you want to guard against errors, check with isdigit(charArray[i]) first. Remember that atoi will return 0 on error, so if you get a 0, you will know know if the conversion worked or not.






share|improve this answer























  • This should be the accepted answer. No need to complicate things.

    – CoffeeTableEspresso
    Mar 21 at 21:18


















1














You need to convert each char in charArray into a string to pass to atoi:



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

int main()
int i = 0;
char charArray[] = "2468";
int intArray[20];
for (i = 0; i < strlen(charArray); ++i)
// Convert character to string
char convert[2] = charArray[i], 0;
// Pass string to atoi
intArray[i] = atoi(convert);
printf("%d", intArray[i]);

return 0;






share|improve this answer






























    0














    int main() 
    int i = 0;
    char charArray[] = "2468";
    int intArray[20];
    while(charArray[i])

    intArray[i] = charArray[i] - '0');
    printf("%d", intArray[i++]);

    return 0;



    Two functions: first converts chars from the string to their digit values, the second converts table pointers to strings to numbers.



    void convert_charsToDigits(const char *str, int *arr)

    int c;
    while(*str)

    if(!isdigit(c = *str++)) continue;
    *arr++ = c - '0';



    void convert_stringsToInts(const char **str, int *arr) //Takes NULL terminated string table

    while(*str)

    *arr++ = atoi(*str++);




    usage



    char *cstr = "696"
    char *str = "2345", "455667", NULL;
    int arr[5];

    convert_charsToDigits(cstr, arr);
    convert_stringsToInts(str, arr);





    share|improve this answer





























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You should read the warnings. They are there to help you:



      b.c: In function ‘main’:
      b.c:11:37: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast [-Wint-conversion]
      intArray[i] = atoi(charArray[i]);
      ~~~~~~~~~^~~
      In file included from b.c:4:
      /usr/include/stdlib.h:104:30: note: expected ‘const char *’ but argument is of type ‘char’
      extern int atoi (const char *__nptr)
      ~~~~~~~~~~~~^~~~~~


      But apart from that, why complicate things? Since you want to copy digits rather than numbers, just do like this:



      for (i = 0; i < strlen(charArray); ++i) 
      intArray[i] = charArray[i] - '0';



      If you want to guard against errors, check with isdigit(charArray[i]) first. Remember that atoi will return 0 on error, so if you get a 0, you will know know if the conversion worked or not.






      share|improve this answer























      • This should be the accepted answer. No need to complicate things.

        – CoffeeTableEspresso
        Mar 21 at 21:18















      1














      You should read the warnings. They are there to help you:



      b.c: In function ‘main’:
      b.c:11:37: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast [-Wint-conversion]
      intArray[i] = atoi(charArray[i]);
      ~~~~~~~~~^~~
      In file included from b.c:4:
      /usr/include/stdlib.h:104:30: note: expected ‘const char *’ but argument is of type ‘char’
      extern int atoi (const char *__nptr)
      ~~~~~~~~~~~~^~~~~~


      But apart from that, why complicate things? Since you want to copy digits rather than numbers, just do like this:



      for (i = 0; i < strlen(charArray); ++i) 
      intArray[i] = charArray[i] - '0';



      If you want to guard against errors, check with isdigit(charArray[i]) first. Remember that atoi will return 0 on error, so if you get a 0, you will know know if the conversion worked or not.






      share|improve this answer























      • This should be the accepted answer. No need to complicate things.

        – CoffeeTableEspresso
        Mar 21 at 21:18













      1












      1








      1







      You should read the warnings. They are there to help you:



      b.c: In function ‘main’:
      b.c:11:37: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast [-Wint-conversion]
      intArray[i] = atoi(charArray[i]);
      ~~~~~~~~~^~~
      In file included from b.c:4:
      /usr/include/stdlib.h:104:30: note: expected ‘const char *’ but argument is of type ‘char’
      extern int atoi (const char *__nptr)
      ~~~~~~~~~~~~^~~~~~


      But apart from that, why complicate things? Since you want to copy digits rather than numbers, just do like this:



      for (i = 0; i < strlen(charArray); ++i) 
      intArray[i] = charArray[i] - '0';



      If you want to guard against errors, check with isdigit(charArray[i]) first. Remember that atoi will return 0 on error, so if you get a 0, you will know know if the conversion worked or not.






      share|improve this answer













      You should read the warnings. They are there to help you:



      b.c: In function ‘main’:
      b.c:11:37: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast [-Wint-conversion]
      intArray[i] = atoi(charArray[i]);
      ~~~~~~~~~^~~
      In file included from b.c:4:
      /usr/include/stdlib.h:104:30: note: expected ‘const char *’ but argument is of type ‘char’
      extern int atoi (const char *__nptr)
      ~~~~~~~~~~~~^~~~~~


      But apart from that, why complicate things? Since you want to copy digits rather than numbers, just do like this:



      for (i = 0; i < strlen(charArray); ++i) 
      intArray[i] = charArray[i] - '0';



      If you want to guard against errors, check with isdigit(charArray[i]) first. Remember that atoi will return 0 on error, so if you get a 0, you will know know if the conversion worked or not.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 21 at 19:50









      BromanBroman

      7,423112644




      7,423112644












      • This should be the accepted answer. No need to complicate things.

        – CoffeeTableEspresso
        Mar 21 at 21:18

















      • This should be the accepted answer. No need to complicate things.

        – CoffeeTableEspresso
        Mar 21 at 21:18
















      This should be the accepted answer. No need to complicate things.

      – CoffeeTableEspresso
      Mar 21 at 21:18





      This should be the accepted answer. No need to complicate things.

      – CoffeeTableEspresso
      Mar 21 at 21:18













      1














      You need to convert each char in charArray into a string to pass to atoi:



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

      int main()
      int i = 0;
      char charArray[] = "2468";
      int intArray[20];
      for (i = 0; i < strlen(charArray); ++i)
      // Convert character to string
      char convert[2] = charArray[i], 0;
      // Pass string to atoi
      intArray[i] = atoi(convert);
      printf("%d", intArray[i]);

      return 0;






      share|improve this answer



























        1














        You need to convert each char in charArray into a string to pass to atoi:



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

        int main()
        int i = 0;
        char charArray[] = "2468";
        int intArray[20];
        for (i = 0; i < strlen(charArray); ++i)
        // Convert character to string
        char convert[2] = charArray[i], 0;
        // Pass string to atoi
        intArray[i] = atoi(convert);
        printf("%d", intArray[i]);

        return 0;






        share|improve this answer

























          1












          1








          1







          You need to convert each char in charArray into a string to pass to atoi:



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

          int main()
          int i = 0;
          char charArray[] = "2468";
          int intArray[20];
          for (i = 0; i < strlen(charArray); ++i)
          // Convert character to string
          char convert[2] = charArray[i], 0;
          // Pass string to atoi
          intArray[i] = atoi(convert);
          printf("%d", intArray[i]);

          return 0;






          share|improve this answer













          You need to convert each char in charArray into a string to pass to atoi:



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

          int main()
          int i = 0;
          char charArray[] = "2468";
          int intArray[20];
          for (i = 0; i < strlen(charArray); ++i)
          // Convert character to string
          char convert[2] = charArray[i], 0;
          // Pass string to atoi
          intArray[i] = atoi(convert);
          printf("%d", intArray[i]);

          return 0;







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 21 at 19:16









          David CullenDavid Cullen

          6,21412445




          6,21412445





















              0














              int main() 
              int i = 0;
              char charArray[] = "2468";
              int intArray[20];
              while(charArray[i])

              intArray[i] = charArray[i] - '0');
              printf("%d", intArray[i++]);

              return 0;



              Two functions: first converts chars from the string to their digit values, the second converts table pointers to strings to numbers.



              void convert_charsToDigits(const char *str, int *arr)

              int c;
              while(*str)

              if(!isdigit(c = *str++)) continue;
              *arr++ = c - '0';



              void convert_stringsToInts(const char **str, int *arr) //Takes NULL terminated string table

              while(*str)

              *arr++ = atoi(*str++);




              usage



              char *cstr = "696"
              char *str = "2345", "455667", NULL;
              int arr[5];

              convert_charsToDigits(cstr, arr);
              convert_stringsToInts(str, arr);





              share|improve this answer



























                0














                int main() 
                int i = 0;
                char charArray[] = "2468";
                int intArray[20];
                while(charArray[i])

                intArray[i] = charArray[i] - '0');
                printf("%d", intArray[i++]);

                return 0;



                Two functions: first converts chars from the string to their digit values, the second converts table pointers to strings to numbers.



                void convert_charsToDigits(const char *str, int *arr)

                int c;
                while(*str)

                if(!isdigit(c = *str++)) continue;
                *arr++ = c - '0';



                void convert_stringsToInts(const char **str, int *arr) //Takes NULL terminated string table

                while(*str)

                *arr++ = atoi(*str++);




                usage



                char *cstr = "696"
                char *str = "2345", "455667", NULL;
                int arr[5];

                convert_charsToDigits(cstr, arr);
                convert_stringsToInts(str, arr);





                share|improve this answer

























                  0












                  0








                  0







                  int main() 
                  int i = 0;
                  char charArray[] = "2468";
                  int intArray[20];
                  while(charArray[i])

                  intArray[i] = charArray[i] - '0');
                  printf("%d", intArray[i++]);

                  return 0;



                  Two functions: first converts chars from the string to their digit values, the second converts table pointers to strings to numbers.



                  void convert_charsToDigits(const char *str, int *arr)

                  int c;
                  while(*str)

                  if(!isdigit(c = *str++)) continue;
                  *arr++ = c - '0';



                  void convert_stringsToInts(const char **str, int *arr) //Takes NULL terminated string table

                  while(*str)

                  *arr++ = atoi(*str++);




                  usage



                  char *cstr = "696"
                  char *str = "2345", "455667", NULL;
                  int arr[5];

                  convert_charsToDigits(cstr, arr);
                  convert_stringsToInts(str, arr);





                  share|improve this answer













                  int main() 
                  int i = 0;
                  char charArray[] = "2468";
                  int intArray[20];
                  while(charArray[i])

                  intArray[i] = charArray[i] - '0');
                  printf("%d", intArray[i++]);

                  return 0;



                  Two functions: first converts chars from the string to their digit values, the second converts table pointers to strings to numbers.



                  void convert_charsToDigits(const char *str, int *arr)

                  int c;
                  while(*str)

                  if(!isdigit(c = *str++)) continue;
                  *arr++ = c - '0';



                  void convert_stringsToInts(const char **str, int *arr) //Takes NULL terminated string table

                  while(*str)

                  *arr++ = atoi(*str++);




                  usage



                  char *cstr = "696"
                  char *str = "2345", "455667", NULL;
                  int arr[5];

                  convert_charsToDigits(cstr, arr);
                  convert_stringsToInts(str, arr);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 21 at 19:20









                  P__J__P__J__

                  11.4k2826




                  11.4k2826













                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현