I want to call a function to fill in the values of a struct in C. I have the following code but I get errors like [Error] request for member 'id' in something not a structure or union.
#include <stdio.h>
typedef struct {
int id;
float grades[3];
} student_t;
void scan_student (student_t *s) {
printf("Please give student's info:\n");
scanf("%d%f%f%f", s.id, s.grades[0], s.grades[1], s.grades[2]);
}
int main ()
{
student_t stu2;
scan_student(&stu2);
printf("Student's info are:\n");
printf("%6d %5.2f %5.2f %5.2f\n", stu2.id, stu2.grades[0], stu2.grades[1], stu2.grades[2]);
return 0;
}
s is a pointer, not a struct. That means you can't use . on it.
Instead you have to write (*s).id (dereference, then access struct member) or s->id (same thing, but shorter).
Also, scanf %d takes a pointer, so that should be &s->id, etc.
I also post an example without using pointers:
#include <stdio.h>
typedef struct {
int id;
float grades[3];
} student_t;
student_t scan_student () {
student_t s;
printf("Please give patient's info:\n");
scanf("%d%f%f%f", &s.id, &s.grades[0], &s.grades[1], &s.grades[2]);
return s;
}
int main ()
{
student_t stu2;
stu2= scan_student();
printf("Student's info are:\n");
printf("%6d %5.2f %5.2f %5.2f\n", stu2.id, stu2.grades[0], stu2.grades[1], stu2.grades[2]);
return 0;
}
Related
the console is showing the following when I am trying to run it. can someone tell me how to fix it:
arrow.c:3:23: warning: 'struct student' declared inside parameter list will not be visible outside of this definition or declaration
void printInfo(struct student s1);
^~~~~~~
arrow.c: In function 'main':
arrow.c:13:15: error: type of formal parameter 1 is incomplete
printInfo(s1);
^~
arrow.c: At top level:
arrow.c:18:6: error: conflicting types for 'printInfo'
void printInfo(struct student s1){
^~~~~~~~~
arrow.c:3:6: note: previous declaration of 'printInfo' was here
void printInfo(struct student s1);
void printInfo(struct student s1);
struct student {
int roll;
float cgpa;
char name[100];
};
int main(){
struct student s1 = {1664, 9.2, "shradha"};
printInfo(s1);
return 0;
}
void printInfo(struct student s1){
printf("student information ; \n");
printf("student.roll = %d\n", s1.roll);
printf("student.name = %s\n", s1.name);
printf("student.cgpa = %f \n ", s1.cgpa);
s1.roll = 4558;
}
formal parameter 1 is incomplete
As soon as you reference anything where its size is required to be known by the compiler, you must have shown the compiler its definition. In your case it's just a matter of reorganizing the declarations and definitions:
#include <stdio.h>
struct student {
int roll;
float cgpa;
char name[100];
};
// struct student is now defined and ok to take by value in this
// forward declaration:
void printInfo(struct student s1);
int main(void) {
struct student s1 = {1664, 9.2, "shradha"};
printInfo(s1);
}
void printInfo(struct student s1) {
printf("student information ; \n");
printf("student.roll = %d\n", s1.roll);
printf("student.name = %s\n", s1.name);
printf("student.cgpa = %f \n ", s1.cgpa);
s1.roll = 4558;
}
Note: if printInfo had taken a struct student* instead, the size of a struct student would not need to be known since the size of a pointer is already known and is the same for all objects. This would also make the assignment of roll useful.
Example:
#include <stdio.h>
struct student;
void printInfo(struct student *s1);
struct student {
int roll;
float cgpa;
char name[100];
};
int main(void) {
struct student s1 = {1664, 9.2, "shradha"};
printInfo(&s1);
printf("now visible at the call site: %d\n", s1.roll); // prints 4558
}
void printInfo(struct student *s1){
printf("student information ; \n");
printf("student.roll = %d\n", s1->roll);
printf("student.name = %s\n", s1->name);
printf("student.cgpa = %f\n", s1->cgpa);
s1->roll = 4558;
}
#include <stdio.h>
int j=0;
struct student
{
int CNE;
char Nom[20];
char Prenom[20];
char Ville[20];
float Note[3];
float Moyenne;
};
void read_struct(struct student stu)
{
stu.Moyenne=0;
printf("Nom de l'etudiant:\t ");
scanf(" %s",stu.Nom);
printf("Prenom de l'etudiant:\t ");
scanf(" %s",stu.Prenom);
printf("CNE de l'etudiant:\t ");
scanf("%d",&stu.CNE);
}
int main()
{
struct student stu[10];
read_struct(stu[0]);
read_struct(stu[1]);
printf("%s \n %s \n",stu[0].Nom,stu[1].Nom);
printf("%d \n %d",stu[0].CNE,stu[1].CNE);
}
I m getting some weird output after compiling, the input from users are not saved in struct after calling them back.( sorry for my english)
Look at how this function is defined:
void read_struct(struct student stu) {
...
}
When you call this function, it passes in a copy of the struct student, so the function does its work to fill in the copy rather than the original.
You may want to have this function take in a pointer to the struct student:
void read_struct(struct student* stu) {
/* You'll need to change things here */
}
read_student(&stu[0]);
read_student(&stu[1]);
Hope this helps!
very new to c. I wrote the following code.
typedef struct
{
char name[100];
int comp, math, phys;
int total;
} student[100];
int main(int argc, char** argv) {
int number;
do
{
printf("Enter how many students: ");
scanf("%d", &number);
if(number < 0)
{
printf("Wrong input! \n");
}
}
while(number < 0);
int i;
for(i=0; i < number; ++i)
{
printf("Student %d's name: ", i+1);
scanf("%s", student[i].name);
printf("Comp: " );
scanf("%d", &student[i].comp);
printf("Phys: " );
scanf("%d", &student[i].phys);
printf("Math: " );
scanf("%d", &student[i].math);
&student[i].total = &student[i].comp + &student[i].math + &student[i].phys;
}
printf("s%", &student[1].name);
return (EXIT_SUCCESS);
}
I keep getting error: expected expression before 'student' in all the scanf lines and the last printf line. What am I doing wrong? Very new to C so any help would be great.
Do
struct
{
char name[100];
int comp, math, phys;
int total;
} student[100];
If you want to combine the definition and identifier. You should know that student isn't a type, it's an array of structs without a name. There are alternatives to what you want to accomplish. For example:
typedef struct student
{
char name[100];
int comp, math, phys;
int total;
} student;
student students[100];
Drop the typedef keyword. You're wanting to create an array of 100 student objects, not a type name representing an array of 100 students.
Hopefully this helps you in the future:
// `Type var` is a shorter way to write `struct tag var`
// `Type` is just an alias (another name) for `struct tag`
typedef struct tag {
int x;
} Type;
// `Type100 arr` is a shorter way to write `struct tag100 arr[100]`
// `Type100` is just an alias for `struct tag100[100]`
// No, you can't do `struct tag100[100] arr`; `Type100 arr` gets around this restriction
typedef struct tag100 {
int x;
} Type100[100];
// `var` contains a single value of type `struct tagX`
struct tagX {
int x;
} var;
// `arr` is an array of 100 values of type `struct tagX100`
struct tagX100 {
int x;
} arr[100];
Trying to pass a struct to a function and browse through it. Is this the correct way to pass a struct to a function? The for loop in the function view() doesn't seem to work. Any help with that too would be appreciated.
My structs:
typedef struct {
char name[20];
employee *list_employees;
int empl_count;
} agency;
typedef struct {
char name[30];
int age;
} employee;
Important pars of the code:
int main()
{
//...
int nmbr_agencies;
agency *list_agencies = malloc(sizeof(agency) * nmbr_agencies);
view(&list_agencies, &nmbr_agencies);
}
void view(agence *ListA[], int *nmbr)
{
int i=0;
for (i = 0; i < *nmbr; i++){
printf("name of agency: %s\n", ListeA[i]->name);
printf("number of employees\n, ListeA[i]->empl_count);
}
}
No.
You should just pass a single array if that is what you have, not pretend (in the call) that you have an array of pointers which you don't have.
Make the function:
void view(const agency *list, size_t number);
and call it like so:
view(list_agencies, nmbr_agencies);
Then inside the function, do direct accesses:
printf("name of agency: %s\n", list[i].name);
since you don't have an array of pointers.
my questions here always seem to be about using functions. It still confuses me! In this textbook exercise i am asked to pass a structure by value, then adjust it and pass by reference. Initially I designed the code to have everything done in main. Now I am passing by value. So I added the new function, and I figured I passed the structure correctly but I am getting an error at line
void function1(struct Inventory inv){ that tells me parameter 1 (inv) has incomplete type. please help!
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void function1(struct Inventory inv);
struct Inventory{
char name[20];
int number;
float price;
float total;
}
void main(){
items;
void function1(items);
float total = items.number*items.price;
printf("Item\tNumber\tPrice\tTotal\tAddress of number\n");
printf("%s\t%d\t%.2f\t%.2f\t%X\n\n",items.name,items.number,items.price,total,&items.number);
getch();
}
void function1(struct Inventory inv) {
printf("Enter the name of the item: ");
scanf("%s", inv.name);
printf("Enter the number of items: ");
scanf("%d", &inv.number);
printf("Enter the price of each item: ");
scanf("%f", &inv.price);
}
You have to define your struct BEFORE you use it in your function prototype.
struct Inventory{
char name[20];
int number;
float price;
float total;
}items;
void function1(struct Inventory inv);