简体   繁体   English

如何通过引用传递创建到结构的指针数组?

[英]How do I pass by reference an array of pointers created into a struct?

I need to create a program that can sum N vectors and print the final array, for example, if I put N = 2, first array (a,b) and second array(c,d) the sum should be (a+c, d+b). 我需要创建一个可以对N个向量求和并打印最终数组的程序,例如,如果我将N = 2,则第一个数组(a,b)和第二个数组(c,d)的总和应为(a + c ,d + b)。 But I cannot compile this program, it gives an error in the 但我无法编译该程序,它在

line 57 saying that it cannot convert 'float*' to 'float' into the subroutine (forca_res) arguments 第57行表示无法将'float *'转换为'float'到子例程(forca_res)参数中

PS: The quotes are into Portuguese, I'm sorry for that, but it is my native language. PS:引号是葡萄牙语,对此感到抱歉,但这是我的母语。

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

//ESTRUTUTRA
struct vetores
{
    int dim=2;
    float *vet;
}vetor_forca,vetor_res; //Lista de Objetos


//SUBROTINA
void forca_res(float vetor_forca.vet[2], float vetor_res.vet[2])
{   
    int i;
    //Calculo do Vetor Resultante
    for(i=0;i<2;i++)
    {
        vetor_res.vet[i] = vetor_res.vet[i] + vetor_forca.vet[i];
    }
}


//PROGRAMA PRINCIPAL
int main(void)
{
    //Declaracao de variaveis
    int num, i, cont=0;
    vetores vetor_forca, vetor_res;
    vetor_forca.vet= (float*) calloc( vetor_forca.dim, sizeof(int) );
    vetor_res.vet= (float*) calloc( vetor_res.dim, sizeof(int) );

    //Leitura de dados
    printf("Digite a quantidade 'N' de vetores de forca: ");
    scanf("%d", &num);

    //Logica
    while (cont != num)
    {
        printf("\nDigite os dois elementos do vetor de forca:\n");
        for(i=0;i<2;i++)
        {
            scanf("%f", &vetor_forca.vet[i]);
        }
        //Chamando a Subrotina
        forca_res(vetor_forca.vet, vetor_res.vet);
        vetor_forca.vet= (float*) calloc( vetor_forca.dim, sizeof(int) );
        cont++;
    }
    free(vetor_forca.vet);

    //Imprimindo o Resultado
    printf("\n\nVETOR RESULTANTE:\n");
    for(i=0;i<2;i++)
    {
        printf("%f ", vetor_res.vet[i]);
    }

    //Finalizando o Programa
    printf("\n\nFim do Programa!\n");
    getch();
    return 0;
}

Mmm, It's been a while since I wrote C code but your syntax isn't even correct (though you didn't give the error) Your subroutine should take a 嗯,自从我编写C代码以来已经有一段时间了,但是您的语法甚至不正确(尽管您没有给出错误),您的子例程应该使用

void subroutine(float* stuff, int size);

Then you treat the pointer just like an array 然后将指针像数组一样对待

for I < size 因为我<大小

You have other problems as well, but it looks like this might be an improved version of your function: 您也有其他问题,但看起来这可能是该功能的改进版本:

void forca_res(struct vetora *forca, struct vetora *res)
{
    // Set dim to be the lesser of forca->dim and res->dim
    int dim = ((forca->dim <= res->dim) ? forca->dim : res->dim);
    int i;
    //Calculo do Vetor Resultante
    for(i=0;i<dim;i++)
    {
        res->vet[i] += forca->vet[i];
    }
}

In main() , you would call it like this: main() ,您可以这样称呼它:

forca_res(&vetor_forca, &vetor_res);

Among other things, note that this version of the function relies on the struct vetora objects to provide the dimension of their array, instead of assuming dimension 2. Note, too, that in truth, you might want to error out if vectors of different lengths are provided as arguments, if indeed that's a case that should not arise. 除其他事项外,请注意,此版本的函数依赖于struct vetora对象提供其数组的维数,而不是假设维数为2。也请注意,实际上,如果长度不同的向量可能会出错如果确实存在这种情况,则应将其作为参数提供。

The cause of the error message you asked about is that the specification of 您询问的错误消息的原因是:

void forca_res(float vetor_forca.vet[2], float vetor_res.vet[2])
{
    /* whatever */

}

is invalid. 是无效的。 It is not possible to have names of function arguments that involve struct member access. 不能包含涉及结构成员访问的函数参数的名称。 Hence the compiler complaints. 因此,编译器抱怨。

The short fix is to remove the . 简短的解决方法是删除. characters from the argument names (both in the argument list, and within the function). 参数名称中的字符(在参数列表中和在函数中)。 In other words, use valid variable names. 换句话说,使用有效的变量名。

There are other problems as well. 还有其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM