-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.– anonimo