Using atoi to populate an ints array

For the first time I asked a question. I apologize if I already have this, but I had several searches, and I did not quite understand what, in my opinion, I was looking for. I am very new to C and have worked through some home exercises for my microcontroller class. We are currently working with simple exercises before we enter embedded C, and I am trying to write a program that will contain a line of text consisting of 10 numbers separated by commas and fill an int array. We were told to use substring and atoi as a hint. I think I'm close to getting it right, but I can't get him to correctly display my numbers.

Also I'm not looking for answers to a spoon. At the moment, a few hints are enough. I would like to try to figure this out myself before asking for a solution.

Here is my code:

 #include <stdio.h> #include <stdlib.h> int main(void) { int a[10]; char str[] = {1,2,3,4,5,6,7,8,9,10}; //contains string of numbers int i; puts("This prints out ten numbers:"); for (i = 0; i < 10; i++) { a[i] = atoi(str); printf("%d", a[i]); //i'm guessing the problem lies in one of the above two lines } return 0; } 

This outputs the following:

 This prints out ten numbers: 0000000000 

Thanks to everyone who can help! Chris

+4
source share
4 answers

You said you need to use a string of text separated by commas, but you actually declared a char array containing ten (binary) integers. To get this into a string, you just need to do this:

char str[] = "1,2,3,4,5,6,7,8,9,10";

Then you will need to somehow process this string in order to print each number into its int array.

+3
source

First, you must declare a string as follows:

 char str[] = {"1,2,3,4,5,6,7,8,9,10"}; 

" made the numbers integer. Then you will need to do the tokenization and use the <string.h> library, which will come in handy in this situation.

Here's how you do tokenization:

first define a buffer marker:

 char* token; token = strtok(str,","); //think of it as substring, the part of the str before the comma for (i = 0; i < 10; i++) { a[i] = atoi(token); printf("%d\t", a[i]); //i'm guessing the problem lies in one of the above two lines token = strtok(NULL, ","); //this line is also required for tokenizing the next element } 

Using the strtok() , you split the elements between the comas and got the line numbers. The atoi() function is used to convert them to integers and print them. You can see this link for the strtok() for a better understanding.

+1
source

The problem is how you create the string.
Sorry my previous answer, I misunderstood your question:

Simply put, the declaration should be as follows:

 char str[] = "1,2,3,4,5,6,7,8,9, 10, 12"; 

Then you can use strtok to split the string into an array of strings, omitting the delimiter (which is a comma in your case), then pass the members of the array to atoi

Now, why is your code not working?
Firstly, the characters must be surrounded by apostrophes, otherwise the compiler will take the number that you pass literally as an ASCII value.

Secondly, arrays in C: char str[] = {'1', '2', '3', '4', '5'}; does not mean a line separated by a comma, these commas are separated by ARRAY members, each in its own index, and not as a whole line.

0
source

Your definition is char str[] = {1,2,3,4,5,6,7,8,9,10}; really sets character values ​​from 1 to 10.

In an ASCII character diagram, these are non-printable control characters. Spelling "1" instead of 1 sets the ASCII value to 1, which is 0x31.

Another mistake is that the commas in your definition only separate the values ​​in the definition, so the result is an array of characters without any separation, therefore 12345678910.

therefore, the correct path would be char str[] = "1,2,3,4,5,6,7,8,9,10";

0
source

All Articles