[英]can't seem to locate the struct
I am trying to fetch values from a struct once its been updated however the problem am facing is an undeclared error as it cannot seem to see it. 我试图在结构更新后从结构中获取值,但是面临的问题是一个未声明的错误,因为它似乎看不到它。
sonicNav.h file sonicNav.h文件
#include<stdio.h>
#include<stdlib.h>
#include"sonicThread.h"
extern void calcSonicS();
sonicThread.c file. sonicThread.c文件。
int funcLock = 0;
void calcSonicS() {
struct results *rData = results;
rData = malloc(sizeof(struct results));
int newVal1 = rData->sens1;
int newVal2 = rData->sens2;
int newVal3 = rData->sens3;
int newVal4 = rData->sens4;
if(funcLock == 0){
funcLock = threadFunc();//returns INT value of 1.
}
printf("value 1: %d value 2: %d value 3: %d value 4 %d\n", newVal1, newVal2, newVal3, newVal4);
}
sonicThread.h file sonicThread.h文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>
#include<errno.h>
#include<pthread.h>
#include<sys/time.h>
#include<wiringPi.h>
//GPIO PINS stored within structs, for each sonic range finder.
typedef struct sonicPins{
//pins and id.
int trig;
int echo;
int id;
}args;
typedef struct results{
//all pins
int sens1;
int sens2;
int sens3;
int sens4;
}rData;
sonicThread.c file sonicThread.c文件
void* setup(void *pinsPtr);
extern int threadFunc();
pthread_t pt[4];
int threadFunc()
{
struct sonicPins pinsArray[4] = { { 21, 20, 1 }, { 16, 12, 2 }, { 26, 19, 3 }, { 13, 6, 4 } };
for(int i =0; i <4; i++){
pthread_create(&pt[i], NULL, setup, &pinsArray[i] );
}
return 1;
}
void* setup(void *pinsPtr)
{
struct sonicPins *ptr = pinsPtr;
int trig = 0, Echo = 0, id;
trig = ptr->trig;
Echo = ptr->echo;
id = ptr->id;
struct results *storePtr;
}
The snippet above does update the struct "results", all threads does work concurrently each sensor giving out is own result. 上面的代码片段确实更新了结构“结果”,所有线程同时工作,每个传感器发出的都是自己的结果。
Main.c Main.c
int main(){
//void(*foo1)(int, int, int);
//foo1 = &calcSonicS;
printf("In operation\n");
int operational = 1;
while(operational ==1)
{
//sonic range finders.
calcSonicS();
//gyroscope and acceometer.
}
return 0;
}
Error output: 错误输出:
sonicNav.c: In function ‘calcSonicS’:
sonicNav.c:5:28: error: ‘results’ undeclared (first use in this function)
sonicNav.c:5:28: note: each undeclared identifier is reported only once for each function it appears in
struct results *rData = results;
error: 'results' undeclared (first use in this function) 错误:未声明“结果”(此功能首次使用)
The above line tries to declare and define a local variable named rData
, which has type struct results *
, and initialise it with the value of the variable (local or global) results
. 上一行试图声明并定义一个名为rData
的局部变量,该变量的类型为struct results *
,并使用变量results
(本地或全局)的值对其进行初始化。 The error message is telling you that there is no such variable. 错误消息告诉您没有这样的变量。
What you're probably mixing up is C++ (old, bad) style initialisation: 您可能正在混淆的是C ++(旧的,糟糕的)样式初始化:
MyClass variable = MyClass();
Since the next thing you do with rData
is assigning it ... 由于您对rData
做的下一件事是分配它...
rData = malloc(sizeof(struct results));
... the solution to your issue is to just remove that "wrong initialisation" from the preceeding line altogether. ...解决您的问题的方法是从前面的行中完全删除“错误的初始化”。 You could also pack it into a single line: 您也可以将其打包为一行:
struct results *rData = malloc(sizeof(struct results));
Looking at ... 看着 ...
typedef struct results{
// ...
} rData;
... I'd guess that you have a serious misunderstanding of the relationship of structure (type) names, type names and variable names. ...我想您对结构(类型)名称,类型名称和变量名称之间的关系有严重的误解。 The above definition gives you: 上面的定义为您提供:
results
as structure (type) name, so it can be used after struct
to name the defined structure type. 名称results
如结构(类型)的名称,因此它可以后使用struct
命名定义的结构类型。 rData
as type name, referring to the same (structure) type as struct results
. 名称rData
作为类型名称,指的是与struct results
相同的(结构)类型。 When you then declare a variable struct results *rData
you have additionally rData
as name for a variable. 然后,当声明变量struct results *rData
您还将 rData
作为变量的名称。 This is possible, but far from good style. 这是可能的,但远非好风格。
If you remove the typedef
, then things would change drastically: You'd then have a global variable named rData
of type struct results
. 如果删除typedef
,那么情况将发生巨大变化:然后,您将拥有一个名为rData
的全局变量,其类型为struct results
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.