The original question is the problem of my confusion, sorry.
#include <stdio.h>
#include <stdlib.h>
void func(int **p_in1, int **p_in2, int **p_in3){
int *p_temp1 = (int *)malloc(5*sizeof(int));
int *p_temp2 = (int *)malloc(5*sizeof(int));
int *p_temp3 = (int *)malloc(5*sizeof(int));
int i;
for(i = 0; i < 5; i++){
p_temp1[i] = i+1;
p_temp2[i] = i+2;
p_temp3[i] = i+3;
}
*p_in1 = p_temp1;
*p_in2 = p_temp2;
*p_in3 = p_temp3;
}
int main(){
int *p_out1 = NULL, *p_out2 = NULL, *p_out3 =NULL;
int i;
func(&p_out1, &p_out2, &p_out3);
for(i = 0; i < 5; i++){
printf("%d", p_out1[i]);
printf(" ");
printf("%d", p_out2[i]);
printf(" ");
printf("%d\n", p_out3[i]);
}
free(p_out1);
free(p_out2);
free(p_out3);
p_out1 = NULL;
p_out2 = NULL;
p_out3 = NULL;
return 0;
}
I want to make the “func” much shorter by just use one para. How can I put the “p_in1, p_in2, p_in3” together in continuous memory space?
Now that you’ve edited in the output that you wanted, the question can be answered.
Change your print loop to this:
This will output:
In your original code, you were interleaving the 3 arrays. What you wanted was to print each one separately.
EDIT: As for your second question, you can combine
p_out1,p_out2, andp_out1into an array.Here’s your code with this done: