[Solved] Incorrect behavior of a pointer in function in C


There are numerous errors in the program although it compiled without any warnings. Chiefly the pointer types for your array, and the memory allocated. Secondly the function does not know how many words is allowed, and does not return how many were read – your method did not work at all (as in comments). Thirdly the string comparisons: you did not state the goals clearly, but in comment you want the “biggest string”. strcoll does not do that – it’s a lexical comparison, so I changed that section to find the longest string for the two sentences you enter. See comments, I made a large number of changes.

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

int returnArrayOfWords (char *str4Parsing, char *arrayParsed[], int maxtokens)  // added max
{
    char seps[] = " ,\t\n"; // separators
    char *token1 = NULL;
    char *next_token1 = NULL;
    int i = 0;

    // Establish string and get the first token:
    token1 = strtok_s( str4Parsing, seps, &next_token1);

    // While there are tokens in "str4Parsing" 
    while (token1 != NULL)
    {
        if(i >= maxtokens)
            return i;                                   // ignore the rest
        arrayParsed[i] = token1;
        printf( " %s\n", token1 );
        token1 = strtok_s( NULL, seps, &next_token1);
        i++;
    }
    return i;
}

int main (void)                                         // correct signature
{
    int i, j, n = 80; /*max number of words in string*/
    char arrS1[80], arrS2[80];
    //const char *w1, *w2; /*pointers*/                 // deleted
    char **ptrArray1, **ptrArray2;                      // changed type
    int currLength1 = 0, currLength2 = 0 ;
    int sizeArr1 = 0, sizeArr2 = 0;
    int maxLength = 0;
    char *wordMaxLength;                                // changed to pointer

    ptrArray1 = malloc(n * sizeof (char*));             // allocate mem for pointer array
    if (ptrArray1 == NULL)
        return 1;
    ptrArray2 = malloc(n * sizeof (char*));             // allocate mem for pointer array
    if (ptrArray2 == NULL)
        return 1;

    printf("Type your first string: "); 
    fgets(arrS1, 80, stdin);
    sizeArr1 = returnArrayOfWords(arrS1, ptrArray1, n); // indirection error, added max words, get actual num

    printf("Type your second string: ");
    fgets(arrS2, 80, stdin);
    sizeArr2 = returnArrayOfWords(arrS2, ptrArray2, n); // indirection error, added max words, get actual num

    for (i = 0; i < sizeArr1; i++)                      // this section rewritten
    {
        // to find the largest word in the array
        currLength1 = strlen(ptrArray1[i]);
        if(currLength1 > maxLength) 
        {
            maxLength = currLength1;
            wordMaxLength = ptrArray1[i];               // changed definition to pointer
        }
    }

    for (j = 0; j < sizeArr2; j++)
    {
        // to find the largest word in the array
        currLength2 = strlen(ptrArray2[j]);
        if(currLength2 > maxLength) 
        {
            maxLength = currLength2;
            wordMaxLength = ptrArray2[j];               // changed definition to pointer
        }
    }

    printf("The largest word is: %s", wordMaxLength);

    free(ptrArray1);                                    // added
    free(ptrArray2);
    return 0;
}

Program session:

Type your first string: one two three four
 one
 two
 three
 four
Type your second string: apple banana pear
 apple
 banana
 pear
The largest word is: banana

2

solved Incorrect behavior of a pointer in function in C