
Originally Posted by
cyberjupiter
Well then you didn't clearly understand what I was trying to do. Your last example does not meet the spec I stated in my #1 post.
if it worked properly I'd not be able to blow it up. you're not checking for over flow on your char[1000], your index++ is not working correctly. look
your code again adding something to show and tell something else as well.
what ever it is it is not working properly,
Code:
#include <stdio.h>
int main()
{
char buffer[10];
char storage[10];
int index = 0;
int holder;
int i = 0;
int newidx = 0;
while((holder = getchar()) != EOF) //while the input is not = end of file. holder will only hold one char every reading
{
if (holder == '\n') //if the input is = newline character
{
if (index > 10) //if the index is more than 10, we set the index to a max of 10 only
{
index = 10;
}
if (index <= 10) //this code is executed because index is now less or equal to 10
{
buffer[index] = holder; //the last index should equal to newline because we want to store the newline per char array
for (i = 0; i <= index; i++ )
{
printf("newidx %d, i %d , index %d \n", newidx, i, index);
storage[i + newidx] = buffer[i]; //this code will always store buffer[] into the last newidx value of storage[]
}
newidx = i + newidx; //update the newidx value
index = 0; //clear the index as we want to receive new char array again
}
}
else //as long the first input is other than newline, this part will get executed first
{
buffer[index] = holder; //every char is put into buffer[] except newline
index++;
printf("index %d \n", index);
}
}
storage[newidx] = '\0'; //char array is always terminated with NULL to indicate end of string
printf("Storage: %s", storage); //print everything inside storage[]. it is always printed from the first which is [0]
}
storage[newidx] = '\0'; //char array is always terminated with NULL to indicate end of string
printf("Not mine storage: %s", storage); //print everything inside storage[]. it is always printed from the first which is [0]
return 0;
}
storage never gets printed?
taking it down to 10 to show you something.
output look at index?
if you ever reached that 1000 it'd blow up.
and your last two lines of code are in the wrong place that is why they never get printed out.
Code:
userx@~/bin <> ./a.out
12345678901234567890
index 1
index 2
index 3
index 4
index 5
index 6
index 7
index 8
index 9
index 10
index 11
index 12
index 13
index 14
index 15
index 16
index 17
index 18
index 19
index 20
newidx 0, i 0 , index 10
newidx 0, i 1 , index 10
newidx 0, i 2 , index 10
newidx 0, i 3 , index 10
newidx 0, i 4 , index 10
newidx 0, i 5 , index 10
newidx 0, i 6 , index 10
newidx 0, i 7 , index 10
newidx 0, i 8 , index 10
newidx 0, i 9 , index 10
newidx 0, i 10 , index 10
^C
userx@~/bin <> gcc -Wall buffynotgood.c
userx@~/bin <> ./a.out
123456789012345678901234567890
index 1
Storage: index 2
Storage: index 3
Storage: index 4
Storage: index 5
Storage: index 6
Storage: index 7
Storage: index 8
Storage: index 9
Storage: index 10
Storage: index 11
Storage: index 12
Storage: index 13
Storage: index 14
Storage: index 15
Storage: index 16
Storage: index 17
Storage: index 18
Storage: index 19
Storage: index 20
Storage: index 21
Storage: index 22
Segmentation fault
userx@~/bin <>