I've a string array:
char array[128][128];
To add strings in this array, I read a file, and append a line if "192.168.101.2" is contained in that line:
while ((read = getline(&line, &len, fp)) != -1) {
if (strstr(line, "192.168.101.2") != NULL) {
printf("%s\n", line);
strcpy(array[i], line);
i++;
}
}
Now, I would like to know how many strings this array contains. I've tried:
sizeof(array)/sizeof(array[0]), but it always returns 128. How can I achieve this?
And, how can i pass an array of strings to a function? I've tried:
void array_length(char array[int][int]);
but:
main.c:15:31: error: expected expression before ‘int’
15 | void array_length(char array [int][int]);
iwill have the number of strings in the array. Save it and use it.i? You must have that index to store into the correct element of your array. If you really want to do it without a counter you can add a sentinel, i.e. an array element that is defined to be invalid. For strings that could be an empty string after the last string read from your file.