#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
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
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;
}
Related
As part of a larger project, I am trying to write a function that will allocate enough memory for a struct variable and assign values to its member variables after scanning them from the keyboard. In other words, I am trying to create a sort of a constructor function. The reason for this is because I think that's the best way to create a database-like program in c, where all the student_t variables will be stored in a student_t* array called classroom.
#include <stdio.h>
#include <stdlib.h>
#define N 10
#define STRLIM 50
typedef struct student{
char* name;
int age;
}student_t;
student_t* init_student(student_t* s);
int i=0;//its the array counter
student_t* classroom[N];
int main(int argc, char const *argv[]){
while(i<N){
// classroom[i]=(student*)malloc(sizeof(student));
init_student(classroom[i]);
classroom[i]->age=i;
printf("The age of student #%d is : %d \n",i,classroom[i]->age);
printf("the name of student #%d is : %s",i,classroom[i]->name);
i++;
}
printf("the size of the classroom is : %ld",sizeof(classroom));
return 0;
}//end of main()
student_t* init_student(student_t* s){
s=(student_t*)malloc(sizeof(student_t));
s->name=(char*)malloc(sizeof(char)*STRLIM);
fflush(stdin);
fgets(s->name,STRLIM,stdin);
return s;
}
Gdb ouput shown in the attached image here.
Please check out my repo.
I am guessing something about my build and datatypes is off .Thanks in advance.
How do I properly use one struct inside another struct using typedef in C?
This method doesn't work and I can't understand why:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *nume;
char *prenume;
data data;
} student;
typedef struct
{
int an,zi,luna;
} data;
int main()
{
student Andrei;
scanf("%s %s %d ",Andrei.nume,Andrei.prenume,&Andrei.b.an);
printf("%s %s %d ",Andrei.nume,Andrei.prenume,Andrei.data.an);
return 0;
}
There are actually a number of errors in your code!
First, you need to declare/define any struct object before you use that as a member of another struct.
Second, your nume and prenume members are declared as pointers to characters but they are not initialized to anything, nor is any memory allocated for those string.
Third, you have a 'typo' in your scanf line: Andrei.b.an should, presumably, be Andrei.data.an.
Finally (I think), because you have a trailing space in the format string for scanf, the function will need at least one 'extra' input field in order to complete.
Here is a potential 'fix', with comments added where I've made changes:
#include <stdio.h>
// <stdlib.h> // You don't use this - perhaps thinking of using "malloc" for "nume" and "prenume"??
typedef struct // This MUST be defined BEFORE it is used as a member in the following struct!
{
int an, zi, luna;
}data;
typedef struct
{
char nume[32]; // Here, we create fixed char arrays (strings) for "nume"...
char prenume[32]; // ... and "prenume". You can change the sizes, as you need!
data data;
} student;
int main()
{
student Andrei;
scanf("%s %s %d", Andrei.nume, Andrei.prenume, &Andrei.data.an); // Note: removed trailing space from format!
printf("%s %s %d", Andrei.nume, Andrei.prenume, Andrei.data.an);
return 0;
}
The errors that prevent my code from compiling are:
[Error] expected identifier or '(' before '.' token
[Error] expected expression before 'books'
[Error] too few arguments to function 'fread'
I'm new to C programming and programming altogether. Although I'm used to Pascal. The errors occur in all functions except in the main and void menu. This is only a part of my code.
Please can someone help me in solving these errors this program.
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
char catergories[][15]={"Computer","Philosophy","Arts","Literature","Science","History"};
void menu(void);
void addbooks(void);
void deletebooks(void);
void updatebooks(void);
void findbooks(void);
void sellbooks(void);
void viewbooks(void);
int enterinfo();
int checkid(int);
void Password();
//list of global files that can be accessed from anywhere in program
FILE *fp,*ft,*fs;
//list of global variable
int s;
char findbook;
char password[10]={"dominique"};
typedef struct
{
int mm,dd,yy;
}OrderDate;
typedef struct
{
int id;
char name[35];
char author[35];
int quantity;
float price;
int count;
int shelf;
char *cat;
struct OrderDate *sold;
}books;
void addbooks(void) //funtion that add books
{
system("cls");
int i;
struct books;
printf("\n ******************************************************\n");
printf("\n \t\t ADD A BOOK");
printf("\n ******************************************************\n\n");
printf("\n SELECT THE CATERGORY OF THE BOOK:");
printf("\n 1. Computer Science, Information & General Works");
printf("\n 2. Philosophy, Psychology and Religion");
printf("\n 3. Arts and Recreation");
printf("\n 4. Literature");
printf("\n 5. Science");
printf("\n 6. History and Geography");
printf("\n 7. Back to main menu\n");
printf("\n Enter your choice:");
scanf("%d",&s);
if(s==7)
{
menu() ;
}
system("cls");
fp=fopen("BookRecords.txt","ab+");
if(enterinfo()==1)
{
books.cat=catergories[s-1]; //the error that exists here states expected identifier or '(' before '.' token
fseek(fp,0,SEEK_END);
fwrite(&books,sizeof(books),1,fp); //the error that exists here states expected expression before 'books'
fclose(fp);
printf("\n The book's information has been saved sucessfully");
printf("\n Would you like to save more information? (Y/N):");
if(getch()=='n')
{
menu();
}
else
system("cls");
addbooks();
}
}
struct books;
books is your type name. Like int or char. Thats why books.id==d won't work. You have to declare variable that is type of books. For example:
books BooksInMyShop;
Now you can use your variable in code ex.:
if(BooksInMyShop.id == currentId)
PS1: When You define:
typedef struct
{
(...)
}books;
You dont need to declare variable by struct books BooksInMyShop; simple books BooksInMyShop; will work now.
PS2: I can see in your code lots of missconceptions. For example "variable" books in every function in not shared beetween them. I guess You have to pass one instance of Your bookstore to every function or have one static global instance.
I want to take information of a class which contains student name,roll no. and print it
I used structure but code not working
#include<stdio.h>
#include <string.h>
int size,i,j;
struct Student
{
char name[50];
int number;
};
typedef struct Student info1;
info1 print[100];
int main()
{
printf("Size of class : ");
scanf("%d",&size);
for(j=0;j<(20);)
{
for(i=1;i<=size;i++)
{
printf("%d.Name:",i);
scanf("%s",&info1.name);// i get an error here that an exp is expected
print[j]=info1.name; // i get an error here that an exp is expected
j++;
printf("Rollno:");
scanf("%d", &info1.number);// i get an error here that an exp is expected
print[j]=info1.number;// i get an error here that an exp is expected
}
}
for (j = 0;j <(20);)
{
printf("%s Name", print[j]);
j++;
printf("%d Rollno", print[j]);
}
return 0;
}
while debugging I get the following errors:
expected exp before info1
In your code, for all the usage like
scanf("%s",&info1.name);
^^^^^
are wrong, as info is an alias to a datatype, not a variable.
You have already defined a variable of that type, print, make use of that.
struct Student
{
char name[50];
int number;
};
This is fine and unexceptional
typedef struct Student info1;
info1 print[100];
This strongly suggests to me you don't know what you are doing. info1 is now an alias for struct Student. Rather an odd name. Then you are creating a buffer called print with a capacity of a hundred.
We can easily fix your compile time errors. You can assign a strudnet
struct Student astudent;
print[i] = astudent;
and you have to initialise him first
scanf("%s %d", astudent.name, &astudent.number);
but you can't assign to the type "info1". And "print" is a very poor choice for an array of students. Make it "printlist" at least.
I'm trying to display the values of my structure array, the compiler throws out the following error:
athletes.c:17: error: expected ')' before '*' token
Could someone help me out with a solution and if possible an explanation as to what I did wrong.
#include <stdio.h>
#include <string.h>
struct athlete {
char last_name[25];
char first_name [20];
int rank;
float salary;
};
int main (void) {
struct athlete players[] = {{"Lebron", "James",1,25}, {"Durant", "Kevin",3,20},{"Duncan","Tim",2,12}};
display_jock(players);
}
void display_jock(athlete *p) {
printf ("%10c%10c%10d%10.1f \n", p[0].last_name,p[0].first_name,p[0].rank, p[0].salary);
printf ("%10c%10c%10d%10.1f \n", p[1].last_name,p[1].first_name,p[1].rank, p[1].salary);
printf ("%10c%10c%10d%10.1f \n", p[2].last_name,p[2].first_name,p[2].rank, p[2].salary);
}
there are several small errors:
your code does not know the type "athlete", so your class display_jock should be defined with argument struct athlete: void display_jock(struct athlete *p)
you should forward declare that function: otherwise main doesn't know it (edit:you could also just move the display_jock function at the top of the main function)
when printing a char array, you should use %s instead of %c when using printf.
your main function should return an int (since it is declared as int main...)
this is your code fixed up:
#include <stdio.h>
#include <string.h>
struct athlete {
char last_name[25];
char first_name [20];
int rank;
float salary;
};
void display_jock(struct athlete *p);
int main (void) {
struct athlete players[] = {{"Lebron", "James",1,25}, {"Durant", "Kevin",3,20},{"Duncan","Tim",2,12}};
display_jock(players);
return 0;
}
void display_jock(struct athlete *p) {
printf ("%s%s%10d%10.1f \n", p[0].last_name,p[0].first_name,p[0].rank, p[0].salary);
printf ("%s%s%10d%10.1f \n", p[1].last_name,p[1].first_name,p[1].rank, p[1].salary);
printf ("%s%s%10d%10.1f \n", p[2].last_name,p[2].first_name,p[2].rank, p[2].salary);
}