Arrays not started in C

Asked

Viewed 252 times

1

How do I know how much size I have to put to scroll through an uninitiated array []? For example this one 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 having to count the elements of an array, vector, how to know? in an array(empty) And also what the character " 0" means, because in the book I’m reading it says that the string text will be 13 in size, but "Language C" is only 12.

3 answers

4

In C, all the strings have a null terminator, which is the character '\0' at the end of string. This character also occupies memory, and because of this, it is part of the string and there should be room reserved for him in array character.

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

Thus the string "Linguagem C." have 12 characters, but counting with the terminator '\0' there will be 13, so a array 13 positions to store this string.

In C, there is the function strlen which gives the length of a string not counting the null terminator. But note that this function computes the size of the string by scrolling through all your characters one by one looking for the null terminator, then if your string is very large (a whole book, for example), it will be slow, and so it is better to avoid using it if you can get the size of the string in some other more efficient way.

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

  • Also keep in mind that strlen computes the size of your valid string. If you have a 12-character string within a 20-position char array yet strlen returns 12.

  • @Jonnypiazzi Thank you, I complemented my text with your comment. :)

  • I’ll see this strlen when studying strings.

3


No need to count the characters! Just know that the string ends in ' 0' and use such a 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] != '\0'){

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

   return 0;
}

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

Don’t stand around counting characters on the screen:

#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] != '\0'){

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

    return 0;
}

Have you ever thought ?

  • Now I get it..

1

Okay you asked several questions.

How to know how much size I should put to scroll through an uninitiated array []?

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

Without having to count the elements of an array, vector, how to know?

There are other languages, more commonly in managed languages (ex java, c#) ways to find the size of an array, but in C this is complicated and depending on the scenario may be impossible.

And also what the character " 0 means"?

The character ' 0' is the "close" character of a string. This is how you mark where to stop reading the string.

You asked a lot of questions in a single one, so my answer was very vague. I suggest you scrap your question into single questions and post each one individually so we can answer each one with more details.

  • Okay, but creating multiple questions would be bad, no??

  • 2

    Not if the question is consistent. Keep in mind that your question is not only for you, it is published for other people who will one day go through the same problem as you. But it will be much harder for these people to find what they’re looking for if they have multiple questions in one, it may be that her problem is just with the character \0 for example, but she doesn’t want to know about string length.

  • I hadn’t thought of it.

Browser other questions tagged

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