简体   繁体   中英

C Converting Char array (strings) into int array

I have been searching around for hours for this, but I can't seem to find an answer, especially when it comes to C language. In java it would be simple matter of casting and such, but I just cant seem to get a firm grip on it in C.

My problem is: I have to get an input from a user, they can enter any number from 0 to 31. They can enter up to 31 numbers. They can repeat. The input could look like this: 3 15 32. I know how to take this input and store it as a string using the fgets() function. I store the input in array s[];

Now, the part that I am stuck on, how do I convert that to an int array so that int int[0]=3, i[1]=15, i[2]=32, etc.

I tried using sscanf(s, "%d", int), but it only is able to get the first number from the input, stores it in int[0], and then doesn't fill in the rest. Is there some kind of a function that makes this all easy and quick?

Thank you in advance.

You can use a library or you can write a helper function. The helper function would look something like this (pseudocode):

int output = 0;
for (int i = 0; i < string.length; i++) {
  output = output * 10;
  output = output + parse(string[i]) //parse first character into a single digit
}

For example, if you have 253, it will read 2. Then it will multiply it by 10 (20) and add 5 (25). Then it will multiply that by 10 (250) and add 3, which will be the expected output.

Note that you need to find a way to parse a single character of your string into a single digit integer - that should be very easy using something like a switch() statement.

EDIT: If you are looking for a library function, check out this question:

What is the difference between sscanf or atoi to convert a string to an integer?

You might be overthinking things. Here's a simply way to input numbers from a user that doesn't involve formatting or parsing strings.

int numArray[31];
int i = 0;
int num = 0;

while( scanf("%d", &num) > 0 && i < 31 && num >0) {
    numArray[i] = num;
    i++;
}

The loop will stop when the user enters a negative number, or has entered all 31 possible numbers.

You still have to make sure the user enters a number between 0 and 31 though!

It's been a while since I've worked with 'C', but here's the basic idea. To convert the char array values to integers, you'll basically have to assign each value to an int var such as:

i[0] = c[0]
i[1] = c[1]
i[2] = c[2]

The reason you can't cast the numbers directly to integer references is because C allots a contiguous memory space to hold those 3 characters. Depending on the compiler you are using will determine the char size and int size. But if the chars are 8 bits each, then an array of 3 chars is 3 bytes. 16 bit int would equal 6 bites. If you were to cast directly to ints, you wouldn't get the correct value translations because memory addresses would overlap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM