Doubt about C pointers

Asked

Viewed 399 times

1

void imprime (char *v, int n) {
   char *c;
   for (c = v; c < v + n; v++)
      printf ("%c", *c);
}

I have this function, but I don’t understand what exactly she’s doing and how the pointers behave in this case.

3 answers

2


Is getting a string (char * is a pointer to characters, in C is how texts are usually represented), and an integer.

Declares another string c without any content.

Start a loop by placing the parameter pointer v in c, then both point to the same object.

The bond will end when c is equal to the address of v plus n bytes/characters.

There is an increment in v in every loop stride. In my opinion there is an error there. I think the increment should be in c.

The character indicated by c. The * is used because you want the content contained in the address c and not its value which is a memory address. Pointers always work like this.

In short, if the algorithm was correct it would print character by character as many as indicated in n. If n is larger than the size of the string will pick up junk from memory. Ie, another problem in the algorithm.

I understand the exercise, but this is unnecessary since the printf() is able to print a set amount of characters in the formatting.

I am answering what the question asks. I know that some concepts presented may not be clear to those who are beginning. You need to research them or ask new questions.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you want to know more have some questions on the subject:

1

This function receives from parameter a pointer to character and a quantity. It is assumed that this pointer points to an array of characters, at least equal in size to the reported amount.

At the initialization of for, is done with the pointer variable c point to the array. In memory, we would have something like:

|A|l|ô| |M|u|n|d|o|
 ^
 c

c is pointing to the first character of the array. Doing *c it is possible to access this character.

With each iteration of for, c is incremented. What happens is that it switches to pointing to the next element of the array:

|A|l|ô| |M|u|n|d|o|
   ^                 segunda iteração
   c

|A|l|ô| |M|u|n|d|o|
     ^               terceira iteração
     c

...

|A|l|ô| |M|u|n|d|o|
                 ^   última iteração
                 c

In your example, each iteration a character is read and printed in the console using the function printf.

0

This function prints n characters from the address pointed by the pointer "v", but in your question the code is wrong, where is "v++" must be "c++", as below.

Initially "c" has the same value as "v". At each iteration of "for" the "c" pointer advances a position, and prints the next character. The loop ends when "c" becomes "v + n", having printed n characters.

void imprime (char *v, int n)
{
   char *c;
   for (c = v; c < v + n; c++)
       printf ("%c", *c);
   }

Browser other questions tagged

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