This question has already been answered:

There is a code:

void LongestWord(char* sen[]) { // Find & print longest word } int main(int argc, char* argv[]) { // Disable stdout buffering setvbuf(stdout, NULL, _IONBF, 0); // Keep this function call here LongestWord(gets(stdin)); return 0; } 

Task: How to calculate the number of sen[] elements in the LongestWord() function?
As far as I know, it is impossible to calculate through the pointer. Usually in such cases the size of the array is transferred along with the size_t type. This code is from coderbyte .

Maybe I'm not catching up with something?

Marked as a duplicate by MSDN.WhiteKnight , ߊߚߤߘ , Community Spirit Jul 3 '18 at 6:06 pm .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 2
    1. None 2. This code does not even compile. - Qwertiy
  • I will say differently, but why does main have argc, which determines the number of elements in argv? - KoVadim
  • Why not compile? This is the task behind the Coderbyte resource. Here is the link: coderbyte.com/editor/guest:Longest%20Word:C - Djasir
  • Because error C2664: gets: cannot convert parameter 1 from "FILE *" to "char *" - MSDN.WhiteKnight
  • I join in @ MSDN.WhiteKnight: gets() takes as an argument not the stream itself (this function is already nailed to stdin ), but the output buffer, into which the contents of this stream are extracted to the nearest newline character. - ߊߚߤߘ

2 answers 2

(Correct definition: void LongestWord(char sen[]) , without an asterisk, as an array of characters.)

sen is a special array, exactly a string in C.

(sen = sentence, i.e. sentence.)

Since strings in C end with a binary zero (the \0 character), there is no problem in determining its length (= the size of the sen array) sen is just the number of characters before the first appearance of the \0 character.

But you don’t even have to do it manually, because the standard function strlen() does it (string's length is the length of the string).

  • Actually, it passes a pointer to the char* sen[] pointer, i.e. array, I think. - LLENN
  • Yes, it is an array of pointers, no it is not a string. - Djasir 1:02 pm
  • @ LENN, sen definitely a string. You can remove the asterisk from the parameter description or save it, anyway. A string is an array of characters, and the bare array name is a pointer to its first element . It doesn't matter if you add an asterisk in front of that name in the parameter description or not. - MarianD 1:51 pm
  • @Djasir, see my comment for LLEEN. - MarianD 1:53 pm
  • I apologize wildly, but how does this char * sen [] differ from char * argv []? In sen string i.e. through sen [i], you can get a string symbolically and in argv [i] by line. Or is it here in sen [i] that the lines were specially cut so that each pointer points only to one character? Those. inserted at the end of a binary zero? Can you provide a link to a graphic explanation of this topic? - Djasir

In the pros, in contrast to C, it is possible to know the size of the array when transferring it to the template by reference:

 template<std::size_t N> char *LongestWord(char *(&text)[N]); 
  • The problem must be solved in C) - Djasir
  • 3
    A. Well, then either pass the size of the array as a separate argument, or you must put the last element sentinel, as the null character at the end of the string, for example. In the argc-argv pair, both methods are used: argv[argc] == NULL . - bipll