So, first you need to understand that the char variable type is responsible for storing one character at a time. And another, when working with vector remember that the first position is always the index [0]. See, you defined as follows:
char *nomes[5]={"Maria","Joaquina","Leticia","Edivalda","Romelia"};
At first you thought that since there are 5 names, it would be necessary to create a vector of 5 positions. It is wrong to interpret this way, you should put 4 positions, because as the index [0] counts, then there are five elements: [0]; [1]; [2]; [3]; [4].
Another point worth mentioning is that vectors can be treated as pointers in C, because you realize that by creating a vector of n positions it will point to the beginning of the defined sequence, do you agree? Therefore, the asterisk symbol is not required in this case.
As I mentioned, the char variable stores one character at a time, to be correct what you did should be like this:
char nomes[4]={"M","J","L","E","R"};
Or you could define a name only:
char nomes[5]={"Maria"}; //Strings sempre terminam com o /0 no final, o nome Maria tem 5 letras + o /0 no final, por isso 6 elementos.
However, let’s get to the point, I know the point of the question is to show the generic names defined, so let’s work on that statement. Look, to store these five names you have to create a two-dimensional array of characters, like this:
char nomes[5][10]={"Maria","Joaquina","Leticia","Edivalda","Romelia"}; //cinco nomes cada um com 9 caracteres (lembre-se do /0 no final);
From this, just pass the variable name as parameter to function displa_names.
The error will continue because as it comes to a matrix you can replace:
that:
void exibir_nomes (char *nome)
therefore:
void exibir_nomes (char nome[5][10])
Your go inside the function is also incorrect, see:
for (int i=0; i<5; i++)
printf("Nome[%d] = %s\n", i, *nome);
}
You won’t display anything with this, if you want to work with pointers remember what I said that vectors and pointers behave the same way. Therefore, the correct one would present each of the positions on the screen using the indexes:
for (int i=0; i<5; i++)
printf("Nome[%d] = %s\n", i, nome[i]);
}
Still the program will not execute what you wanted from the beginning, I adjusted your code and it got like this:
#include <stdio.h>
void exibir_nomes (char nome[5][10])
{
for (int i=0; i<5; i++)
{
printf("Nome[%d] = %s\n", i, nome[i]);
}
}
int main ()
{
char nomes[5][10]={"Maria","Joaquina","Leticia","Edivalda","Romelia"};
exibir_nomes(nomes);
}
This way the names are presented on the screen, however if you intend to work strictly with pointers to solve this issue, I believe it would be more challenging and even more complex. Furthermore, I believe that some tips here will serve as a basis for you to discuss this and other exercises in the same style.
The last form is almost correct, in the printf you take each name using name[i] instead of **name.
– epx
Thanks friend, it worked!! I was hammering here also trying to find a way while getting no answers, it seems that if the function is set to receive ** but when using I use only with a * it also works. I imagine it is because to get closer to the second pointer, which points even to the texts, just a dereferencing right, I did not study double pointers and I came across this at work, I did not know to leave the place, worth!
– Ricardo Farias