Arrays not started in C

1

How do I know how much size I should put to go through an uninitialized array []? For example in that with char [].

#include <stdio.h>
#include <stdlib.h>

int main()
{

   char texto[] = "Linguagem C.";
   /* for(int i=0; i<13; i++) {
    printf("%c", texto[i]); 
   } */

}

Without needing to count the elements of an array, vector, how to know? in an array (empty) And also what does the "\ 0" character mean, because in the book I'm reading it says the string text will be 13 in size, but "Language C" is only 12.

    
asked by anonymous 17.05.2015 / 04:09

3 answers

3

You do not need to count the characters! Just know that the string ends in '\ 0' and use that condition in some loop. Maybe this is one of the reasons that the string in C ends in '\ 0'.

#include <stdio.h>

int main()
{
   char texto[] = "Linguagem C.";
   int i = 0;  

   while(texto[i] != '
#include <stdio.h>

int main()
{
   char texto[] = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting";
    int i = 0;  

    while(texto[i] != '
#include <stdio.h>

int main()
{
   char texto[] = "Linguagem C.";
   int i = 0;  

   while(texto[i] != '
#include <stdio.h>

int main()
{
   char texto[] = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting";
    int i = 0;  

    while(texto[i] != '%pre%'){

       printf("%c", texto[i]); 
       i++;
    }

    return 0;
}
'){ printf("%c", texto[i]); i++; } return 0; }
'){ printf("%c", texto[i]); i++; } return 0; }
'){ printf("%c", texto[i]); i++; } return 0; }

Every string in C must end with '\ 0'. When you type char texto[] = "Linguagem C."; the compiler automatically puts a '\ 0' at the end of your string.

Do not bother counting characters on the screen:

%pre%

Ever wondered?

    
17.05.2015 / 04:51
3

In C, all strings have a null terminator, which is the caratecere ' Linguagem C. ' at the end of the string . This character also takes up memory, and because of this, it is part of the string and should have space reserved for it in the array

The character ' strlen ' is nothing more than the zero value represented as a character (see the ASCII table ). In C, strings use it to represent where the string ends.

In this way the string " strlen " has 12 characters, but with the terminator ' %code% ' will be 13, so you need an array with 13 positions to store this string .

In C, there is the %code% function that gives the length of a string without counting the null terminator. But note that this function computes the size of string by traversing all its characters one by one looking for the null terminator, so if your string is too large , for example), it will be slow, so it's best to avoid using it if you can get the string size in some other more efficient way.

And as pointed out by Jonny Piazzi in a comment, since %code% only looks for the null terminator, the size of the array itself does not matter. So if you use a array of 20 characters to store a string with only 12, what will import will be the position where the null terminator will be found.     

17.05.2015 / 04:44
1

Okay you asked several questions.

  

How do I know how much size I should put to go through an uninitialized array []?

Response, whenever you are not working with a dynamically allocated array.

  

No need to count the elements of an array, vector, how to know?

In other languages, more commonly in managed languages (eg java, c #) ways to find out the size of an array, but in C this is tricky and depending on the scenario it may be impossible.

  

And also what does the "\ 0" character mean?

The '\ 0' character is the "date" character of a string. It's how you point out where to stop the string reading.

You asked lots of questions in a single one, so my answer was very vague. I suggest you debunk your question into unique questions and post each one individually so we can answer each one in more detail.

    
17.05.2015 / 04:44