Array of strings and pointers

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);
}

Is not this code 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;
}
    
asked by anonymous 11.08.2018 / 20:06

1 answer

2

You are creating a two-dimensional (2X2) array of strings . You should know what a string is, as I I explained in another question from you , but you preferred the answer that explains nothing and ignored mine, so you are in doubt about this. So if you go back there and read again what I explained, you will know why taking * gives error. If it were only this doubt, this would be a duplicate of 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, so the size of each element of this array is 4 or 8 bytes depending on whether your platform is 32 or 64 bits. The text is not there, it is in static area.

When you give printf() you are asking for it to print text that is composed of a reserved slot , hence a type char * , that is defined by %s , is a documentation convention of the printf() function, and then comes a \n which is a line break. As printf() does this is a detail of its implementation, but if we simplify and simplify it simply to understand it easier, what it does there is more or less this:

while (*string != '
    |    0     |    1
--------------------------
 0  | (char *) | (char *)
--------------------------
 1  | (char *) | (char *)
') putchar(*string++);

It is taking the variable it receives and sweeps it by character and by printing each one individually. The output condition is when it encounters the null character ( char ) which is the string terminator, as explained in the above link , at this point and ends the loop of repetition. In each print it increments the memory address to the next character and goes one more step in the loop. This is the basic way to go through arrays in memory, where string is an array of [256] .

By taking it it gets the address where the string is, in this case a static area of memory, and will scan that area of memory pointed to by the element of its two-dimensional array until you find the terminator.

The second example is that it is reserved in stack (also called automatic memory and not in the static area of memory, through %code% , because even when changing the value of the area at runtime it can not be in the static area. Then the semantic difference of the first and second code. Understand the use of static memory, and heap >.

this might help: What's the difference in assigning an already started array to an uninitiated array (if not for duplicate).

If it is hard to understand why you skipped steps and you need to get back to basics, understand these things I am talking about first, do not build a house starting with the roof, you need to build the foundation. The links that I passed in the previous answer have all this, if not directly at least within them. have to read, all your doubts have already been answered before here in SOpt. Links on the internet were created to be followed and give more details on the subject.

    
11.08.2018 / 20:40