0

i'm searching for a long time on net. but no use. please help me.

/* getch and ungetch to handle EOF Character In all the ungetch and getch 
* functions written so far, the buf is declared as char buf[BUFSIZ]. 
* Changing this to int buf[BUFSIZ] enable it to handle EOF. As EOF is an 
* integer declared in stdio.h having the value -1 */ 
#include<stdio.h> 
#define BUFSIZE 100 
int getch(void); 
void ungetch(int c); 

char buf[BUFSIZE]; /* buffer for ungetch */ 
int bufp = 0; /* next free position in buf */ 
int main(void) 
{ 
    int c; 

    c = '*'; 

    ungetch(c); 
    while((c=getch())!=EOF) 
        putchar(c); 
    return 0; 
} 

/* getch: get a (possibly pushed back) character */ 
int getch(void) 
{ 
    return (bufp > 0) ? buf[--bufp] : getchar(); 
} 

/* ungetch: push a character back onto the input */ 
void ungetch(int c) 
{ 
    if (bufp >= BUFSIZE) 
        printf("ungetch: too many characters \n"); 
    else 
        buf[bufp++] = c; 
}

i wanted to use pointer to achieve same effect. But i failed( wrong codes are as follows). Bufp = buf has a error.The error was that Bufp: different level between the indirection of int and char*. In my limited experience with pointer, the type of pointer Bufp is char* ,it points to char data type.char buf[BUFSIZE]; means buf[] is a array of characters. If what i said was right, why did the problem appear? was there something i ignored?

char buf[30];
char *Bufp;

Bufp = buf;
int getch(void)
{
    return (Bufp >= buf) ? *(--Bufp) : getchar();
}

void ungetch(int c)
{
    if (c != EOF)
        *Bufp++ = c;
    else
        printf("no space\n");
}
neil guo
  • 3
  • 3
  • which line has the error? – user253751 Apr 08 '22 at 13:52
  • Please [edit] your question and copy&paste the complete error message including all lines related to the error. You cannot place an assignment `Bufp = buf;` outside a function. Maybe `char *Bufp = buf;`? If you have difficulties in debugging, then writing the code in a more verbose way might help. (Use `if`/`else` instead of conditional operator `?`/`:`, separate pointer incrementing and decrementing from dereferencing the pointer.) `printf` as error handling inside a function that normally does not print anything is bad design. Function `ungetch` does not prevent out-of-bounds access. – Bodo Apr 08 '22 at 14:06
  • Note that if you print an error message, it should be written to `stderr`, not `stdout`. The space at the end of the output (before the `\n`) is undesirable too. More generally, it probably isn't a good idea to print a message in a low-level function like that — it should be a non-void function that reports the error via a return value (e.g. EOF, returning the character ungotten when it succeeds, or maybe returning 0 vs EOF for success/failure). – Jonathan Leffler Apr 08 '22 at 15:32
  • @user253751 `Bufp = buf` this line. – neil guo Apr 08 '22 at 16:21
  • does this other question answer your question? https://stackoverflow.com/questions/50661263/why-cant-i-assign-values-to-global-variables-outside-a-function-in-c – user253751 Apr 08 '22 at 16:23
  • @neilguo Please don't modify your question in a way that it the original code is replaced by something that does no longer show the problem. Please undo the changes and write an answer instead. – Bodo Apr 08 '22 at 19:59
  • @Bodo well, i didn't change original code and just clarify which line had problem in my question. – neil guo Apr 09 '22 at 02:34

0 Answers0