一维数组作为不确定参数传参怎么写?

下面是我的程序,运行结果有问题,麻烦懂得大佬支支招。#include "stdio.h"#include<stdlib.h> #include "stdarg.h"int yy[100][3] = {0};void Fun(int *color,...){ int i = 0,j; int *s; va_list ap; va_start(ap, color); for(i = 0; i < 3; i ++) { s = va_arg(ap, int*); for(j = 0; j < 3; j ++) { printf("%d ", s[j]); } yy[i][0] = s[0]; yy[i][1] = s[1]; yy[i][2] = s[2]; printf("i = %d ", i); printf("\n"); } printf("\n"); va_end(ap);}int main( ){ int i, j; int a[3] = {1,2,3}, b[3] = {4,5,6}, c[3] = {7,8,9}; Fun(a, b, c); for(i = 0; i < 3; i ++) { for(j = 0; j < 3; j ++) { printf("%d ", yy[i][j]); } printf("\n"); } return 0;}

//下面做了注释,有问题追问,满意采纳3Q
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

int yy[100][3] = {0};
void Fun(int *color,...)
{
int i = 0,j;
int *s;

//color是第一个参数对应a[3]
for(j = 0; j < 3; j ++)
{
printf("%d ", color[j]); 
}
yy[i][0] = color[0];
yy[i][1] = color[1];
yy[i][2] = color[2];
printf("i = %d ", i);
printf("\n");

//余下的才是b[3]和c[3]
va_list ap;
va_start(ap, color);
for(i = 1; i < 3; i ++)//i从1开始
{
s = va_arg(ap, int*);
if(!s) break;//这里加个判断,没有可变参数跳出循环。如果参数是2个的话不至于出错
for(j = 0; j < 3; j ++)
{
printf("%d ", s[j]); 
}
yy[i][0] = s[0];
yy[i][1] = s[1];
yy[i][2] = s[2];
printf("i = %d ", i);
printf("\n");
}
printf("\n");
va_end(ap);
}

int main()
{
int i, j;
int a[3] = {1,2,3}, b[3] = {4,5,6}, c[3] = {7,8,9};
Fun(a, b, c);
for(i = 0; i < 3; i ++)
{
for(j = 0; j < 3; j ++)
{
printf("%d ", yy[i][j]);
}
printf("\n");
}
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-03-30
1、数组做参数,完全无法按值传递。这是由C/C++函数的实现机制决定的。
2、传数组给一个函数,数组类型自动转换为指针类型,因而传的实际是地址。
下面三种函数声明完全等同:
void func(int array[10])
void func(int array[])
void func(int *array)
相似回答