Home > front end >  Is the buffer in this function prototype of sprintf() a buffer or just an array whose name happens t
Is the buffer in this function prototype of sprintf() a buffer or just an array whose name happens t

Time:08-22

The buffer is something that we declare when we write the program just like we declare an array or is it something that's already in the computer.

  1. List item

If I declare char buffer[MAX_LENGTH],is the buffer ,which I declare ,a buffer?

  1. List item Book called Pointers On C says the function sprintf() write values to the buffer.This is the function prototype from the book.int sprintf(char*buffer,char const * format,...)Is the buffer in this function prototype a buffer or just an array whose name happens to be buffer?
#include<stdio.h> 
#include<string.h>
int main(void)
{
    char buffer[50]={0};
    sprintf(buffer,"%c%c%c",'a','b','c');
    puts(buffer);
    return 0;
}

The result is abc.So is the buffer just a ordinary array?Is the buffer here absolutely different with the buffer in I/O?If the buffer here just an ordinary array,then why is the first argument in this function prototype in such a misleading way, instead of just declaring it as char*arr?

CodePudding user response:

Let's try to shed some light on the technical terms. This answer will not be a definition, just the trial to express in simple words, how the terms use commonly used.


An "array" is a sequence of elements of identical data type. Its "size" is the number of elements. Some programming languages allow non-negative numbers including zero, but some require at least one element. Commonly the individual elements are accessed by indexes.

"Array" describes only the data type, but not the intended usage.


A "buffer" is a specialized space used to, well, buffer values. The reason for buffering depends on the usage, for example a function needs readily available space for generated output.

A buffer can be an array, but it also can be a single value.

"Buffer" does not describe a data type, but an intended usage.


The names of parameter shall tell the user the purpose of the parameter. This is true for all names, not only parameter names.

The data type is clear from the specification. It always shows a lack of understanding if a name describes the data type, not the intention.

So, sprintf(char *buffer, const char *format, ...); is quite OK.

In contrast, sprintf(char *arr1, const char *arr2, ...); brings confusion.

CodePudding user response:

buffer is nothing more than a chunk of memory used to store the data. So if you store characters - it will be simply a char array.

If your data has another type, you will need "a simple" array of that type. Example: jf you want to store double you will need array of doubles

Of course, every array of something is just a chunk of memory.

In declaration:

int sprintf(char * restrict s, const char * restrict format, ...);

function sprintf takes pointers to char as parameters, but those pointers simply reference some chunks of memory. You can call them bufferes.

  •  Tags:  
  • c
  • Related