arrays - Incorrect behavior of a pointer in function in C -
i have problem following program.
the main function calls function returnarrayofwords(arrs1, &ptrarray1)
twice. on first call, array parsed perfectly, , afterward pointer points first word. on other hand, after second call, pointer of first array points second word, third word, or first word, should point first word -- else.
why function misbehave when called second time?
#include <stdio.h> #include <string.h> #include <stdlib.h> void returnarrayofwords (char *str4parsing, char *arrayparsed[]) { char seps[] = " ,\t\n"; // separators char *token1 = null; char *next_token1 = null; int = 0; // establish string , first token: token1 = strtok_s( str4parsing, seps, &next_token1); // while there tokens in "str4parsing" while (token1 != null) { // next token: if (token1 != null) { arrayparsed[i] = token1; printf( " %s\n", token1 ); token1 = strtok_s( null, seps, &next_token1); i++; } } } //int main1 () int main () { int i, j, n = 80; /*max number of words in string*/ char arrs1[80], arrs2[80]; const char *w1, *w2; /*pointers*/ char *ptrarray1, *ptrarray2; int currlength1 = 0, currlength2 = 0 ; int sizearr1 = 0, sizearr2 = 0; int maxlength = 0; char wordmaxlength ; printf("type first string: "); fgets(arrs1, 80, stdin); returnarrayofwords(arrs1, &ptrarray1); sizearr1 = sizeof(ptrarray1) / sizeof(ptrarray1[0]); printf("type second string: "); fgets(arrs2, 80, stdin); returnarrayofwords(arrs2, &ptrarray2); sizearr2 = sizeof(ptrarray2) / sizeof(ptrarray2[0]); (i = 0; < sizearr1; i++) { // find largest word in array w1 = &ptrarray1[i]; currlength1 = strlen(w1); (j = 0; j < sizearr2; j++) { w2 = &ptrarray2[j]; currlength2 = strlen(w2); if (strcoll(w1, w2) == 0) // compares strings { if (currlength2 >= maxlength) // in 0th element -> length of longest word { maxlength = currlength2; wordmaxlength = ptrarray2[j]; } } } } printf("the largest word is: %s", wordmaxlength); return 0; }
edit:
here's latest version of code, here works fine, managed fix myself. i'm posting in case needs solution:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stddef.h> #include <ctype.h> #define n 80 /*max number of words in string*/ /* arrays , pointers */ int returnarrayofwords (char *str4parsing, char *arrayparsed[]) { // returns length of array int elarr = 0, na = 0; char *delim = " .,;-\t\n"; /* word delimiters */ char *next_token1 = null; char *ap = str4parsing; /* pointer str4parsing */ (ap = strtok_s (str4parsing, delim, &next_token1); ap; ap = strtok_s( null, delim, &next_token1)) { arrayparsed[na++] = ap; elarr++; } return elarr; } void printarr(char *arr[]) { int i; ( = 0; < n; i++) { printf("element %d %s \n", i, arr[i]); } } void findlargestword(char *ptrarray1[], int sizearr1, char *ptrarray2[], int sizearr2) { size_t maxlength = 0; char *wordmaxlength = null ; int = 0, j = 0; char *w1 = null, *w2 = null; /*pointers*/ size_t currlength1 = 0, currlength2 = 0 ; (i = 0; < sizearr1; i++) { // find largest word in array w1 = (ptrarray1[i]); // value of address (ptrarray1 + i) currlength1 = strlen(w1); //printf("the word first string is: %s , length : %d \n", w1, currlength1); // check point (j = 0; j < sizearr2; j++) { w2 = (ptrarray2[j]); // value of address (ptrarray2 + j) currlength2 = strlen(w2); //printf("the word second string : %s , length : %d \n", w2, currlength2); // check point if (strcoll(w1, w2) == 0 && currlength1 == currlength2) // compares strings { if (currlength2 >= maxlength) // in variable maxlength -> length of longest word { maxlength = currlength2; wordmaxlength = w2; //printf("the largest word : %s , length : %d \n", wordmaxlength, maxlength); // check point } } } } printf("the largest word is: %s \n", wordmaxlength); printf("its length is: %d \n", maxlength); } void typearray (char *arrs1) { int err = 0; if (!fgets (arrs1, n, stdin)) { /* validate 'arrs1' */ fprintf (stderr, "error: invalid input string.\n"); err = 1; } while (err == 1) { if (!fgets (arrs1, n, stdin)) { /* validate 'arrs1' */ fprintf (stderr, "error: invalid input string.\n"); err = 1; } } } int main(void) { char arrs1[n], arrs2[n]; char *ptrarray1[n] = {null}, *ptrarray2[n] = {null}; int sizearr1 = 0, sizearr2 = 0; printf("type first string: "); typearray (arrs1); sizearr1 = returnarrayofwords (arrs1, ptrarray1); // sizearr1 = number of elements in array 1 printf("type second string: "); typearray (arrs2); sizearr2 = returnarrayofwords (arrs2, ptrarray2); // sizearr2 = number of elements in array 2 findlargestword(ptrarray1, sizearr1, ptrarray2, sizearr2); return 0; }
there numerous errors in program although compiled without warnings. chiefly pointer types array, , memory allocated. secondly function not know how many words allowed, , not return how many read - method did not work @ (as in comments). thirdly string comparisons: did not state goals clearly, in comment want "biggest string". strcoll
not - it's lexical comparison, changed section find longest string 2 sentences enter. see comments, made 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 = 0; // establish string , first token: token1 = strtok_s( str4parsing, seps, &next_token1); // while there tokens in "str4parsing" while (token1 != null) { if(i >= maxtokens) return i; // ignore 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 pointer ptrarray1 = malloc(n * sizeof (char*)); // allocate mem pointer array if (ptrarray1 == null) return 1; ptrarray2 = malloc(n * sizeof (char*)); // allocate mem pointer array if (ptrarray2 == null) return 1; printf("type first string: "); fgets(arrs1, 80, stdin); sizearr1 = returnarrayofwords(arrs1, ptrarray1, n); // indirection error, added max words, actual num printf("type second string: "); fgets(arrs2, 80, stdin); sizearr2 = returnarrayofwords(arrs2, ptrarray2, n); // indirection error, added max words, actual num (i = 0; < sizearr1; i++) // section rewritten { // find largest word in array currlength1 = strlen(ptrarray1[i]); if(currlength1 > maxlength) { maxlength = currlength1; wordmaxlength = ptrarray1[i]; // changed definition pointer } } (j = 0; j < sizearr2; j++) { // find largest word in array currlength2 = strlen(ptrarray2[j]); if(currlength2 > maxlength) { maxlength = currlength2; wordmaxlength = ptrarray2[j]; // changed definition pointer } } printf("the largest word is: %s", wordmaxlength); free(ptrarray1); // added free(ptrarray2); return 0; }
program session:
type first string: 1 2 3 4 1 2 3 4 type second string: apple banana pear apple banana pear largest word is: banana