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?
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
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
add a comment |
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
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
2
atoi
takes aconst char*
, not achar
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, preferstrtol
overatoi
. 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
withgcc
orclang
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
add a comment |
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
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
c arrays char atoi
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
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
2
atoi
takes aconst char*
, not achar
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, preferstrtol
overatoi
. 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
withgcc
orclang
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
add a comment |
2
atoi
takes aconst char*
, not achar
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, preferstrtol
overatoi
. 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
withgcc
orclang
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
add a comment |
3 Answers
3
active
oldest
votes
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.
This should be the accepted answer. No need to complicate things.
– CoffeeTableEspresso
Mar 21 at 21:18
add a comment |
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;
add a comment |
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);
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
This should be the accepted answer. No need to complicate things.
– CoffeeTableEspresso
Mar 21 at 21:18
add a comment |
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.
This should be the accepted answer. No need to complicate things.
– CoffeeTableEspresso
Mar 21 at 21:18
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Mar 21 at 19:16
David CullenDavid Cullen
6,21412445
6,21412445
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Mar 21 at 19:20
P__J__P__J__
11.4k2826
11.4k2826
add a comment |
add a comment |
2
atoi
takes aconst char*
, not achar
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
overatoi
. 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
orclang
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