0
I managed to do this exercise inside main
, however the same concept has not worked when using functions.
The aim of the program is simple, read six values and display them.
#include <stdio.h>
#include <stdlib.h>
int main(){
int *i;
int *valor[6];
int temp = 0;
i = (int *)malloc(sizeof(int));
valor[6] = (int *)malloc(sizeof(int));
for (*i=0; *i<6; *i = *i + 1){
scanf("%d", (valor+*i));
}
for (*i=0; *i<6; *i = *i + 1){
printf("%d\n", *(valor+*i));
}
}
How is the code that tries to fulfill the same goal using functions:
#include <stdio.h>
#include <stdlib.h>
void write(int *i, int *valor){
for (*i=0; *i<6; *i = *i+1){
scanf("%d", &valor[*i]);
}
}
void read(int *i, int *valor){
for (*i=0; *i<6; *i = *i+1){
printf("%d\n", valor[*i]);
}
}
int main(){
int *i = 0;
int *valor;
i = (int *)malloc(sizeof(int));
valor = (int *)malloc(sizeof(int)*6);
write(&i, &valor);
read(&i, &valor);
}