Rigel Kent Carbonel Rigel Kent Carbonel - 5 months ago 14
C Question

Why I can only pass the first index of my Array?

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();

Answer

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 );