How to omit char array size in function?

1

I came up with a question now, I have a function implemented this way:

mostrarMensagem(char msg[10])
{
//aqui faço algo com a variável "msg".
}

But I do not know the size that I will receive as the actual parameter, if it is greater than 10 it will not work as expected, the parameter passed to the function might look like this:

mostrarMensagem("ola");

or so:

mostrarMensagem("ola156186485348545215554548843456etc");

So I would not need to pass the initial size of the char array, I'm thinking, I figured something out, but I think it will be hard work to develop whatever comes up in my mind, is there any "quick" way to do this in C ++? / p>     

asked by anonymous 29.06.2017 / 06:17

2 answers

1

Well, the string name represents a pointer to the first position, and the c / c ++ language considers everything that lies ahead as part of the string, or in the case of a char "char str [N]" vector it will have N characters, so a function that receives a string as a parameter will simply receive a pointer to the first position of the vector - all functions of the string library do this.

Example - quite simple:

void printString(char* strRecebida)/*não sabedo a tamanho da string voce pode usar este recurso de "char*" */
{
  printf("Sua string : %s",strRecebida);
}

CAUTION: Strings in c are passed by reference, that is, do not change this string within the function. : wacko: Example - a little more complicated:

void alterarString(char* strRecebida)
{
  strcpy(strRecebida,"ESTRAGUEI");
}
int main
{
  char str[20]="MINHA STRING";
  printf("Minha string: %s",str);/*IMPRIMIRA : Minha string : MINHA STRING*/
  alterarString(str);
  printf("Minha string alterada: %s",str);/*IMPRIMIRA Minha string alterada : ESTRAGUEI*/
}

To avoid this problem, you must put "const": ninja: in the prototype of the

void alterString (const char * strReceived)

the compiler will warn you if you try to change something in the string inside the

I hope I have helped, I think I got a little excited.

    
29.06.2017 / 08:55
2

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");
    
29.06.2017 / 08:54