# Manipulation of character arrays

• 04-17-2002
Han
Manipulation of character arrays
I have been having trouble with using arrays of indefinite size.
I havent had a problem I think creating an array of unknown size but I have had problems ending the program. I have noticed that others have had similiar problems but I am still unsure as to how I should go about fixing the problem.

If I were to calculate the sum of an array of size known I would do this and this works:

for(z=0;x<10;x++){
scanf("%s",temp_array);
total=atoi(temp_array);
sum=sum+total;
}
printf("the sum of these numbers is %i\n"sum);
return 0;
}

this is fine for 10 numbers in the array, it will find the sum of ten numbers and terminate.
However what I want to know is what if you wanted to find 20 30 or even 40 numbers and terminate when you type in a string.
so if you were to type in 1 2 3 4 5 6 and then end it would find the sum of the 6 numbers you put in.
What I need is something that terminates the loop when a string of characters or a character is typed in. I have tried lots of things but it just hangs telling me that it is ignoring the code that I have put in. Can anybody help me.
I need to change the above code to handle this and I think it involves putting in a loop either while or for but I dont know how to implement it
• 04-17-2002
Monster
I'm not very good with scanf but I'll give it a try:

Code:

```int main() {         int sum = 0;         int number;         while(scanf("%d*%c", &number))         {                 sum += number;         }         printf("the sum of these numbers is %d\n", sum);         return 0; }```
• 04-17-2002
Shiro
You can create an array dynamically by using malloc and free the memory with free when you don't need it anymore.

Code:

```int *array; int size_of_array; size_of_array = calculate_size (); array = malloc (sizeof (int) * size_of_array); free (array);```
• 04-17-2002
Han
could you say something like this:

while(std_array[z]!='\0'){
for(z=0;sizeof(std_array);z++{
total=atoi(std_array);
sum=sum+total;
}
if(std_array[z]='\0' /*this represents the end of a string*/
{
printf("terminate loop:\n");
}

} /*end the while loop*/

I havent tried this and it probably doesnt work as I am new to C but is there something like this that would work.
I am trying to work around this null character thing to get this to work.
basically what I am trying to say is if it isnt null then continue to loop but when it is null then terminate.
I am thinking though this isnt going to solve my problem of when you enter a character it is going to carry out the sum function that it is supposed to.
• 04-17-2002
Shiro
Am I correct that you are trying to do the following:

1. The user enters numbers
2. You read in the numbers as strings and convert them to int
3. You calculate the sum of all ints

?

Perhaps you could try something like this:

Code:

```stop = 0; while (stop != 1) {     scanf ("%s",temp_array);         if (strcmp (temp_array, "stop") == 0)     {         stop = 1;     }     else     {         total = atoi (temp_array);         sum = sum+total;     } } printf("the sum of these numbers is %d\n", sum);```
The user enters numbers. If the user enters the word stop, then the loop ends. I've not tested this and probably there are better solutions, but you can give it a try.

The point where it is all about in my example is that you should introduce a check variable.