Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8423955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:51:46+00:00 2026-06-10T03:51:46+00:00

Why do we write *bufp = buf since both are arrays so in my

  • 0
  1. Why do we write *bufp = buf since both are arrays so in my opinion it should be:

    static char bufp = buf;
    
  2. How does *bufp “know” from which position to start displaying ? It is not initialized to zero in any way. After assigning buf to bufp I’d expect that in return line it starts with the last entered char.

  3. Is unsigned char modifier used here just to omit the case of -1 being the input – meaning EOF on most systems?

#include "syscalls.h"
/* getchar: simple buffered version */
int getchar(void)
{
    static char buf[BUFSIZ];
    static char *bufp = buf; /* [1] */
    static int n = 0;
    if (n == 0) {            /* buffer is empty */
        n = read(0, buf, sizeof buf);
        bufp = buf;          /* ? [1] here it is written like in my question so which is true ? */
    }
    return (--n >= 0) ? (unsigned char) *bufp++ : EOF; /* [2] & [3] */
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:51:47+00:00Added an answer on June 10, 2026 at 3:51 am

    [1] char bufp = buf is incorrect, as buf is an array of char (and is internally an address, i.e. the content of a pointer), and char bufp would declare a unique character. char *bufp, instead, is a pointer to a char (to the first char, but you can access the next ones also).

    [2] bufp points to the buf array, ie its first character, at the beginning. And n is set to 0. bufp, buf and n are all static, meaning they “live” after the function returns – each of their value is initialized when the program loads, then the initialization is not performed anymore each time the function is called. Thus they “remember” the status of the buffer:

    `n` is the number of characters in the buffer, ready to be returned one by one,
    
    `bufp` points to the next character to be returned (when n > 0),
    
    and `buf` the array just holds the characters in the buffer.
    

    So to answer your [2] question,

    • when there is no character available (n == 0) a call to read fills the buffer buf and bufp points to the beginning of that array.
    • then as long as the buffer characters have not all been returned one by one (n > 0), *bufp is the next character to be returned ; *bufp++ gives the character to be returned and increments the bufp pointer by one.

    [3] The unsigned modifier prevents the compiler to propagate the *bufp character (8 bits) sign to the int other bytes (usually 32 bits, ie the 24 most significant bits), since an int is returned. Thus any character where code would be > 127 (for unsigned chars, or negative for signed char) is returned as is (eg (unsigned char)200 is returned as (int)200).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Write a small C program, which while compiling takes another program from input terminal,
Write a NASM macro: divide, which has 2 arguments, which specify unsigned integers in
Write a NASM macro: divide, which has 2 arguments, which specify unsigned integers in
I write app for Android which will be communicate with HTTP server. I need
Write a function lv: cfg -> (blabel -> ide set) , which computes the
Write an application that inputs one number consisting of FIVE digits from the user,
Write a SQL query to find those values from column A in table X
Write a program to input a series of 12 integers from the keyboard and
// Write a program to check whether or not two arrays are identical in
I write a hibernate.cfg.xml file almost the same from hibernate books like: <property name=hibernate.dialect>org.hibernate.dialect.MySQL5Dialect</property>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.