0

I'm learning how arrays are passed to a function and I got confused when the book (C for dummies) said that when you pass an array to a function, the address of the array's first element is what is passed to the function.

I know that an array like char name[] cannot hold memory address if it is passed to it so I wonder why the function say(char name[]) below can hold the memory address of the array's first element passed to it.

#include <stdio.h>

void say(char name[])
{
    printf("Welcome %s",name);
}

int main()
{
    char h[] = "Kyle";
    say(h);
    return(0);
}
Richie
  • 19
  • 5

4 Answers4

0

In C, an array is basically some contiguous variables in memory. If my array is int tab[4], my memory will contain each element (of int type in this example), one next to another. The variable tab works indeed as a pointer to its first element.

When you write tab[2], it is exactly the same thing for the compiler as if you had written tab + 2.

In your example, char name[] means the function expects an array. char h[] creates an array named h and initializes it with the correct size to contain "Kyle" (and one more char for the \0 character). When you pass h as a parameter for say(), the address of the first element is sent to your function, that's why it can access it without any problem.

  • "The variable tab works indeed as a pointer to its first element." Does it mean that inside the compiler, `tab` is a pointer variable? – Richie Apr 02 '19 at 09:50
  • It works as one but saying that an array is a pointer is not completely exact. Take a look [here](http://people.scs.carleton.ca/~mjhinek/W13/COMP2401/notes/Arrays_and_Pointers.pdf) – The Coding Penguin Apr 02 '19 at 11:08
0

C is pass-by-value except for arrays. Per 6.9.1 Function definitions, paragraph 10 of the C standard:

On entry to the function, the size expressions of each variably modified parameter are evaluated and the value of each argument expression is converted to the type of the corresponding parameter as if by assignment. (Array expressions and function designators as arguments were converted to pointers before the call.)

Your h array is passed by address to the function say in the name parameter.

So

void say(char name[])

is equivalent to

void say(char *name)

and can be called the exact same way:

int main()
{
    char h[] = "Kyle";
    say(h);
    return(0);
}
Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
0

inside parameters char name[] is exactly like char * name

note if you give a size like doing char name[123] that changes nothing and the compiler does not take into account the size at all

bruno
  • 32,421
  • 7
  • 25
  • 37
0

In C the array is represented by a pointer.
If it is a character array (string) it is terminated by the character \0. This is the end of the string.

If we have array from integers for example we have to pass the size as well to be sure that we do not access memory that is not meant for us.

Passing an array to a function will always result in passing only the pointer. foo(char name[]) and foo(char* name) are basically the same.

Definition of the array on the other hand differs.

char string[12]; // this will allocate memory(12 bytes)
string = "hello world";

char* string_ptr; // this is only a pointer (no memory allocation)
Petar Velev
  • 2,305
  • 12
  • 24