How to omit char matrix size in the function?

Asked

Viewed 94 times

1

I have a question now, I have a function implemented this way:

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

however I do not know the size I will receive as real parameter, if it is greater than 10 it will not work as expected, the parameter passed to the function may be like this:

mostrarMensagem("ola");

or so:

mostrarMensagem("ola156186485348545215554548843456etc");

Then I would need to not pass the initial size of the char matrix, I’m thinking, I could imagine something, but I think it will be a lot of work to develop what I want to emerge in my mind, there is some "quick" way to do this in c++?

2 answers

2

Hello, you can do this in two ways.

The first is to use vector notation within the function parameters:

mostrarMensagem(char msg[])
{
   cout << msg;
}

mostrarMensagem("oi");

In this case the function shows Remember takes as argument a char array, no matter the size of the array.

The second way is using pointer notation:

mostrarMensagem(char * msg)
{
   cout << msg;
}

mostrarMensagem("oi");

In these two cases the function shows Remember expects as argument a pointer to a char, because when you use char msg[] as function parameter is equivalent to use char * msg.

Since the char pointer received by the function stores the memory address of the first "string" character, the other characters will be in the memory positions subsequent to that pointer up to a limit that is the size of the "string", we can use pointer arithmetic to display the entire array starting from position *(msg + 0) while *(msg + i) != '\0' indicating the end of the "string":

mostrarMensagem(char * msg)
    {
       int i = 0;

       while(*(msg + i) != '\0') {
          cout << *(msg + i);
          i++;
       }
 }

mostrarMensagem("ola156186485348545215554548843456etc");

similarly, we can use the array notation that we are most used to, which in this case will give in the same:

mostrarMensagem(char * msg)
{
   int i = 0;

   while(msg[i] != '\0') {
      cout << msg[i];
      i++;
   }
 }

mostrarMensagem("ola156186485348545215554548843456etc");
  • Very good, thank you.

1


Note well the name of the string represents a pointer to the first position and the c/c++ language considers everything you see in front as part of the string , or in the case of a char vector "char str[N]" it has N characters , thus being 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 - very 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 , ie , 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 one must place "const" :ninja: in the prototype of function

void alterrString(const char* strReceived)

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

I hope I helped, I think I got a little carried away.

  • It helped yes, I had already done it, but it helped to improve what I had done here, I was using <string>, and using char stg* to pick up the string, and I didn’t even think to go straight with the pointer in the parameter, I thank you.

  • I’m getting this error while compiling: undefined reference toMinhaclasse::Minhafuncao(Std::__cxx11::basic_string<char, Std::char_traits<char>, Std::allocator<char> >) `

  • All right, I cleaned up the files-objects here, it was a file-object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.