我使用一个函数来显示指针指向的内存内容块。 但没有得到所需的输出,我是新来的,请纠正我,如果我错了。 当我输入大小= 3,元素= 1,2,3,我得到输出= 1只。在函数中使用指针指针的正确方法是什么?
下面的代码:
#include <stdio.h>
#include <stdlib.h>
void merge(int **arr1);
int main(void) {
int size1;
printf("Give me the size of first array\n");
scanf("%d", &size1);
int *arr1 = malloc(size1*sizeof(int));
int *p1=arr1;
printf("Give me the elements of first array\n");
int index1;
for(index1 = 0 ; index1<size1; index1++)
scanf("%d", p1++);
merge(&arr1);
return;
}
void merge(int **arr1) {
while(**arr1) //**arr1 is the content of the passed array, if there
// is an int in it, print that out and increment to next one
{
printf("%d", **arr1); // ** is the content and * is the address i think, right?
*arr1++;
}
}
你得到了什么输出?你期望什么?你到目前为止做了哪些调试? – 2012-04-07 20:54:16
请格式化您的代码。 – 2012-04-07 20:54:58
对不起,我编辑它 – 2012-04-07 20:57:24