C 逐行读单词

C 逐行读单词,c,string,pointers,C,String,Pointers,我不知道哪里出了问题。第一个函数readline工作正常,但第二个函数不工作。错误:赋值从整数生成指针,但没有强制转换 #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 1024 char line[MAX_LINE]; int line_length = 0; int pocet_radek=0; int

我不知道哪里出了问题。第一个函数readline工作正常,但第二个函数不工作。错误:赋值从整数生成指针,但没有强制转换

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

#define MAX_LINE 1024

char line[MAX_LINE];
int line_length = 0;
int pocet_radek=0;
int input;
char cell;


char* readline(char* line){
line[line_length] = 0;
while ( (input = getchar()) != '\n' && line_length < MAX_LINE - 1) {
  line[line_length] = input;
  line_length++;
  }
  pocet_radek++;
  printf("%d\n", pocet_radek);
  return line;
}

char* read_cell(char* line, char* cell){
int i;
for(i=0;i<line_length;i++){
if( line[i]!=' ' || line[i]!='\n')
cell=line[i];
}
return cell;
}

int main()
{
printf("%s",readline(line));
printf("%s",read_cell(line, cell));
return 0;
}
#包括
#包括
#包括
#包括
#定义最大行1024
字符行[最大行];
int line_length=0;
int pocet_radek=0;
int输入;
炭细胞;
字符*读取行(字符*行){
行[行长度]=0;
while((input=getchar())!='\n'&行长度#包括
#包括
#包括
#定义最大行1024
字符行[最大行];
int line_length=0;
int pocet_radek=0;
int输入;
char*cell;//全局变量可以在任何函数中使用
char*readline(){//不需要任何参数
行[行长度]=0;
while((input=getchar())!='\n'&行长度
#包括
#包括
#包括
#包括
#定义最大行1024
int pocet_radek=0;
字符*读取行(字符*行,大小\u t大小){
int输入,行长度=0;
while((input=getchar())!=EOF&&input!='\n'&&line_长度
为什么要将变量作为参数传递?它们可以直接在函数中使用,因为它们是全局变量。谢谢,现在它可以工作了,但我在读取单词方面有问题。返回单元格始终为零…@user3699313,删除你的“答案”因为它没有回答问题。只需对其进行注释。@user3699313,什么是
readwords
?和
return cell
将返回输入单词的最后一个字符,前提是它不是空格或
\n
。您还没有真正提到您的程序应该做什么。所以我所能做的就是猜测…我需要读取一个单词并将其保存到单元格。单词可以用一个或多个分隔符分隔spaces@user3699313,哪个词?我们应该神奇地知道吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE 1024

char line[MAX_LINE];
int line_length = 0;
int pocet_radek=0;
int input;
char *cell; //global variables can be used in any function


char* readline(){ //No need for any parameters
   line[line_length] = 0;
   while ( (input = getchar()) != '\n' && line_length < MAX_LINE - 1) {
      line[line_length] = input;
      line_length++;
   }
   pocet_radek++; //This variable doesn't make sense as
   printf("%d\n", pocet_radek); //This will always print 1
   return line;
}

void read_cell(){
   if((cell=strtok(line," "))==NULL)
      return(-1);
   do
     printf("\"%s\"\n",cell);
   while((cell=strtok(NULL," "))!=NULL);
}

int main()
{ 
  printf("%s\n",readline());
  read_cell();
  return 0;
 }
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE 1024

int pocet_radek=0;

char *readline(char* line, size_t size){
    int input, line_length = 0;

    while ((input = getchar()) != EOF && input != '\n' && line_length < size - 1) {
        line[line_length++] = input;
    }
    line[line_length] = 0;
    if(!line_length && input == EOF)
        return NULL;
    pocet_radek++;
    printf("%d\n", pocet_radek);
    return line;
}

char *read_cell(char **pos){
    char *ret = *pos;
    if(!ret)
        return NULL;
    while(isspace(*ret))
        ++ret;
    if(!*ret)
        return NULL;
    *pos = ret;
    while(**pos && !isspace(**pos))
        ++*pos;
    if(**pos == 0)
        *pos = NULL;
    else {
        **pos = 0;
        ++*pos;
    }
    return ret;
}

int main(void){
    char line[MAX_LINE];
    char *p, *cell;

    while(readline(line, sizeof line)){
        printf("line:%s\n", line);
        p = line;
        while(cell = read_cell(&p))
            printf("word:'%s'\n", cell);
    }
    return 0;
}