用C语言制作一个标量乘法函数

用C语言制作一个标量乘法函数,c,vector,scalar,C,Vector,Scalar,我已经编写了一个函数定义来获得向量的标量乘法。我得到了一个原型来使用,但我对理解这个原型中的指针感到困惑。这是原型 void scalar_mult(double k, const threeVec_t *src, threeVec_t *dest); // REQUIRES: src and dest point to threeVec_t objects. // PROMISES: *dest contains the result of scalar multiplication //

我已经编写了一个函数定义来获得向量的标量乘法。我得到了一个原型来使用,但我对理解这个原型中的指针感到困惑。这是原型

void scalar_mult(double k, const threeVec_t *src, threeVec_t *dest);
// REQUIRES: src and dest point to threeVec_t objects.
// PROMISES: *dest contains the result of scalar multiplication
//   of k and *src.

struct threeVec {
  double x;
  double y;
  double z;
};

typedef struct threeVec threeVec_t;
这是我的代码,我做错了什么

void scalar_mult(double k, const threeVec_t *src, threeVec_t *dest)
{
    int i, j, result;
    for (i = 0; i < src; i++) {
                for (j = 0; j < dest; j++) {
                        result[i][j] = k * result[i][j];
}
void scalar\u mult(双k,常数threeVec\u t*src,threeVec\u t*dest)
{
int i,j,结果;
对于(i=0;i
您将
结果声明为

int i, j, result;
但您将
结果
视为2D数组,这是错误的

你在找这样的东西吗

// it is the callers duty whether the src is null or not
void scalar_mult(double k, const threeVec_t *src, threeVec_t *dest)
{
    dest->x = src->x * k;// arrow operator as member accessed by pointer 
    dest->y = src->y * k;
    dest->z = src->z * k;
}
您可以从
main
调用
scalar\u mult
,如下所示:

int main() {
    threeVec_t src = {2.0, 3.0, -1.0};
    threeVec_t dest;
    double k = 3.0;

    scalar_mult(k, &src, &dest);

    printf("x = %lf, y = %lf, z = %lf", dest.x, dest.y, dest.z); // Dot operator as member accessed by object 
    return 0;
}

.

dest和src指向x的确切作用是什么?所以在这种情况下你不需要for循环?@johnjohn,我想你需要学习一下
struct
和pointer.yessir,但是你为什么要去掉for循环呢?是的,你实际上不需要任何循环,因为你知道向量的维数(图3供您使用).你知道什么是标量乘法吗?你能用铅笔和纸计算两个3向量的标量积吗?你能在不涉及指针的情况下用C计算吗?顺便说一句,发布的原型是错误的,请仔细检查你复制的是否正确。
i
应该做什么?为什么你要将整数索引与arbi进行比较trary pointer?那根本没有意义。我投票将这个问题作为离题来结束,因为你应该先学习语言的基本知识,然后再将毫无意义的东西输入源文件,并期望我们教你C。