小编maz*_*ian的帖子

C代码中的字符串指针问题

这个C程序从键盘读取一行文本,然后在行中写入最长的单词.下面我的代码的问题是它只打印除了它的长度之外的最后一个单词,尽管一切看起来都很好.任何人都可以看到我的代码有问题吗?

#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 < …
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
150
查看次数

标签 统计

c ×1