Array of strings and pointers

Asked

Viewed 549 times

0

Read the comments in the code

 #include <stdio.h>
 int main(void) {
 char *nomes[2][2]; //Eu não entendo porque se eu tirar esse ponteiro * o codigo dá erro      
 nomes[0][0]="Misael";  
 nomes[0][1]="Zaes";
 nomes[1][0]="Joao";
 nomes[1][1]="Pedrao";

  printf("%s\n",nomes[0][0]); //Como um simples nomes[0][0] pode imprimir vários caracteres?
  printf("%s\n",nomes[0][1]); 
  printf("%s\n",nomes[1][0]);
  printf("%s\n",nomes[1][1]);

 return (0);
}

This code below is no longer right for storing and printing strings?

 #include <stdio.h>

 int main(void) {
 int N,i;

 scanf("%d",&N);

 char strings[N][256]; //Eu entendo que cada "N" posições vai ter um espaço de 256 caracteres. 

 for(i=0; i<N; i++)
   {
    scanf("%s",strings[i]);
   }

 for(i=0; i<N; i++)
 {
   printf("%s\n",strings[i]);
 }     

 return 0;
}
  • Any problems in the second code?

  • The question is whether you are trying to build an array of names or a two-dimensional array of names, like a table. These are two different things. In the first example you have a 2d array of pointers to char, i.e., a table with rows and columns of "names".

1 answer

2


You are creating a two-dimensional (2 X 2) matrix of strings. You must already know what a string, as I do I explained in another question from you, But you chose the answer that doesn’t explain anything and you ignored mine, so you’re in doubt about it. So if you go back in there and read again what I explained, you’ll know why * gives error. If only this doubt, this would be duplicate the other.

So you have something like this:

    |    0     |    1
--------------------------
 0  | (char *) | (char *)
--------------------------
 1  | (char *) | (char *)

Remembering that this char * is a memory address, nothing more than that, so the size of each element of this array is 4 or 8 bytes depending on your platform being 32 or 64 bits. The text is not there, it is in static area.

When does the printf() is asking him to print a text that is composed of a slot booked a string, so a guy char *, this is defined by %s, is a document convention of the function printf(), and then comes a \n which is a line break. Like the printf() does this is a detail of its implementation, but if we exemplify it in a very simplified way and naively just to understand it easier, what it does there is more or less this:

while (*string != '\0') putchar(*string++);

I put in the Github for future reference.

He’s taking the variable he receives and scanning it character by character and printing each one individually. The output condition is when it finds the null character (\0) which is the terminator of string, as already explained in link above, at this point and encloses the loop of repetition. In each print it increments the memory address to the next character and goes one step further in the loop. This is the basic way to traverse arrays in memory, being that a string is a array of char.

So he gets the address from where the string, in the specific case a static area of memory, and will scan that area of memory pointed by the element of your array two-dimensional until you find that terminator.

The difference from the second example is that this is reserved in stack (also called automatic memory) and not in the static area of memory, through the [256], even because when will change the value of the area at runtime cannot be in the static area. So the semantic difference of the first and second code. Understand about the use of static memory, stack and heap.

Tsto can help: What is the difference in the assignment of an already started matrix to an uninitiated one? (if it is not duplicate).

If you’re hard to understand, it’s because you skipped steps and you need to get back to the basics, understand these things that I’m talking about in the first place, you don’t build a house by starting from the rooftop, you need to build the foundation. The links that I went through in the previous answer has it all, if not directly at least within them. have to read, all your questions have been answered before here at Sopt. Links on the internet were created to be followed and give more details on the subject.

Browser other questions tagged

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