0
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct PROCESS{
        int priority;
        int lifecycle;
        int ttl; 

}process1,process2,process3,process4,process5,process6;

main(){
       PROCESS *waiting_queue;
       waiting_queue = process1;      //this is were I get the error. 
       waiting_queue =(PROCESS *)malloc(6*sizeof(PROCESS));
       if(!waiting_queue){printf("no memory for waiting queue   "); exit(0);}


       getch();       
}

I am trying to create a struct array with pointer. I am getting the error. Expected primary expression before ';' token

mfd
  • 25
  • 1
  • 3
  • You're mixing up the declaration of a *type* and a *struct* in a rather broken way. AFAICT, `process1` in the line where you get the error is a *type*, not a structure, rendering the statement somewhat illegal. – DevSolar Nov 25 '16 at 08:08
  • [Don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv Nov 25 '16 at 08:12

2 Answers2

4

You should create your struct object from (process1 to process6).

Let me give you an example:

#include <stdio.h>
#include <string.h>

typedef struct student 
{
  int id;
  char name[20];
  float percentage;
} status;

int main() 
{
  status record;
  record.id=1;
  strcpy(record.name, "Orcun");
  record.percentage = 86.5;
  printf(" Id is: %d \n", record.id);
  printf(" Name is: %s \n", record.name);
  printf(" Percentage is: %f \n", record.percentage);
  return 0;
}

This is why you are getting your error in your main function. So you should create your struct object like:

process1 processOrcun;

You can also check here : https://www.codingunit.com/c-tutorial-structures-unions-typedef

Prometheus
  • 1,522
  • 3
  • 23
  • 41
3

You have multiple problem, but the one causing your error is that you don't define a type PROCESS, but a structure with that name.

When you use typedef to define a structure type, the type-name comes after the structure:

typedef struct
{
    ...
} PROCESS;

The error you have is because you define e.g. process1 as a type, so the assignment (making a pointer point to a type) doesn't make any sense.


Another, and unrelated, problem is how you define the main function. It must be defined to return an int (even if your declaration does that implicitly, it's good to do it explicitly) and to either have void for argument or an integer and an array of pointers to char. In your case it should look like

int main(void) {
    ...
    return 0;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you my friend; I did it as you instructed but giving the same error. – mfd Nov 25 '16 at 08:08
  • @mfd What is the type of `waiting_queue`? What is the type of `process1`? Are the types compatible (answer is no)? So no you won't get the *same* error, but a different one. Also, that assignment is useless since you then directly reassign `waiting_queue`. – Some programmer dude Nov 25 '16 at 08:10
  • their types are PROCESS. your idea inspired me and I found the solution; waiting_queue = &process1; THANK YOU. – mfd Nov 25 '16 at 08:19
  • @mfd Not quite. The type of `waiting_queue` is `PROCESS*` which is *very* different from the type of `process1` which is `PROCESS`. That's why you need the address-of operator `&`. And again, why do that assignment if you are just going to reassign `waiting_queue` in the very next statement? – Some programmer dude Nov 25 '16 at 08:20