C语言问题
#include<stdio.h>void mul_array(int*p1, int*p2, int x)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < x; j++)
{
p2 = (p1)*2;
}
}
}
int main()
{
int arr1 = { {1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15} };
int arr2 = { 0 };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
printf("%d ", arr1);
}
}
printf("\n");
mul_array(arr1, arr2, 5);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
printf("%d ", arr2);
}
}
return 0;
}
XDM题目是将数组arr1每个元素乘2 放到arr2中去 我这样写有什么问题吗 ? 我感觉传参数的时候有问题 但是具体不清楚
#include<stdio.h>
void mul_array(int (*p1), int (*p2), int x)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < x; j++)
{
*(*(p2+i)+j) = *(*(p1+i)+j)*2;
}
}
}
int main()
{
int arr1 = { {1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15} };
int arr2 = { 0 };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
printf("%d ", arr1);
}
}
printf("\n");
mul_array(&arr1, &arr2, 5);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
printf("%d ", arr2);
}
}
return 0;
} #include <stdio.h>
#define R 3
#define C 5
void multiply(int(*A), int(*B)) // <----- 注意这里,指针传入方式
{
for(int r = 0; r < R; r++)
for (int c = 0; c < C; c++)
B = A * 2;
}
int main(void) {
int A = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
int B = { 0 };
multiply(&(*A), &(*B)); // <----- 注意这里,指针传入方式
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++)
printf("%d ", B);
printf("\n");
}
return 0;
}
页:
[1]