Display imaginary vector values with pointer arithmetic

Asked

Viewed 312 times

-2

Good afternoon ! I’m doing the following exercise !

-Create a program that contains an integer array containing 5 elements. Using pointer arithmetic only, read this keyboard array and print double each read value.

Resolute

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

int main(){
    int x[5];
    int *v=NULL;
    v=&x;


     for (int i;i<5;i++){
        int y;
        scanf("%i",&y);
        *v=y;
        v++;

    }

    printf("\n");
    printf("Valor %i - Posicao %i\n",*v,v);
    printf("Valor %i - Posicao %i\n",*v+1,v+1);
    printf("Valor %i - Posicao %i\n",*v+2,v+2);
    printf("Valor %i - Posicao %i\n",*v+3,v+3);
    printf("Valor %i - Posicao %i\n",*v+4,v+4);
}

My doubt is as follows: When I create a "FOR" or other whole variable to create another loop the variables and memory placement get all weird. I believe that this happens because when I create another variable I say what I want to save and not where I want to save in memory, as I indicate the position of memory that I want to save?

Another question is why when I create a forced vector without error []? I’m messing with memory positioning, so shouldn’t I accept when I place the referenced value position of type int + 1? If it became hard to understand and only delete the brackets of variable x at the beginning and run, there goes the error the second time.

In short How do I put these printf at the end inside a loop!

  • Here: printf("Valor %i - Posicao %i\n",*v+1,v+1); shouldn’t be: printf("Valor %i - Posicao %i\n",*(v+1), v+1);? Idem too many positions.

1 answer

0

My doubt and the following: When I create a "FOR" or other whole variable to create another loop the variables and memory placement get all weird.

What is happening is that you are reading the memory beyond the 5 elements of the array.

You can fix this by pointing the pointer again to the beginning of the array.

You should not use the operator & to get the address of an array, because the name of an array is already an address.

#include <stdio.h>

int main() {
  // aloca a memoria para os 5 elementos.
  int valores[5];

  // aponta para o inicio do array.
  int *ptr_valores = valores;

  for(int i = 0; i < 5; ++i) {
    printf("Insira um número: ");
    scanf("%d", ptr_valores);

    // incrementa o ponteiro para o proximo elemento.
    ++ptr_valores;
  }

  // aponta para o inicio do array novamente.
  ptr_valores = valores;

  for(int i = 0; i < 5; ++i) {
    // imprime o dobro de cada elemento.
    printf("%d\n", (*ptr_valores) * 2);

    // incrementa o ponteiro para o próximo elemento.
    ++ptr_valores;
  }

  return 0;
}

Another question is why when I create a forced vector without error []?

I don’t know what you mean by forced vector, but if you remove [] from a variable, you are creating a normal variable and not an array.

I’m messing with memory positioning, so I shouldn’t accept when I place the referenced value position of type int + 1?

In pointer arithmetic when a pointer has its value added or subtracted by another value the following calculation:

address = start address + (pointer type size in bytes * n).

Therefore, if an int type ptr pointer points to 0x0, when doing ptr + 1, the following calculation will be made:

ptr = ptr + (4 * 1)

Now ptr points to the 0x4 address, assuming int has 4 bytes.

Browser other questions tagged

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