0

This C program reads a line of text from keyboard and then writes the longest word in the line. The issue with my code below is that it only prints the last word apart from its length, although everything seems fine. Can anyone see a problem with my code?

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

#define MAX 132
#define MAXW 30

int Len_w[MAXW];
int Max_V(int vf[], int len);

main()
{
    char s[MAX+1], w[MAXW], *Ind_w[MAXW],*p,out[MAXW];
    int k=0, i=0, Maximum, g=0;
    printf("\nInsert the line....\n");
    p=fgets(s, MAX, stdin);

    while(sscanf(p, "%s%n", w, &k)==1){
        Len_w[i] = strlen(w);
        Ind_w[i] = w; //the issue is here!!
        p+=k+1;
        i++;
    }
    Maximum = Max_V(Len_w,i);

    for(g=0;g<i;g++){
        if(Len_w[g] == Maximum){
                //sscanf(Ind_w[g],"%s",out);
                printf("\n%s", Ind_w[g]);


            }
    }

    return 0;
}

/*----------------------------------------------------------------------------*/
int Max_V(int vf[], int len)
{
  int j; int Max;
  Max=*vf;

  for(j=1; j < len; j++)
  {
     if(*(vf+j) > Max)
     {
         Max=*(vf + j);
     }
  }

  return Max;
}
/*----------------------------------------------------------------------------*/
1
  • 5
    Pointer arithmetic is fun, but regular indexing tends to make such code more readable. Commented Jan 3, 2013 at 16:00

2 Answers 2

5
Ind_w[i] = w;//the issue is here!!

You let all pointers in Ind_w point to the same buffer, which is overwritten for each entered word. So only the last entered word remains "visible".

If you have it,

Ind_w[i] = strdup(w);

is a simple solution. Otherwise

Ind_w[i] = malloc(strlen(w)+1);
strcpy(Ind_w[i], w);

Both ways require the pointed-to memory to be freed when it is no longer used.

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

23 Comments

Remember; you'll have to free the memory allocated by strdup or malloc yourself, afterwards.
can i ask something related to this matter through email
@maziarparsaeian I'd rather keep it on the site. What is it about?
it is another similar solution with no duplication that works but i do not get the point
it is written by one of the friends in my pc,that s why
|
2
Ind_w[i] = strdup(w);//the issue is here!!

You have to duplicate the w buffer each time you read the buffer and not use the same buffer for all reading. with the way you did you will have all the array elements pointing to the same buffer and this buffer is containing the same string which is the last one read with sscanf

Note: you have to free all the duplicated buffers when they come useless. You can do it by go over the array of pointers and free each element (pointer)

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.