Good afternoon, gentlemen, actually a question. I see periodically that in some functions, pointers insert something like this: function(&name) , then like this: function(name) .

Can someone explain that yes how about this business? I understand how pointers work, but I can't even catch up with why the function has an input parameter char , and they send the address of the pointer. And if possible, an example to use the possible options. Thank you in advance!

  • one
    Show me an example of code where you are having trouble. - KoVadim
  • why the function has the char input parameter, and the pointer address is sent — no, the compiler rejects such code. - D-side
  • @KoVadim, suppose scanf. - Qwertiy

2 answers 2

With expressions, expressions 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 ); 

it is not necessary to specify the & operator before the array name. The array has already been converted to an int * type pointer.

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

 f( &a[0], 10 ); 

In fact, these two calls

 f( a, 10 ); 

and

 f( &a[0], 10 ); 

equivalent, since in both cases the address is transferred to the first element of the array.

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 announcements in the program at the same time, and the program will be successfully compiled.

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 certain function, then the address of the first character of the literal is transferred 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 a demo program, which 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; } 

Output of the program to the console

 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 H 
     char *name = "Я уже указатель :)"; function(name); function(name+3); // Я указатель на 3 символа дальше, чем name function(&name[3]); // А я не был, я был третьим символом строки, пока не взяли адрес 
     char name='q'; // Я символ, а не указатель function(&name); 
    • Can I have another question? also according to pointers, why such a construct is not true: * name = "hello"; if it's a type unrequest. Through it, the text is called, and such a construct is correct name = "hello"; - Corle
    • @Corle, dereferencing a pointer to a char is a reference to char — one character. And the string in quotes is a pointer to char (and constant). You can reassign a pointer, but you cannot assign a pointer to a symbol. - Qwertiy