request member for 'name' in something not structure or union - c

i have this two structures
typedef struct pokemon_move_t {
char* name;
PokemonType type;
int power_points, max_power_points;
int strength;
} *PokemonMove;
typedef struct pokemon_t {
char* name;
PokemonType type;
int experience;
int health_points;
PokemonMove* moves;
int number_of_moves, max_number_of_moves;
} *Pokemon;
and i have a function that receives pokemon struct and i'm trying to reach the name field in the function and it shows me the error message in the title,i tried everything that suggested before and it didn't work, the function is(not complete) :
int pokemonMoveName(Pokemon pokemon){
char* name= pokemon->moves->name; //the error is in this line
return 0;
}

The element moves is:
PokemonMove * moves;
Which is:
struct pokemon_move_t ** moves;
And not:
struct pokemon_move_t * moves;
... it is a pointer to a pointer to a structure and not to a structure itself.
I think that you don't want this!
Either you have to remove the * at the typedef or the * in the struct.
If moves really is a pointer to a pointer you'll have to access it the following way (or similar):
char* name= (*(pokemon->moves))->name;
... which is equal to:
char* name= pokemon->moves[0]->name;
... or, if moves points to an array:
char* name= pokemon->moves[index]->name;

I think you mean the following declaration of the data member
typedef struct pokemon_t {
char* name;
PokemonType type;
int experience;
int health_points;
PokemonMove moves;
^^^^^^^^^^^^^^^^^
int number_of_moves, max_number_of_moves;
} *Pokemon;
In this case this statement
char* name= pokemon->moves->name;
will be valid.
The type PokemonMove is already declared like a pointer type.
typedef struct pokemon_move_t {
char* name;
PokemonType type;
int power_points, max_power_points;
int strength;
} *PokemonMove;
^^^^^^^^^^^^

Related

Assigning values and passing struct as parameter

To asign a char value into the struct 'field', I have to use strcpy ?
I tried this:
struct student{
char name[50];
int age;
};
int main(){
struct student std;
std.name = "john"; //Doesnt work
strcpy(std.name, "john"); //It DOES work
std.age = 20;
return 0;}
Why when comes to char I can not simply use the ' = ' to assign a value ?
How may I pass a struct initialized in main(){} as a parameter to a function and change it's values inside the function without the need of a return.
Do I just use the '*' like:
void MyFunction(struct student *std){
std->Name = "John";
std->Age = 20;
}
int main(){
struct student me;
Myfunction(me);
}
Is that the correct way to do so ?
No matter which way you pass the struct (by value or by pointer), you cannot directly assign a string literal to a char array. You can only use strcpy or strncpy or something else that copies the characters one by one.
But there is a workaround, you can directly assign struct to another one. C compiler will perform a bitwise copy of the struct.
struct student s1;
strcpy(s1.name, "john");
s1.age = 10;
struct student s2;
s2 = s1; // now s2 is exactly same as s1.
Attach an example of use pointer to pass struct:
void handle_student(struct student *p) { ... }
int main() {
struct student s1;
handle_student(&s1);
}
This is just an additional information
While declaring the structure variable, it can be initialized as below.
int main(){
struct student s1 = {"John", 21};
//or
struct student s = {.name= "John" };
}

How can i pass a structure to a function

I have called a function with seven parameters from main
find_sync(es_data + (loop_count * size) + chunk_bytes_counter,
size - chunk_bytes_counter, &sync_index, &flag,
&sync_length, &chunk_bytes_counter, &total_bytes_counter);
in function.c:
void find_sync(char data[], size_t size, unsigned int *sync_index, int *flag, unsigned int *sync_length, unsigned int *chunk_bytes_counter, unsigned int *total_bytes_counter)
prototype in header file:
extern void find_sync(char data[], size_t size, unsigned int *sync_index, int *flag, unsigned int *sync_length, unsigned int *bytes_counter, unsigned int *total_bytes_counter);
Now, my question is, how can i declare all these 7 parameters in a structure, so that i can only pass one structure variable.
Begin by declaring the struct:
struct find_sync_parameters {
char* data;
size_t size;
unsigned int *sync_index;
int *flag;
unsigned int *sync_length;
unsigned int *bytes_counter;
unsigned int *total_bytes_counter;
}
Then change your function signature either to:
void find_sync(struct find_sync_parameters param)
Or to
void find_sync(struct find_sync_parameters *param)
In the first case the whole struct will be pushed onto the stack before transferring control to find_sync. On the second case only a pointer to the struct (stored elsewhere) will be pushed.
There are advantages and drawbacks in each one. When passing a pointer note that the function can change the contents (this can be positive: for returning values directly inside the struct; also can be negative: the caller cannot be sure if its data were changed or not). If the struct is too big (not your case), then pushing everything onto the stack can take a significant amount of time and become a performance hit.
Inside the function you use it either with '.' (dot, the first case) or '->' (arrow, the second case) operator.
To call it:
struct find_sync_parameters p = { ... };
find_sync(p); // first case
find_sync(&p); // second case
If you find it annoying to type struct find_sync_parameters everytime you can define a new type with typedef:
typedef struct find_sync_parameters find_sync_parameters;
Or in one line (struct and typedef definitions):
typedef struct find_sync_parameters {
...
} find_sync_parameters;
Or even without struct name (anonymous struct)
typedef struct {
...
} find_sync_parameters;
In this last case you cannot reference the struct itself inside the struct (the case, for example, with linked list nodes).
Just put in structure .
struct pars_t
{
char data[];
size_t size; unsigned int *sync_index;
int *flag; unsigned int *sync_length;
unsigned int *bytes_counter;
unsigned int *total_bytes_counter;
} pars;
and then call foo (pars_t par)
You can create a struct (and typedef it at the same time, to make it usable without saying "struct" every time) like so:
typedef struct _find_sync_str{
char* data;
size_t size;
unsigned int *sync_index;
int *flag;
unsigned int *sync_length;
unsigned int *bytes_counter;
unsigned int *total_bytes_counter;
} find_sync_str
Then you can list the function as:
void find_sync(find_sync_str f);
I am going to suggest:
struct find_sync_struct {
char* data;
size_t size;
unsigned int sync_index;
int flag;
unsigned int sync_length;
unsigned int bytes_counter;
unsigned int total_bytes_counter;
};
Change the input argument of find_sync to:
void find_sync(struct find_sync_struct* strPtr);
Call the function using:
struct find_sync_struct str;
// Set str.data to something suitable.
// ....
find_sync(&str);
Here is a simple example demonstrating how to pass a structure to a function:
#include<stdio.h>
#include<conio.h>
//-------------------------------------
struct Example
{
int num1;
int num2;
}s[3];
//-------------------------------------
void accept(struct Example *sptr)
{
printf("\nEnter num1 : ");
scanf("%d",&sptr->num1);
printf("\nEnter num2 : ");
scanf("%d",&sptr->num2);
}
//-------------------------------------
void print(struct Example *sptr)
{
printf("\nNum1 : %d",sptr->num1);
printf("\nNum2 : %d",sptr->num2);
}
//-------------------------------------
void main()
{
int i;
clrscr();
for(i=0;i<3;i++)
accept(&s[i]);
for(i=0;i<3;i++)
print(&s[i]);
getch();
}

Initializing a Structure that's within a Structure through a Pointer that points to the first Structure

Basically, I want to make a Pointer to an object from stu_data and then initialize all the variables in it(including the variables of the clg_data structure within). Problem is that I don't know how to access
the second structure with a Pointer.
There's an example of me trying to do that below (in void main()).
typedef struct {
int college_id;
char college_name[50];
} clg_data;
typedef struct {
int id;
char name[20];
float percentage;
// structure within structure
clg_data clg_data;
} stu_data;
stu_data *p;
void main()
{
stu_data STU1;
p = &STU1;
p->clg_data->college_id = 3; //STU1.clg_data.college_id = 3; that's basically what I'm trying to achieve here with p.
}
p->clg_data is not a pointer. Use . to access its members
p->clg_data.college_id = 3;

Defining a pointer in a struct

I've create a structure Person and had these variables in it, then I added a new field named father and its type is pointer to a person.
I have to initialize the data of FJames as following: fname = Whatever, lname = Bond, age = 80, job = Farmer, father = NULL
Then initialize the data of James as following: fname = James, lname = Bond, age = 40, job = Actor, father = FJames
Then display all the data.
I'm getting an error " initializing struct Person * with an expression of incompatible type "Person"
What to do? :/
I don't even think I'm doing it right, please help!
/#include <stdio.h>
typedef struct {
int age;
char *fname;
char *lname;
char *job;
struct Person *father;
}Person;
int main(int argc, const char * argv[])
{
Person James;
Person FJames = {80,"Whatever","Bond","Painting",NULL};
James.age = 40;
James.fname = "James";
James.lname = "Bond";
James.job = "Engineering";
James.father = FJames;
}
You don't declare struct Person actually.
You're declaring anonymous structure and typedef it to person. You then should use it as just Person, not struct Person.
struct Person {
struct Person *father; // this will work
}
or if you want typedef
typedef struct s_Person {
struct s_Person *father
} Person;
typedef struct {
int age;
char *fname;
char *lname;
char *job;
struct Person *father; // << This is a pointer to a Person
} Person;
James.father = FJames;
FJames is not a Person*. He's a Person. You need to malloc him in order to get a Person*. Or take his address with &
This code has some other issues, but that's the one that's giving you the error in question.
James.father = &FJames should be what you need.
There are other minor problems. These days initialising a char* from a string constant is frowned upon, because char* implies that the memory being pointed at can be altered.
Here's a working program:
#include <stdio.h>
#include <stdlib.h>
typedef struct Person {
int age;
char *fname;
char *lname;
char *job;
struct Person *father;
} Person;
int main(int argc, const char * argv[])
{
Person James;
Person FJames = {80,"Whatever","Bond","Painting",NULL};
James.age = 40;
James.fname = "James";
James.lname = "Bond";
James.job = "Engineering";
James.father = &FJames;
fprintf(stdout, "%d\t%s\t%s\t%s\tpops:\t%s\t%s\n", James.age, James.fname, James.lname, James.job, (James.father)->fname, (James.father)->lname);
return EXIT_SUCCESS;
}
Corrections:
You should correct your typedef so that it declares Person.
When setting James.father it should dereference FJames so that you are setting it to the value of the pointer to FJames.
Your main() function should return an int, so return EXIT_SUCCESS (defined in stdlib.h) to note that you exited properly.
Advice:
When dereferencing properties of James.father, use precedence and arrow notation to dereference its values.
If you are using gcc, compile with -Wall option to enable all compilation warnings. This will help note warnings that point where corrections are needed.
That last line should be
James.father = &FJames;
The father field is a pointer, but FJames is a Person. You can use & to get the address of FJames.
Edit
The struct definition should also be changed in addition to that, in one of the ways aragaer suggested, e.g.:
typedef struct s_Person {
// ...
struct s_Person *father;
} Person;

C accessing array correctly

os-sim.h
typedef enum {
PROCESS_NEW = 0,
PROCESS_READY,
PROCESS_RUNNING,
PROCESS_WAITING,
PROCESS_TERMINATED
} process_state_t;
typedef struct _pcb_t {
const unsigned int pid;
const char *name;
const unsigned int static_priority;
process_state_t state; <<---Trying to access this
op_t *pc;
struct _pcb_t *next;
} pcb_t;
file1.c
static pcb_t **current;
extern void yield(unsigned int cpu_id)
{
/* FIX ME */
while (1)
{
pthread_mutex_lock(&current_mutex);
current[cpu_id].state = PROCESS_WAITING; ///<-------ERROR HERE
pthread_mutex_unlock(&current_mutex);
break;
}
schedule(cpu_id);
}
in main method():
current = malloc(sizeof(pcb_t*) * 10);
I have error in this line current[cpu_id].state = PROCESS_WAITING;
error: request for member ‘state’ in something not a structure or union
What does this error mean?
Is this not the right way to access current array which holds pcb_t?
If so, how do i access the current array? and state field?
You're likely looking for:
current[cpu_id]->state = PROCESS_WAITING;
The type of current is pcb_t **. So the type of current[cpu_id] is pcb_t *.

Resources