I'm doing an exercise in which I pass by parameter two arrays of integers already defined by the user. I need now, in a third array, to store the sum of the two received arrays, in the same positions, using pointer arithmetic.
Ex:
vet 1 [1,2,3]
vet 2 [1,2,3]
Here in the third vector I have to get the sum using pointer arithmetic.
vet 3 [2,4,6]
My attempt:
#include <stdio.h>
#define MAX 100
int soma_ponteiro(int *vet1,int *vet2,int num);
int main(){
int vet1[MAX],vet2[MAX];
int num,i;
printf("Digite qtd de numeros:\n");
scanf("%d",&num);
for(i=0;i<num;i++){
printf("\nDigite numero %d vetor 1\n",i+1);
scanf("%d",&vet1[i]);
printf("\nDigite numero %d vetor 2\n",i+1);
scanf("%d",&vet2[i]);
}
soma_ponteiro(vet1,vet2,num);
printf("\n%d",soma_ponteiro(vet1,vet2,num));
return 0;
}
int soma_ponteiro(int *vet1,int *vet2,int num){
int vet3[MAX];
int *last_pos=vet1+num;
int *ptr = vet1;
int *ptr2= vet2;
int *ptr3= vet3;
while(ptr<last_pos){
*ptr3 = *vet1+*vet2;
ptr++;
}
return ptr3;
}