0

I want to find the number of elements in that array, but as far as I know I'm not allowed to use strlen or sizeof. strlen(array[0]) gives out 5 cause apple consists of 5 characters, but I need length to equal to 4, because the arrays contains 4 words. Any suggestions?

#include <stdio.h>
#include <string.h>

int main() {
    char array[10][100] = {"apple", "banana", "strawberry", "grapefruit"};
    int length = strlen(array[0]);
    printf("%d", length);

    return 0;
}
7
  • Iterate over the words until you hit a word with zero length (or you hit the end of the array). To avoid the need for strlen, simply test the first character of each word (0 => empty string). Commented Sep 30, 2022 at 14:26
  • char array[10][100]; has 10 elements. Those elements are arrays of 100 char. If you decide to initialize only 4 of them, then the number of initialized elements if 4 but the size is still 10. You don't need strlen or size; you initialized 4 of them so 4 of them are initialized. And the size remains 10. Don't compute anything at run time; this is entirely knowable at compile time, and doing any computation at runtime is wasted cycles. "As far as I know I'm not allowed to use ... sizeof". Well, that's just silly, since sizeof is the right operator if you don't want to hard-code 4 or 10 Commented Sep 30, 2022 at 14:31
  • @WilliamPursell... <pedantic>all 10 elements are initialized, there is no partial initialization in C</pedantic> Commented Sep 30, 2022 at 14:44
  • 1
    Why did you write 10 if you needed 4? Commented Sep 30, 2022 at 14:46
  • @pmg A valid point. 6 of the 10 elements are initialized to be all zero. I still don't understand the point of this question! Commented Sep 30, 2022 at 14:55

2 Answers 2

2

You can search over array[i] until you find an empty string:

size_t arrayLength = 0;
for (size_t i=0; i<10; i++)
{
  if (array[i][0] != '\0')
  {
    arrayLength++;
  }
  else
  {
    // with brace initialization, there will be no other words in the
    // array, we're safe to break
    break;
  }
}

When you use a brace-initialization list like that with a specified size, it will initialize the array to the contents you provided, and 0 for everything else. The pointers in the first dimension will still be valid (ie, array[i] is not-NULL for all i [0, 10) ), but they point to empty strings.

Demonstration

Sign up to request clarification or add additional context in comments.

3 Comments

ITYM if (array[i][0]) to check for empty string rather than NULL pointer. ... ideone.com/8Yf72w
@pmg Absolutely correct. In fact my last answer was completely wrong,, the first dimension of pointers are valid for each array[i], but after the 4th word they don't point to anything. Whoops.
They decay to point to the empty strings, just as with the assigned elements.
1

If you want to know the size of the array, the "correct" operator to use is sizeof. For example:

#include <stdio.h>
#include <string.h>

int 
main(void)
{
    char array[][100] = {"apple", "banana", "strawberry", "grapefruit"};
    printf("%zd\n", sizeof array / sizeof *array);

    return 0;
}

I suspect the real issue here is that the char array[10][100] ought to be replaced with char array[][100]. There is no need to do any run-time computation here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.