1

I was studying a socket tutorial in C, and I came across this:

char buffer[256];
bzero(buffer, 256);
n = read(newsockfd, buffer, 255);

However, as far I know, shouldn't the buffer variable be passed by reference (using &) to bzero and read functions? As well, the man pages for bzero and read specify that these arguments are pointers. Am I missing something?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Gabriel Lins
  • 65
  • 1
  • 7
  • 1
    The line `char buffer[256];` Allocates an array of size `256*sizeof(char)` on the stack and assigns the variable `buffer` a pointer to the start of the array. This means `buffer is of type `char*`, which is what those system calls want. – Paul Sep 13 '21 at 16:48
  • 7
    @Paul Please do not propagate the myth that arrays are pointers. – Ian Abbott Sep 13 '21 at 16:49
  • 1
    You can't pass an array in C, which decays to a pointer to its first element. – Weather Vane Sep 13 '21 at 16:50
  • BTW, I'm pretty sure `bzero` is a deprecated POSIX function, you should use `memset(buffer, 0, 256)` instead. – mediocrevegetable1 Sep 13 '21 at 17:11
  • 1
    @mediocrevegetable1 Thanks! I did noticed something weird about it, was doing the same of memset. However, isn't my code, it is from a tutorial. – Gabriel Lins Sep 13 '21 at 17:15
  • @IanAbbott I didn't? The pointer is to the beginning of the array. – Paul Sep 14 '21 at 00:10
  • @Paul The variable `buffer` holds the array itself, not a pointer to the start of the array. It "decays" to a pointer in most expressions (except when it is the operand of `sizeof`, `_Alignof`, or unary `&`). – Ian Abbott Sep 14 '21 at 07:10

3 Answers3

4

The variable buffer is declared as having an array type

char buffer[256];

Array designators used in expressions are implicitly converted (with rare exceptions) to pointers to their first elements.

The function bzero changes elements of the array buffer So in fact all elements of the array are passed to the function by reference through the pointer to the first element of the array due to the mentioned above implicit conversion

bzero(buffer, 256);

As a result, the function can access and change any element of the array using the passed pointer and the pointer arithmetic.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

C has no pass by reference. You cannot pass things by reference in C. You can only pass by value.

The value passed here is a pointer to the first object in buffer. That is all either bzero or read need because an array always occupies a contiguous chunk of memory.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

When you call functions with an array,

char buffer[256];
bzero(buffer, sizeof buffer);

the array decays into a pointer:

void bzero(char *buffer, size_t len) {
    //...
}

If you on the other hand take the address of the array:

foo(&buffer);

A matching function would have to have a signature like:

void foo(char(*buffer)[256]) {
    // ...
}

The latter is pretty uncommon since it'd only accept arrays of a certain size.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108