-3

When I pass the array using getEnglishWord(counterLine); counterLine is array. But when it goes in the void getEnglishWord(int countLine[]) function the only passed is countLine[0] or the first index in the array countLine[].

CODE

if (countWords == countWordsInput + 1)
                        {
                            fclose(fpB);
                            getEnglishWord(counterLine);
                        }

Code in void getEnglishWord(int counterLine[]):

int c;
        int countLine = 0, countWords = 0, countLetters = 0;
        char translatedWords[words][letters];
        int indexCount = 0;
        c = getc(fpE);

        for (int y = 0; y < words; y++)
        {
            for (int x = 0; x < letters; x++)
                translatedWords[y][x] = NULL;
        }
        while (c != EOF)
        {
            if (c == '\n')
            {
                if (countLine == counterLine[indexCount])
                {
                    translatedWords[countWords][countLetters] = c;
                    countLetters++;
                }
                indexCount++;
                countLine++;
            }

            c = getc(fpE);
        }
        fclose(fpE);
        _getch();

2 Answers2

2

In C, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array. The exceptions to this rule are when the array expression is the operand of the sizeof or unary & operators, or is a string literal used to initialize another array in a declaration.

Array objects themselves are not pointers; they do not store a pointer value.

So, when you call

getEnglishWord(counterLine);

the expression (not the object!) counterLine is converted from type "N-element array of int" to "pointer to int", and the value of the expression is the address of the first element of counterLine (&counterLine[0]).

In the function prototype

void getEnglishWord(int counterLine[])

the declaration int counterLine[] is treated as though it had been written int *counterLine; IOW, counterLine is declared to be a pointer to int, not an array of int (this is only true for function parameter declarations).

So all your function can ever receive is a pointer to the first element of the array. However, you can still use the [] subscript operator on pointer values (in fact, it's defined in terms of pointer operations, but we don't need to get into that now). You can still access all the elements of counterLine from your function, you just need to make sure you don't try to access elements past the end of counterLine. It's usually a good idea to pass the array size as a separate parameter with the array:

void getEnglishWord ( int *counterLine, size_t counterLineSize )
{
  ...
  if ( i < counterLineSize )
    do_something_with( counterLine[i] );
  ...
}

size_t numElements = ...; // see below
getEnglishWord( counterLine, numElements );

There are several ways to determine the size of an array. You can use the sizeof operator on the array itself, giving the total number of bytes in the array, and dividing that by the number of bytes in a single array element:

int counterLine[N];
size_t numElements = sizeof counterLine / sizeof counterLine[0];

This only works for array expressions, though; you can't use this on pointers that point to the first element of an array. You'll just get the size of the pointer divided by the size of the element, which won't give you what you want.

Otherwise, just pass N:

getEnglishWord( counterLine, N );
John Bode
  • 119,563
  • 19
  • 122
  • 198
1

This code here is working:

void getEnglishWord ( int *counterLine, size_t counterLineSize )
{
  ...
  if ( i < counterLineSize )
    do_something_with( counterLine[i] );
  ...
}

size_t numElements = ...; // see below
getEnglishWord( counterLine, numElements );
laurence keith albano
  • 1,409
  • 4
  • 27
  • 59