|
下のプログラムで関数 vectorSum() の第2,3引数にはconst修飾子が付いているけど、
第4引数には付いていない。この理由ってなんですか??
教えてください
#include <stdio.h>
#include <stdlib.h>
int raedNumberofElements(int , char *[]);
void vectorSum(int, const double *, const double *, double *);
int main(int argc, char *argv[])
{
double *vectorA, *vectorB, *vectorC, *p_element;
int n, i;
n = raedNumberofElements(argc, argv);
if (n <= 0) {
return -1;
}
vectorA=(double *)malloc(n*sizeof(double));
vectorB=(double *)malloc(n*sizeof(double));
vectorC=(double *)malloc(n*sizeof(double));
p_element = vectorA;
printf("ベクトルaの各要素を入力して下さい\n");
for (i = 0; i < n; i++){
printf("ベクトルaの%d番目の要素: ", i + 1);
scanf("%lf", p_element);
p_element++;
}
printf("\n");
p_element = vectorB;
printf("ベクトルbの各要素を入力して下さい\n");
for (i = 0; i < n; i++){
printf("ベクトルbの%d番目の要素: ", i + 1);
scanf("%lf", p_element);
p_element++;
}
printf("\n");
vectorSum(n,vectorA,vectorB,vectorC);
p_element = vectorC;
printf("ベクトルの和を出力します\n");
for (i = 0; i < n; i++){
printf("和の%d番目の要素: ", i + 1);
printf("%10f\n", *p_element);
p_element++;
}
free(vectorA); vectorA = NULL;
free(vectorB); vectorB = NULL;
free(vectorC); vectorC = NULL;
return 0;
}
int raedNumberofElements(int argc, char *argv[])
{
int n;
if (argc <= 1) {
printf("引数がありません\n");
return -1;
} else if (argc >= 3) {
printf("引数が多すぎます。\n");
return -1;
} else {
n=atoi(argv[1]);
if (n <= 0) {
printf("1以上の整数を指定して下さい\n");
return -1;
}
return n;
}
}
void vectorSum(int n, const double *vectorA, const double *vectorB, double *vectorC)
{
int a;
for(a=0;a<n;a++)
{
*vectorC=*vectorA+*vectorB;
vectorA++;
vectorB++;
vectorC++;
}
return;
}
|