-1

So I've been tried C for like one week and now I'm starting with buffers and everything going with that.

I saw many things like

char buffer[1024];

So I wonder what they means and how we know what to write between the [ ]

(I'm not very familiar with the vocabulary so forgive me if it's absolutely not clear).

Thanks

  • Do you already know the ideas of "arrays", "pointers", "call by reference"? – Yunnosch Mar 08 '20 at 12:53
  • 3
    It sounds to me that you need to buy a good book which teaches you C, or find a good tutorial on the Internet. Stack Overflow is a site for asking specific questions and not for providing general tutorials for individual people. Anyhow, after a quick Google search I found [this tutorial on arrays](https://www.tutorialspoint.com/cprogramming/c_arrays.htm), which should answer most of your questions. – Andreas Wenzel Mar 08 '20 at 12:58
  • Does this answer your question? [Creating arrays in C](https://stackoverflow.com/questions/21769904/creating-arrays-in-c) – Tarek Dakhran Mar 08 '20 at 13:04

4 Answers4

2

In C, we use [] to define an array of the given type. for example:

char buffer[1024];

is nothing but an array of characters (type char) with length of 1024.
and you can access each array member by its index, starting from 0.
As an example:

int number[10];
int i;
...
for (i=0; i<10; i++) {
    number[i] = i * i;
}
...
for (i=0; i<10; i++) {
    printf("Number %d is : %d\n", i, number[i]);
}

I think reading more about arrays and different data-types in C will help you with that.

mostafa.v
  • 21
  • 3
1

What you actually mean with "buffer" is what we and the C standard calls an "array".

An array is a sequence or collection of several objects of the same type, stored contiguous in memory, as opposed to a structure, which can hold objects of different types.

Quote from the yet-current C standard ISO/IEC 9899:2018 (C18), 6.2.5/20:

"An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified. Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is T, the array type is sometimes called "array of T". The construction of an array type from an element type is called "array type derivation"."


The term "buffer" is more used to describe a temporary storage, but an array is in most cases, in this explicit comparison, more "static"-like although you can create dynamic and variable-length arrays, too. Note that this "static" is not to be confused with the static keyword but the explanation and the use of that is far away from now.

Also a "buffer" might be just one single object of a certain type; it does not need to be an array of objects.

Quote from the Stack Overflow "buffer" tag wiki:

"A buffer is an area of memory set aside for temporary storage of data while it is being moved from one place to another. This is typically done to speed up processes with significant latency, such as writing to a disk, printer or other physical device. The output is ready to be sent to the device before the device is ready to accept it, so it is moved to the buffer so that the sending program does not have to continue waiting."


We can define an array by specify the amount of single objects to hold. This is made by the number inside [].

So,

char buffer[1024];

means buffer is an array with the identifier or name "buffer" which holds 1024 char objects.

You can omit the number of elements, if you initialize the array directly by its definition with appropriate values. The compiler will detect the amount of elements automatically and adjusts the size of the array accordingly.

int buffer[] = {1,2,3,4,5};   // That is permissible.
int buffer[];   // That is not permissible.

The [] always indicates either an array itself or a pointer to an array, when used f.e. as a parameter of a function:

void foo(char array_pointer[])

In this case array_pointer is not an array itself, but a pointer to an array of char in the calling function.


You should note, that index counting starts at 0 when you later want to access a certain object in the collection.

This means if you want to access the first element of buffer, you need to use:

buffer[0]

buffer[0] refers to the first element, buffer[1] to the second element, buffer[2] to the third element and so on.

0

This topic is very broad, and your question can have lots of answers.

Before i start, you have to know that a buffer is an area where you temporarely hold things so you can work on them and then send them elswhere or do something with the info you got from precessing the buffer. Here are some examples:

Text Input buffer: This is your case with char buffer[BUFFER_SIZE]. You usually do this when you want to process some text. This can fit problems like

A sentence can contain x maximum english characters and ends with a '.'. Count and display the number of words in it.

An actual example is when you want to log in on some site and you have to enter your ID and PassWord. The work behind this can be something like.

char IDBuffer[256];
char PWBuffer[256];

GetWindowText(IDWindow, IDBuffer, 256); // get the text the user has eneter as the ID
GetWindowText(PWWindow, PWBuffer, 256); // get the text the user has eneter as the PW

// do something with the two...

Now, the size of the buffer is specific to the case. It can be any constant number, or it can vary depending on what you want to do. If you want to take a deeper look and do a buffer with a varying size, i suggest you get familiar with dynamic allocation first. (like malloc and new)

What else is considered a buffer? (Oversimplified)

  • If you press some keys or use the mouse, all events are recorded on a Hardware Input Buffer and processed one by one. A buffer is required because, if a user does a large amount of events in a short time, and a single events needs time to be processed, other events would have been ignored if not held inside this queue like buffer.

  • When windows on your screen are updated, let's say you browse the net, the region on the screen where things change is invalidated and modifications are made on the Screen Buffer at those coordinates. Depending on the refresh rate of the monitor, this Screen Buffer is displayed at constant intervals. The monitor will not wait for Chrome to finish drawing, it will update (lets suppose) 60 times a second and will display what it has in the Screen Buffer no matter if it is complete or not.

  • When sound is played, the Sound Buffer is updated, and similarly to the Screen Buffer, the device will not wait for the buffer to be updated. At regular intervals, it plays what it has.

  • When you work with files, you get part, or even the entire file, in your RAM memory. Whenever you have something like this you can consider it a buffer.

    FILE* pFile = fopen("somefile.txt", "r");
    char buffer[16];
    fgets(buffer, 16, pFile);
    // .....
    
Tudor
  • 436
  • 3
  • 10
0

Any declaration of the form

T a[N];

declares a as an array of N elements of type T.

char buf[128];    // buf holds 128 chars
int  arr[32];     // arr holds 32 ints

An array is simply a contiguous sequence of objects of a given type. Arrays of character type (char, wchar_t, etc.) can store strings (text).

You’d access each element of the array by using a subscript:

for ( size_t i = 0; i < 128; i++ )
  putchar( buf[i] ); // write the i’th element of buf to stdout

If the expression between the [ and ] in the declaration is a constant expression (basically, something that can be evaluated at compile time), then you have what is sometimes called a “fixed-size” array.

int arr[128];

#define MAX_STRING_LENGTH 80
char input[MAX_STRING_LENGTH + 1];

If the expression between the [ and ] in a declaration is a variable or a function call or something that can’t be evaluated until runtime, then you have what is called a variable-length array:

int x;
x = some_value();
double foo[x];

Despite the name, a VLA can’t change its size after it’s been defined - “variable” simply means its size can be different each time it’s defined. VLAs were introduced in the 1999 version of the language, but were made optional in the 2011 version. Their uses are fairly limited.

Once defined, arrays cannot grow or shrink - you cannot change the number of elements in the array.

Arrays in C are “dumb” - the don’t store any metadata about their size, type, or anything else. You can’t query an array about its size (number of elements) or the number of elements currently in use. The sizeof operator can be used to get the array’s size in bytes, but that’s it. Any bookkeeping about the array’s size or number of elements in use must be tracked manually.

John Bode
  • 119,563
  • 19
  • 122
  • 198