Pointers in C, functions

Good afternoon, gentlemen, the actual question. I see periodically that in some functions pointers are inserted in this way: function(&name), then in this way: function(name).

Can someone explain what's wrong with this case? I understand how pointers work, but I can't even catch up with why the function has an input parameter char, and the address of the pointer is sent. And if possible, an example for using possible options. Thank you in advance!

Author: Arhadthedev, 2016-11-14

2 answers

In expressions, arrays, with rare exceptions, are implicitly converted to a pointer to their first element. So when you pass an array to a function, as in the example below

void f( int *a, size_t n )
{
    for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
    printf( "\n" );
}

int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

f( a, 10 );

Then you do not need to specify the & operator before the array name. The array has already been converted to a pointer of type int *.

However, if you are dealing with scalar objects, such as the expression a[0], which is the first element of the array, then there is a scalar object with the value 0, as follows from the definition of the array in the example above, if you want to pass its address to the above function, then you will need to write

f( &a[0], 10 );

In fact, the data is two calls

f( a, 10 );

And

f( &a[0], 10 );

They are equivalent, since in both cases the address to the first element of the array is passed.

In view of this, these function declarations are equivalent and declare the same function

void f( int a[10], size_t n )
void f( int a[20], size_t n )
void f( int a[], size_t n )
void f( int *a, size_t n )

You can include all these ads in the program at the same time, and the program will compile successfully.

The same is true for character arrays. Keep in mind that string literals also have an array type. And if there is a call of the form

h( "Hello" );

Where h is a function, the address of the first character of the literal is passed to the function, since, as described above, this literal, which is a character array, is implicitly converted to a pointer to its first character.

In conclusion, I will give you a demo program that also includes an example showing that string literals are arrays.

#include <stdio.h>

void f( int a[10], size_t n );
void f( int a[20], size_t n );
void f( int a[], size_t n );
void f( int *a, size_t n );

void f( int *a, size_t n )
{
    for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
    printf( "\n" );
}

void h( char c ) { printf( "%c\n", c ); }

int main(void) 
{
    int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    f( a, 10 );
    f( &a[0], 10 );

    h( "Hello"[0] );

    return 0;
}

Program output to the console

0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
H
 8
Author: Vlad from Moscow, 2016-11-14 15:02:16
char *name = "Я уже указатель :)";
function(name);
function(name+3); // Я указатель на 3 символа дальше, чем name
function(&name[3]); // А я не был, я был третьим символом строки, пока не взяли адрес
char name='q'; // Я символ, а не указатель
function(&name);
 5
Author: Qwertiy, 2016-11-14 14:25:35