Access vector value by index

Asked

Viewed 78 times

1

I’m practicing a little vector, when I come across this code, of an exercise:

#include <stdio.h>
main ()
{
  char ex3[5] = { 'z', 'k', 'w', 'x', 'v' };
  int cont1;
  cont1 = 2;
  printf ("%c %c",ex3[cont1+1], ex3[4]+cont1);
}

What I understood:

In the stretch, ex3[cont1 + 1] understood the following: sum the value of the variable cont1 at value 1, what will be ex3[3] and return the x

In the meantime ex3[4] + cont1 , I couldn’t understand it, the index 4 vector ex3 will be v, but I didn’t understand what the variable cont1 is doing there.

Why ex3[4] + cont1 will result in x?

2 answers

1


What is doing there only those who wrote the code can say. It may have been unintentionally. If it was by wanting the meaning it would be that she wants the next letter of the alphabet, skipping one, (technically the next character of the ASCII table extended) to what was picked up in that index. So as 4 is the index that has a v the next character, skipping one, is the x (I’m skipping one because the cont1 worth 2, if it was worth 1 would not need to skip one. I’m not saying that it will take the next element the array, This is done just on ex3[cont1 + 1].

You’re adding 2 to the number you find there.

I always say that, a lot of people don’t understand about graphic representation of a number.

The guy char is a number like any other in which it is guaranteed to have 1 byte, so you can represent 256 different numbers. You can use this as the same number, it’s quiet, try printing with %d instead of %c.

When you take this data and have it printed with the formatter %c instead of putting a text with the number worth there (actually a text of 1 to 3 characters only with numeric digits), it will print a single character based on an extended ASCII table, ie for each number has a corresponding graphic character.

Normal ASCII table (without the extended part which are characters ranging from 128 to 255):

tabela ASCII

Some characters are not exactly printable, or the print has a special way of handling, and has the number 0 which is the text terminator and not a valid character.

So when you get the v that you see in your code, for the computer actually is only the number 118. When sum 2 it takes the 120 and in the table it is equivalent to the x.

Do an experiment: change the letters inside the array by 5 numbers. Take the numbers by looking at the letters according to the table above. It is not so readable, but to that end it explains better what is happening.

You are only having the number 120 printed as a character of the ASCII table. If impri9mir only the number would print 120 (three characters composed of numerical digits). If you had them printed as a hexadecimal number then you would print 78 (again two characters) but this all does not change the number that for us who are accustomed to decimal is the 120. There in the table it still shows the octal shape or how is the entity of HTML, and could have other ways to represent a number.

It has many ways of representing numbers. I’m representing number 3, everyone knows it’s a 3 looking at it:

3 patos de borracha

  • Thank you for the reply!

1

In the ASCII table the char characters have a Decimal, Hexadecimal, Octal and char. Let’s just focus on the decimal representations and char.

inserir a descrição da imagem aqui

According to the table, v is equivalent to 118 in decimal. When the sum is made ex3[4] + cont1, (in which ex3[4] = 'v' and cont1 = 2, being the type int) the result is equivalent to 120 decimally.

How the print output is in char %c, this result is converted to the respective character, which, according to the table, is 'x'.

If you do:

printf("%d", 'a');

will get 97 as a result, which is the decimal representation of 'a'. From this idea you can test several examples by adding char with int, making the conversion and checking the result in the ASCII table.

  • Thank you very much for your reply!

Browser other questions tagged

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