Doubt regarding pointers in C

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 do not understand what it is doing exactly and how the pointers behave in this case.

    
asked by anonymous 28.09.2016 / 21:46

3 answers

1

You are getting a string ( char * is a pointer to characters, in C this is how you usually represent texts), and an integer.

Declare another string c with no content whatsoever.

Initiates a loop by setting the pointer of the parameter v to c , then both point to the same object.

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

There is an increment in v for each loop loop. In my opinion there is a mistake there. I think the increment should be in c .

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

In summary, if the algorithm was correct it would print character by character as many as indicated in% with_%. If n is greater than the size of string will get garbage from memory. That is, another problem in the algorithm.

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

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

See running on CodingGround and ideone .

If you want to know more you have some questions about it:

28.09.2016 / 21:55
1

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

When initializing for , the pointer variable c is pointed 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. By doing *c you can access this character.

At each iteration of for , c is incremented. What happens is that it points to the next element in 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, at each iteration a character is read and printed on the console using the printf function.

    
28.09.2016 / 22:06
0

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

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

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