Hello, you can do this in two ways.
The first of these is to use vector notation within function parameters:
mostrarMensagem(char msg[])
{
cout << msg;
}
mostrarMensagem("oi");
In this case the showMessage function takes as its argument an array of char, regardless of the size of the array.
The second way is to use pointers notation:
mostrarMensagem(char * msg)
{
cout << msg;
}
mostrarMensagem("oi");
In these two cases the showMessage function expects as a argument a pointer to a char, because when you use char msg[]
as a function parameter it is equivalent to using char * msg
.
Once the char pointer received by the function stores the memory address of the first character of the string, the remaining characters will be in the memory positions subsequent to that pointer up to a limit that is the length of the string, we can use pointer arithmetic to display the entire array starting from position *(msg + 0)
while *(msg + i) != '%code%'
indicating the end of the string:
mostrarMensagem(char * msg)
{
int i = 0;
while(*(msg + i) != 'mostrarMensagem(char * msg)
{
int i = 0;
while(msg[i] != 'mostrarMensagem(char msg[])
{
cout << msg;
}
mostrarMensagem("oi");
') {
cout << msg[i];
i++;
}
}
mostrarMensagem("ola156186485348545215554548843456etc");
') {
cout << *(msg + i);
i++;
}
}
mostrarMensagem("ola156186485348545215554548843456etc");
Similarly, we can use the array notation we are most familiar with, which will then give the same:
mostrarMensagem(char * msg)
{
cout << msg;
}
mostrarMensagem("oi");