Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is the buffer in this function prototype of sprintf() a buffer or just an array whose name happens to be buffer?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading