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
#include<stdio.h>
#include<stdlib.h>
#include"sonicThread.h"
extern void calcSonicS();
sonicThread.c file.
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
#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
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
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. The error message is telling you that there is no such variable.
What you're probably mixing up is C++ (old, bad) style initialisation:
MyClass variable = MyClass();
Since the next thing you do with rData is assigning it ...
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:
The name results as structure (type) name, so it can be used after struct to name the defined structure type.
The name rData as type name, referring to the same (structure) type as struct results.
When you then declare a variable struct results *rData you have additionally rData as name for a variable. 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.
Related
I'm new and i try to create a struct of stuct on C code. I don't understand how i can inizialize a struct of struct. Someneone help me?
I have:
#define SIZEHOSP 200
#define DIM 200
#define SIZESICK 2000
typedef struct SICKREGION {
char firstName[20];
char lastName[20];
char fiscalCode[16];
enum stateSick;
diagnosisDate data;
healingDate dataH;
};
typedef struct HOSPITAL {
char nameHospital[30];
int codeHospital;
char addressHospital[40];
char departmentManager[30];
int beds;
int bedsIntensiveCare;
};
HOSPITAL hospital[SIZEHOSP];
typedef struct REGION {
char nameRegion[20];
int codeRegion;
char MainTownRegion[15];
char namePresidentRegion[20];
int numberHospital;
int numberSickRegion;
HOSPITAL hospital[SIZEHOSP];
SICKREGION sickregion[SIZESICK];
};
REGION region[DIM] = {
{"Sicilia", 0004, "Palermo", "Musumeci", 40, 150},
{"sardegna", 4444, "cagliari", "pippo", 200, 50},
{"calabria", 0000, "reggio", "Josh", 12, 18}
};
for example i inizialized 3 type of REGION. but they are incomplete because i don't know how insert the value of structure HOSPITAL and SICKREGION inside the region[DIM]. What is the syntax? I hope he explained the problem well.
How do I initialize a struct containing another struct?
There are several ways to initialize a struct. To simplify, the following example uses smaller structs than those you provided...
The following will illustrate initialization with initialization with values ( = {,,,{,,}}; ), then with zeros = {0} :
typedef struct {
int count;
float cash;
char item[50];
}Purchase;
typedef struct {
int accnt;
char acct_name[50];
Purchase purch;
} Acct;
Acct acct = {100123, "Robert Baily", {15, 12.50, "Tires"}};
//Or, using member names to self document the initialization statement as suggested in comments:
Acct acct1 = Acct acct = {.accnt=100123, .acct_name="Robert Baily", {.count=15, .cash=12.50, .item="Tires"}};
Acct acct2 = {0};
int main(void)
{
printf("acct = %d\nAcct_name = %s\nitem = %s\ncount = %d\ncash = %3.2f\n", acct.accnt, acct.acct_name, acct.purch.item, acct.purch.count, acct.purch.cash);
printf("acct2 = %d\nAcct_name = %s\nitem = %s\ncount = %d\ncash = %3.2f\n", acct2.accnt, acct2.acct_name, acct2.purch.item, acct2.purch.count, acct2.purch.cash);
return 0;
}
Although these are small, they illustrate what you are doing with your larger, more complicated structs. I suggest that for your structs it will be extremely tedious, and probably not necessary to use the first method. struct declaration in an actual program is often initialized by zeroing. i.e. {0}
I am trying to store the main features of my hardware configuration in one place, so I created structures for it. I declared them as typedef in a header file, and I want to create and define the actual structures in a function, that I can call when the application starts. (I am using an STM32F04 board with chibiOS running on it.)
Here is the header file:
#ifndef HW_INIT_INCLUDED
#define HW_INIT_INCLUDED
#include <mcuconf.h>
#include <hal.h>
#include <ch.h>
struct Recievers;
struct LEDs;
struct Ultrasonic;
typedef struct Recievers{
int iD;
int port;
int pinNumber;
} Reciever;
typedef struct LEDs{
int iD;
char port;
int pinNumber;
bool status;
float dutyCycleConstant;
} LED;
typedef struct Ultrasonic{
char port;
int pinNumber;
}Ultrasonic;
/*
Initaliases the values of hw parameters.
*/
void hardWareInit(struct Recievers Reciever, struct LEDs LED, struct Ultrasonic Ultrasonic);
#endif // HW_INIT_INCLUDED
and here is the source code:
#include <hw_init.h>
#include <stdlib.h>
void hardWareInit(struct Recievers Reciever, struct LEDs LED, struct Ultrasonic Ultrasonic)
{
Reciever Reciever1, Reciever2, Reciever3, Reciever4;
LED LED1, LED2, LED3;
Ultrasonic Ultrasonic1;
Reciever1.iD = 1;
Reciever1.port = GPIOF;
Reciever1.pinNumber = 1;
Reciever2.iD = 2;
Reciever2.port = GPIOG;
Reciever2.pinNumber = 1;
Reciever3.iD = 3;
Reciever3.port = GPIOI;
Reciever3.pinNumber = 1;
Reciever4.iD = 3;
Reciever4.port = GPIOE;
Reciever4.pinNumber = 1;
LED1.iD = 1;
LED1.port = GPIOD;
LED1.pinNumber = 16;
LED1.status = 0;
LED1.dutyCycleConstant = 0.3;
LED2.iD = 2;
LED1.port = GPIOD;
LED2.pinNumber = 16;
LED2.status = 0;
LED2.dutyCycleConstant = 0.3;
LED3.iD = 3;
LED1.port = GPIOD;
LED3.pinNumber = 16;
LED3.status = 0;
LED3.dutyCycleConstant = 0.3;
Ultrasonic1.port = GPIOC;
Ultrasonic1.pinNumber = 1;
}
When I try to compile, it basically gives these errors for every struct I try to create:
hw_init.c:8:5: warning: statement with no effect [-Wunused-value]
Reciever Reciever1;
^
hw_init.c:8:14: error: expected ';' before 'Reciever1'
Reciever Reciever1;
When I try to compile, it basically gives these errors for every struct I try to create:
hw_init.c:8:5: warning: statement with no effect [-Wunused-value] Reciever Reciever1;
hw_init.c:8:14: error: expected ';' before 'Reciever1' Reciever Reciever1;
You get the error message because you use the word Reciever for a typedef'd struct as well as the first function parameter:
void hardWareInit(struct Recievers Reciever, struct LEDs LED, struct Ultrasonic Ultrasonic)
You should use a diffent designator for the first parameter.
Further I suggest to name reciever to receiver.
my code as follows:
here I declare the publicgroup that after "struct group":
When I try to use the method, there is an error"struct group is incompatible with parameters of type struct group". I am using VS2013.
The error is in "int value = isgroupCointainsPID(publicgroup, 300);"This is the screen shot in my VS:
The strang thing is that if I ust the commented "test", it works well.
I am not quite sure ,what's wrong about my code?
struct proNode{
int pID;
struct proNode *next;
};
char groups [3][128];
struct group{
int gID;
char *name;
struct proNode *prolist;
struct proNode *blacklist;
}publicgroup;
int isgroupCointainsPID(struct group _group, int pID){
if (_group.prolist == NULL){
printf("There is no process %d\n", pID);
return 0;
}
struct proNode *pros = _group.prolist;
while (pros != NULL){
if (pros->pID == pID)
return 1;
pros = pros->next;
}
printf("There is no process %d\n", pID);
return 0;
}
int main(){
publicgroup.gID = -1;
publicgroup.name = "public group";
publicgroup.prolist = NULL;
publicgroup.blacklist = NULL;
publicgroup.gID = 2;
// there is an error :struct group is incompatible with parameters of type struct group
int value = isgroupCointainsPID(publicgroup, 300);
return 0;
}
Incompatible parameters error is possible when you miss the function prototype. So please check whether you written the function prototype or not
i guest you wrong when typing that method paramaters,
i never use VS so i cant give much advice,
i guest you want scan that usergroup, so you want pass it as pointer?
As i know when i use codeblocks, just try to change this
isgroupCointainsPID(struct group _group, int pID)
to
isgroupCointainsPID(group _group, int pID)
i hope its work
ok I have three structs:
struct rss_s {
Radio_types device_type; // Its device_type which is defined by the typedef above Radio_Types
char * device_info; // some thing about the radio NAV/COM/etc.
char * device_model; // the Manufactures part/model number.
char * device_serial; // the device's serial number..
int power_48v; // power to the unit..
int power_400hz;
int panel_lamps; // turn off or on the Panel Lamps only
void * radio_info;
struct radio_s_C614L8
{
loopsw_614L8 loop_sw_614L8; this is an emum
modesw_614L8 mode_sw_614L8; this is an emum
int sw_band;
int sw_bfo;
int meter;
tuner *Tuner;
int tuners;
};
typedef struct tuner_s
{
char *device_name; // OS NAME
int frequency[tuned];
int power;
int dial_lamp;
void * back_radio; // back-link to radios[n]
void * back_info; // back-link to radio_xxxx
int fd[];
} tuner;
I initialize them in main.c
// Radio 614L8
static tuner tuner_C614L8[] = {{ .device_name = "/dev/TBD", }};
static struct radio_s_C614L8 radio_C614L8 = { .Tuner = &tuner_C614L8, .tuners = DIM(tuner_C614L8) };
static struct rss_s radios[] = {
{ .device_type = C614L8,
.device_info = "ADF",
.device_model = "614L8",
.device_serial = "8384",
.radio_info = &radio_C614L8,},};
the above works with out errors....
but when I try to initialize the the above radio... in my init_C614L8.c
with the following code I get an error...
error: dereferencing pointer to incomplete type in lines 4 & 6
int init_C614L8( struct rss_s * radios ){
int rw, i;
struct radio_s_614L8 * rad_info = radios -> radio_info;
tuner * this_tuner = rad_info -> Tuner;
// Now we will loop over the sub_devices....
for ( i = 0; i < rad_info -> tuners; i++ ) {
I think I have to cast something but not shure
Thanks
In rss.h you declare
struct radio_s_C614L8
but in init_C614L8.c you use
struct radio_s_614L8
which is declared nowhere.
Update:
To fix this error
error: dereferencing pointer to incomplete type
In init_C614L8.c (and any other place, but rss.h) replace
struct radio_s_614L8
by
struct radio_s_C614L8
The lesson learned here is either go for some glasses or some sleep! ;-) And also: "The compiler never lies!"
This means that the definition of struct radio_s_614L8 is not visible to the code where the errors are seen. You have either forgotten to include the definition or there are #if... directives removing the definitions or includes you think are there.
Here are (some of) the structures that I am using; they are in a .h file:
struct rss_s {
Radio_types device_type; // Its device_type which is defined by the typedef above Radio_Types
char * device_info; // some thing about the radio NAV/COM/etc.
char * device_model; // the Manufactures part/model number.
char * device_serial; // the device's serial number..
int power_48v; // power to the unit..
int power_400hz;
int panel_lamps; // turn off or on the Panel Lamps only
void * radio_info;
};
typedef struct tuner_s { // when we talk about 'sub-radios' we are really saying how many tuners are there??
char * device_name; // OS-name
int frequency[tuned];
int power;
int dial_lamp;
int fd[ ]; // file descriptors
}tuner;
//// 614L8 ::= C614L8
typedef enum Lp_Sw_614L8 { OFF_loop, LEFT, RIGHT, SLEW_LEFT, SLEW_RIGHT } loopsw_614L8;
typedef enum Mo_Sw_614L8 { OFF_614L8, ADF, ANT, LOOP } modesw_614L8;
struct radio_s_614L8 {
loopsw_614L8 loop_sw_614L8;
modesw_614L8 mode_sw_614l8;
int sw_band;
int sw_bfo;
int meter;
tuner * Tuner;
int tuners;
};
Now file main.c, which has all of the normal includes:
// Radio 614L8<br>
static struct radio_s_614L8 radio_614L8 = { { .Tuner = tuner_614L8, .tuners = DIM( tuner_C_614L8 ) } };
static tuner tuner_614L8 = { { .device_name = "/dev/TBD", } };
static struct rss_s radios[] = {
{ C614L8, "ADF", "614L8", "8384", & radio_C_614L8,},};
// now comes the normal main()
The errors that I have:
error: field name not in record or union initializer
error: (near initialization for ‘radio_614L8.loop_sw_614L8’)
error: ‘tuner_614L8’ undeclared here (not in a function)
error: field name not in record or union initializer
error: (near initialization for ‘radio_614L8.loop_sw_614L8’)
error: ‘tuner_C_614L8’ undeclared here (not in a function)
error: field name not in record or union initializer
error: (near initialization for‘tuner_614L8.device_name’)
error: ‘radio_C_614L8’ undeclared here (not in a function)
You currently have:
static struct radio_s_614L8 radio_614L8 = { { .Tuner = tuner_614L8, .tuners = DIM( tuner_C_614L8 ) } };
static tuner tuner_614L8 = { { .device_name = "/dev/TBD", } };
You need:
static tuner tuner_614L8 = { .device_name = "/dev/TBD", };
static struct radio_s_614L8 radio_614L8 = { .Tuner = &tuner_614L8, .tuners = 1 };
You can't refer to a variable like tuner_614L8 until you've defined or declared it. You shouldn't try to make a non-array into an array, either. You do need to take the address of the tuner, too. You don't show DIM, but I'm assuming it is more or less one of these two equivalent macros:
#define DIM(x) (sizeof(x)/sizeof(*(x)))
#define DIM(x) (sizeof(x)/sizeof((x)[0]))
On further analysis, your tuner structure contains a flexible array member. You can't sensibly allocate such variables as static or global variables, or as automatic variables; you have to allocate them with malloc() and relatives to get a non-empty array.
However, with that caveat in mind, this code compiles:
typedef enum Radio_types { C614L8 } Radio_types;
enum { tuned = 5 };
typedef struct tuner_s
{
char *device_name;
int frequency[tuned];
int power;
int dial_lamp;
int fd[];
} tuner;
typedef enum Lp_Sw_614L8 { OFF_loop, LEFT, RIGHT, SLEW_LEFT, SLEW_RIGHT } loopsw_614L8;
typedef enum Mo_Sw_614L8 { OFF_614L8, ADF, ANT, LOOP } modesw_614L8;
struct radio_s_614L8
{
loopsw_614L8 loop_sw_614L8;
modesw_614L8 mode_sw_614l8;
int sw_band;
int sw_bfo;
int meter;
tuner *Tuner;
int tuners;
};
static tuner tuner_614L8 = { .device_name = "/dev/TBD", };
static struct radio_s_614L8 radio_614L8 = { .Tuner = &tuner_614L8, .tuners = 1 };
struct rss_s
{
Radio_types device_type;
char *device_info;
char *device_model;
char *device_serial;
int power_48v;
int power_400hz;
int panel_lamps;
void *radio_info;
};
struct rss_s radios[] =
{
{ C614L8, "ADF", "614L8", "8384", 0, 0, 0, &radio_614L8, },
};