Array storage

Asked

Viewed 401 times

1

I have a question about storing information in array, I thought, when inside a loop, an array of size 200, for example char nome[200] it would be populated continuously until there is no more space left to store the information, causing the system to return an error, but in some examples I have done, the array is reused in the loop and instead of being "completed" in the space it still has, it seems that the space that was used has the information replaced, if it really happens, why the array is not completed?

int main()
{
    int n,i;
    char ch;
    int length;
    scanf("%d", &n);
    char abc[200];
    for( i = 0; i < n; i++)
    {
        while( (ch = getchar() != '\n') && ch != EOF);
        fgets( abc,200,stdin);
        length = strlen( abc);
        printf("%d", length);
        printf("%s", abc);
    }
}

This is an example. if I type one sentence of size 100 and then another of size 100 the second will take the place of the first, instead of completing the other 100 that was left? and if I type one of size 200 and then another of size 100, the replacement will occur?

1 answer

2


Never has empty space in an array. When you declare char abc[200];, each of the 200 chars within this array has some unknown value.

For specification of the language C (which is the document officially describing the language C, published by ISO), when an array is declared this way, as a local variable, its initial or unspecified value, or is a special value called Trap representation, which should not be read or used in the program. In practice, its array abc will contain, at first, the value that happens to already be in the memory region that the compiler reserves for the variable. The important thing to keep in mind is: there is no concept of the "empty" variable in the C language, so there is no point in talking about "completing the array".

In your code, when you call fgets(abc, 200, stdin);, all the information that fgets has been what you went through: abc, 200 and stdin. In your noose for you call fgets exactly the same way by n times, and so it will behave the same way every time, writing the string from the beginning of the array abc.

When you pass an array to a function that receives a pointer, as is the case with the first parameter of fgets(), what you pass is actually the memory address of the first element of the array. Therefore, the two codes below are equivalent:

fgets( abc,200,stdin);

and

fgets(&abc[0],200,stdin);

If you want the string to be written in abc from a given position k 200, you should call it with:

fgets(&abc[k], 200 - k, stdin);

Why 200 - k in the second argument? Because the second argument tells how many positions from the address given in the first argument the function fgets() can use. If you are writing from position k, only exist 200 - k positions from there.

  • Excellent! thanks for the explanation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.