convert a char array of integers to an int

hello all,

I've got the following problem:
I am reading a list of integers as arguments from console, so they end up in an array of character strings *argv[].
Since it is a list of integers, I would like to put them in an integer array.
My problem is, I only manage to access single elements of these arrays (the numbers might consist of multiple digits), that is, I do not manage to cast this char arrays argv[i] somehow to int...

can anyone help me?

cheers

Edit: I tried to define a pointer to int variable to which I wanted to assign the starting address of my character array. I thought I could easily read out an integer that way, but the compiler didn't let me to.
Last edited on
Well, theres the hard way, where you loop through the string while the next character is a digit, and add that digit to 10*what you already have, and then a little funniness when you reach decimal points, or you can use scanf(), or fscanf() for streams other than stdin, from the stdio.h library.

http://www.cplusplus.com/reference/clibrary/cstdio/scanf.html

Edit: Lol i forgot that a lot of people here use cin >> [variable]; here. For input one number at a time, cin (iostream library) might be easier, but if the user is typing more than one number at a time, presumably separated by whitespace, then scanf would be better.
Last edited on
could you please be a little more specific?
I do understand that one can loop through the string, but I don't know how to concatenate the single character constants (ASCII values) from the string to a single integer value.

And since you cannot assign an address of a character string to a pointers to int...
If i understand you clearly, you are trying to take a string of characters and derive the integer value from it. So, for example the user might type the number 4235 and you would have the string "4235". What you want is when they enter 4235, to have an integer representing that value.

What i was saying was a function was written in stdio.h already to do that. If you have ever used printf() instead of cout, it will be familiar.
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h> /* same as <cstdio> */

int main(){
  int input_decimalInteger;
  double input_float;
  printf("Enter an integer, then a floating point number.\n");
  scanf("%d %Lf",&input_decimalInteger,&input_float);
  printf("Your integer was %d, \nand your float was %f.\n",input_decimalInteger,input_float);
  return 0;
}


These functions are a little bit tricky. You can find their descriptions at
http://www.cplusplus.com/reference/clibrary/cstdio/
but i'll try and explain a little in case you don't feel like going there.

scanf() looks at stdin, short for standard in. This is the stream (a giant array type thing) that command-line input goes. The first argument to scanf() is a format string. This tells it what to look for. I told it to look for a regular decimal integer (%d), and then a double size floating point (%Lf). I then told it where to put the input. Notice I put '&' before the variable names when i gave them to scanf(). That means I'm really passing their memory addresses. Because a function can only return one value, and because scanf() is meant to find more than one number, it goes ahead and writes to those addresses from within the function. printf() is similar, but because it doesn't change any values, you send the variables' values, not addresses.

Another function to suit your purpose, and might be more obvious is as follows, from the iostream library.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main(){
  int input_integer;
  cout << "Enter a decimal integer.\n";
  cin >> input_integer;
  cout << "The number you entered is:\n" << input_integer;
  return 0;
}


After testing something for myself, it turns out cin can also accept multiple values.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main(){
  int input_integer;
  double input_float;
  cout << "Enter a decimal integer.\n";
  cin >> input_integer >> input_float;
  cout << "The numbers you entered were " << input_integer << " " << input_float << endl;
  return 0;
}


I hope that helps.
Last edited on
Thanks for your effort xnabu.

Though, I don't want to read input from standard input.

I am trying to read a list of integers (exclusively) as arguments *to main* from console, so they end up in an array of character strings *argv[].
i.e../a.out 45 3 12 34 54 23 2

Since it is a list of integers, I would like to put them in an integer array.
But they are stored in char arrays ("C-style strings"):
1
2
3
4
argv[1]      is {'4', '5', '\0'}
argv[1][0]  is'4';
argv[1][1]  is '5';
argv[1][2]  is '\0';

My problem is, I only manage to access single elements of these arrays (the numbers might consist of multiple digits), that is, I do not manage to cast this char arrays argv[i] somehow to int...

for example, (int)(*argv[1]) would give me the ASCII value for the first number (1) in argv[1], if i started the program by ./a.out 12 23 3 4

Anyone?

Edit: I tried to define a pointer to int variable to which I wanted to assign the starting address of my character array. I thought I could easily read out an integer that way, but the compiler didn't let me to.
Last edited on
If I understand it right, what I see here is a simple program to print the command-line arguments in numeric form which they entered in a string from. There is an already existing built-in function available for that, is atoi().

Simple example, to print out the command line arguments (I assume only numbers are inputted) string to numeric.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
for (i = 1; i <= argc; i++)
printf("\n input:%s integer(output):%d",argv[i],atoi(argv[i]));

return 0;
}


I assume the input from command-line is number values. If not, the atoi() prints zero (or something else if your compiler thinkgs different).

If I run the program (argTest.c) as:

$> argTest 12 234 345 4

input:12 integer(output):12
input:234 integer(output):234
input:345 integer(output):345
input:4 integer(output):4


Good luck :)
Otherwise, if you want it to store and work on them from array, try this out.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i,j, arr[10];

for (i = 1; i <= argc; i++)
{
printf("\n input:%s integer(output):%d",argv[i],atoi(argv[i]));
if (i < 10)
arr[i-1] = atoi(argv[i]);
}

for (j = 0; j < i; j++)
if (j < 10)
printf("\n arr[%d]: %d", j, arr[j]);


return 0;
}


Good luck :)
Cheers mate!
atoi was exactly what I was looking for! I didn't know of its existence.
Thanks for your illustration.
Topic archived. No new replies allowed.