在C中将结构作为不兼容的指针类型 [英] Struct as incompatible pointer type in C
问题描述
我创建了一个只有1个成员的结构,我唯一想做的就是:
I have created a struct with only 1 member and the only thing I want to is:
- 将结构传递给函数updateDispCase
- 在范围内和范围外更新成员StartTime.
我面临的问题是我收到一个错误从不兼容的指针类型传递updateDispCase的参数".如果我尝试取消注释"//dispCaseAr [iNumber]-> StartTime = 10;"程序崩溃.
The problem I face is that I get a error "Passing argument of updateDispCase from incompatible pointer type". If I try to uncomment "//dispCaseAr[iNumber]->StartTime = 10;" the program crashes.
如何将结构传递给函数以及如何对其进行更新?
How do I pass my struct to the function and how to I update it?
我的代码如下所示:
#include <stdio.h>
#define NROFDISPCASE 4
typedef struct {
int nr;
double StartTime;
} DispCase;
DispCase dispCaseArray[NROFDISPCASE];
//Preprocessor
void updateDispCase(DispCase *dispCaseAr[],int);
//
int main(){
int i;
int value1[NROFDISPCASE]={1,2,3,4}; // Display nr.
//Initialize DisplayCase
for(i = 0; i<NROFDISPCASE; i++){
dispCaseArray[i].StartTime = value1[i];
}
updateDispCase(&dispCaseArray,0);
printf("%f\n",dispCaseArray[0].StartTime);
return 0;
}
void updateDispCase(DispCase *dispCaseAr[],int iNumber){
//dispCaseAr[iNumber]->StartTime = 10;
}
推荐答案
我自己解决了这个问题,我认为写出我的问题的答案是一个主意. 我删除了&从函数调用中删除,并从函数中删除了括号[].新代码如下: 我忘了数组永远是指针.
I solved the problem myself and I thought it would be an idea to write the answer to my problem. I removed the & from the function call and removed the brackets [] from the function. The new code is as follows: I forgot an array is always a pointer.
#include <stdio.h>
#define NROFDISPCASE 4
typedef struct {
int nr;
double StartTime;
} DispCase;
//Preprocessor
void updateDispCase(DispCase *dispCaseAr[],int);
//
int main(){
DispCase dispCaseArray[NROFDISPCASE];
int i;
int value1[NROFDISPCASE]={1,2,3,4}; // Display nr.
//Initialize DisplayCase
for(i = 0; i<NROFDISPCASE; i++){
dispCaseArray[i].StartTime = value1[i];
}
updateDispCase(dispCaseArray,0);
printf("%f\n",dispCaseArray[0].StartTime);
return 0;
}
void updateDispCase(DispCase *dispCaseAr,int iNumber){
dispCaseAr[iNumber]->StartTime = 10;
}
这篇关于在C中将结构作为不兼容的指针类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!