c: Struct type incompatiable error - c

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

Related

Apparently allocating memory and freeing it properly but program still crashes

So I've got a weird problem and can't seem to solve it. I have an ADT called TEAM:
typedef struct Team {
char *name;
int points;
int matches_won;
int goal_difference;
int goals_for;
}TEAM;
I created a function to initialize variables of the TEAM* type with a given name:
TEAM *createTEAM (char *name){
int error_code;
if (name != NULL){
if(strcmp(name, "") != 0){
TEAM *new_team = (TEAM*)malloc(sizeof(TEAM));
new_team->name = (char*)malloc(sizeof(char)*strlen(name));
strcpy(new_team->name, name);
new_team->points = 0;
new_team->matches_won = 0;
new_team->goal_difference = 0;
new_team->goals_for = 0;
return new_team;
}else{
error_code = EMPTY_STRING_CODE;
}
} else {
error_code = NULL_STRING_CODE;
}
printf("Erro ao criar time.\n");
printError(error_code);
return NULL;
}
I also created a function to delete one of these TEAM* variables properly:
void deleteTEAM (TEAM *team_to_remove){
free(team_to_remove->name);
team_to_remove->name = NULL;
free(team_to_remove);
team_to_remove = NULL;
}
But when one or multiple test functions that I created (example below) run, the program sometimes crashes, sometimes doesn't. I've noticed that changing the names I use affects whether it crashes or not, even if they don't affect the test results.
int create_team_01(){
int test_result;
TEAM *Teste = createTEAM("Cruzeiro");
if (strcmp(Teste->name, "Cruzeiro") == 0){
test_result = TRUE;
}else test_result = FALSE;
_assert(test_result); //just a macro function that will check the argument and return 1 if it's false
deleteTEAM(Teste);
return 0;
}
I don't see any problems with memory allocation or freeing. Still, the debugger complains a lot about the first free() (can't find bounds) of the deleteTEAM function. Any ideas? Thanks a lot in advance for any help.
P.S.: I've even tried checking the mallocs' results, but it doesn't seem to be the problem either, so I removed it for the sake of simplicity.

How do you use a typedef struct for a FIFO?

I just started programming in C for school. I am being asked to do a program that uses a FIFO struct to resolve math problems. I got the folowing code on the internet for a FIFO, I just don't know how to use it. I tried a lot of things and I can't find anything useful on the internet or maybe that I just don't know the right thing to research but could you please help me? Thanks!
#include <stdio.h>
#include <stdlib.h>
typedef struct pile
{
int donnee;
struct pile *precedent;
} Pile;
void pile_push(Pile **p_pile, int donnee)
{
Pile *p_nouveau = malloc(sizeof *p_nouveau);
if (p_nouveau != NULL)
{
p_nouveau->donnee = donnee;
p_nouveau->precedent = *p_pile;
*p_pile = p_nouveau;
}
}
int pile_pop(Pile **p_pile)
{
int ret = -1;
if (p_pile != NULL)
{
Pile *temporaire = (*p_pile)->precedent;
ret = (*p_pile)->donnee;
free(*p_pile), *p_pile = NULL;
*p_pile = temporaire;
}
return ret;
}
void pile_clear(Pile **p_pile)
{
while (*p_pile != NULL)
{
pile_pop(p_pile);
}
}
I tried doing this:
int main()
{
int return_val;
Pile pile;
pile_push(Pile, 5);
return_val = pile_pop(Pile);
printf(return_val);
}
and got this error:
expected expression before 'Pile'
too few arguments to function 'pile_push'
You have mixed up Pile and pile which is the issue with the first warning. The functions expect a pointer to a pointer to a Pile. That is: They update the value of a pointer, so they need to be passed a reference to a pointer. Your use of printf is also wrong.
int main()
{
int return_val;
Pile *pile = NULL;
pile_push(&pile,5);
return_val = pile_pop(&pile);
printf("return_val is: %d\n",return_val);
}

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
#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.

Strange characters when returning char array from struct in C

I am having some problems when returning with printf of a char array from a struct in C.
struct q_entry
{
long mtype;
char mtext[MAXLENGTH + 1];
};
The long mtype from the struct is returning fine, but the string is just returning some weird characters.
int proc_obj(struct q_entry *msg)
{
printf("\npriority: %ld name: %s\n", msg->mtype, msg->mtext);
}
It just returns some strange characters like "priority: 1 name: ▒▒(" and not "priority: 1 name: hello"
I am populating the struct using the following code
int enter(char *objname, int priority)
{
...
strncpy(s_entry.mtext, objname, sizeof(s_entry.mtext) - 1);
s_entry.mtype = priority;
// Send the message
if (msgsnd(s_qid, &s_entry, len, 0) == -1)
{
printf("error: msgsnd failed\n");
return(-1);
}
else
{
return(0);
}
}
I don't have much experience with C, so I don't know too much about using structs. Please let me know if more context or parts of the code is needed. Any kind of help would be very helpful.
I have added a little more code in enter above, and here is more code of the when enter and proc_obj are called
main(int argc, char **argv)
{
int priority;
if (argc != 3)
{
printf("error: incorrect number of arguments\n");
exit(1);
}
else
{
priority = atoi(argv[2]);
}
if (enter(argv[1], priority) < 0)
{
printf("error: message entering failed\n");
exit(1);
}
exit(0);
}
This is in a different file from enter and above code
int server(void)
{
int mlen, r_qid;
struct q_entry r_entry;
// Initialize queue
if ((r_qid = init_queue()) == -1)
return(-1);
for (;;)
{
if ((mlen = msgrcv(r_qid, &r_entry, MAXLENGTH, (-1 * MAXPRIOR), MSG_NOERROR)) == -1)
{
printf("error: msgrcv failed\n");
exit(1);
}
else
{
proc_obj(&r_entry);
}
}
}
The only obvious error in your code is that you should explicitly fill in a zero at s_entry.mtext[MAXLENGTH] such that the string will still be zero terminated if strncpy() hits the limit. But if that were the problem, you would see "hello" followed by strange characters. Are you sure that objname points to the text you're expecting it to point to?
Also, it looks a bit strange that proc_obj() is declared to return an int but actually does not return anything. Your compiler ought to complain about that.
Answer before adding more code
It looks like the s_entry structure object is local, and enter works on the local variable. How are you calling enter and returning the structure after you have done initializing it ? note that you have int as the return type of the enter function. If you are doing return s_entry; then the output you are getting is possible, as only the first word of the structure, ie the lower sizeof (int) part of mtype is considered.
If you are using enter function like this as i described above then make the return type of enter to struct s_entry
You should check the size of len when sending the message.
You don't show the message queue call but my guess is you are somehow miscalling the API and putting garbage into the queue (and then printing garbage in the server).

How to use two parameters pointing to the same structure in one function?

I have my code below that consits of a structure, a main, and a function. The function is supposed to display two parameters that have certain values, both of which point to the same structure.
The problem I dont know how to add the SECOND parameter onto the following code :
#include<stdio.h>
#define first 500
#define sec 500
struct trial{
int f;
int r;
float what[first][sec];
};
int trialtest(trial *test);
main(){
trial test;
trialtest(&test);
}
int trialtest(trial *test){
int z,x,i;
for(i=0;i<5;i++){
printf("%f,(*test).what[z][x]);
}
return 0;
}
I need to add a new parameter test_2 there (IN THE SAME FUNCTION) using this code :
for(i=0;i<5;i++){
printf("%f,(*test_2).what[z][x]);
How does int trialtest(trial *test) changes ?
and how does it change in main ?
I know that I should declare test_2 as well, like this :
trial test,test_2;
But what about passing the address in the function ? I do not need to edit it right ?
trialtest(&test); --- This will remain the same ?
So please, tell me how would I use test_2 as a parameter pointing to the same structure as test, both in the same function..
Thank you !!
Please tell me if you need more clarification
I think that this is your homework, so I'll just write a different function that may give you an idea of what (I think) you need to do. I read that you don't want to change the trail_test parameter list, so I stuck with a similar parameter list.
struct thing {
/* put some stuff here */
};
typedef struct thing thing; /* because this is C, not C++ */
int how_many_things(thing * thing_list);
int main(void) {
int i;
thing * a;
int count_init = random(); /* let's surprise ourselves and make a random number of these */
count_init %= 128; /* but not too many or it might not work at all */
a = malloc(count_init*sizeof(things)+1);
for (i = 0; i < count_init; i++) {
thing_init(&(a[i]));
}
make_illegal_thing(&(a[count_init]) ); /* like '\0' at the end of a string */
printf("There are %i things in the list\n", how_many_things(a) );
return 0;
}
/* This is very similar to strlen */
int how_many_things(thing * a) {
int count = 0;
while (is_legal_thing(a) ) {
a++;
count++;
}
return count;
}

Resources