How can I pass the pointer to the structure to another function?
sample:
#include<stdio.h>
struct customer{
char name[50];
int number;
};
void input(){
struct customer details;
printf("enter your name: ");
scanf("%s",details.name);
details.number++;
printf("%d\t%s",details.number,details.name);
}
int main(){
struct customer details;
input();
//pcust->number++; how should I declare this?
printf("%d\t%s",details.number,details.name);
}
how do you automatically assign a number in a struct where I can call its value in other functions?
suppose I have this struct
struct customer{
char name[50];
int number;
};
and request input for the user
void input(){
struct customer details;
printf("enter your name");
scanf("%s",details.name);
printf("enter your number");
scanf("%d",&details.number);
}
instead of asking them to enter their number in the function to call it by value, how can I assign it automatically, so I can call it by its value? The possible output I need is like this so that in the next function again, I can call by its value again.
1 customer1
2 customer2
....
#include <stdio.h>
struct customer
{
char name[50];
int number;
};
void input(struct customer *pDetails)
{
printf("enter your name: ");
scanf("%s", pDetails->name);
pDetails->number++;
printf("%d\t%s \n", pDetails->number, pDetails->name);
}
int main()
{
struct customer details = {};
input(&details);
printf("%d\t%s \n", details.number, details.name);
}
Your main problem is that you do not use any function parameters.
You need to have some kind of search (for example if you want to modify a customer record
Use arrays to store multiple data.
Example:
struct customer
{
char name[50];
int somedata;
};
struct database
{
size_t size;
struct customer customers[];
};
struct customer *findCustomer(const struct database *db, const char *name)
{
struct customer *result = NULL;
if(db)
{
for(size_t i = 0; i < db -> size; i++)
if(!strcmp(db -> customers[i].name, name))
{
result = (struct customer *)&db -> customers[i];
break;
}
}
return result;
}
struct database *addCustomer(struct database *db, const char *name)
{
size_t newSize = db ? db -> size + 1 : 1;
db = realloc(db, newSize * sizeof(db -> customers[0]) + sizeof(*db));
if(db)
{
strcpy(db -> customers[db -> size].name, name);
}
return db;
}
Related
i am receiving a default char array (first/last) in guest_init (and i need to initialize the values such that guest have default values ) is my following code correct? as when i run this g->first_name is always being assigned garbage. need some help.
struct guest {
char last_name[30];
char first_name[30];
};
struct auditorium_seating {
struct guest **seating;
};
void guest_init_default(struct guest *g)
{
*g->first_name = "???";
*g->last_name = "???";
}
void guest_init(struct guest *g, char *info)
{
strcpy(g->first_name, strtok(info, "/"));
strcpy(g->last_name, strtok(NULL, "\0"));
}
void auditorium_seating_init(int rowNum, int columnNum, struct auditorium_seating *a)
{
a->seating=malloc((sizeof(a->seating[rowNum][columnNum])));
char string_arr[30]="aaa/bbb";
for (int i = 0; i<rowNum; i++)
{
for (int j = 0; j<columnNum; j++)
{
//guest_init_default(a->seating);
guest_init(a->seating,string_arr);
}
}
}
auditorium_seating_init being called from main.
void main() {
struct auditorium_seating auditorium_seating;
struct guest temp_guest;
int row, col, rowNum, columnNum;
char guest_info[30];
printf("Please enter a number of rows for an auditorium seating.");
scanf_s("%d", &rowNum);
printf("Please enter a number of columns for an auditorium seating.");
scanf_s("%d", &columnNum);
auditorium_seating_init(rowNum, columnNum, &auditorium_seating);
printf("Please enter a guest information or enter \"Q\" to quit.");
}
Enable your compiler warnings: *g->first_name = "???"; is wrong.
And strtok(NULL, "\0")); is wrong too.
You probably want this:
#include <string.h>
#include <stdio.h>
struct guest {
char last_name[30];
char first_name[30];
};
void guest_init(struct guest *g, char *info)
{
strcpy(g->first_name, strtok(info, "/"));
strcpy(g->last_name, strtok(NULL, "/"));
}
int main()
{
struct guest g;
char info[] = "Foo/Bar";
guest_init(&g, info);
printf("Last Name = %s\n", g.last_name);
printf("First Name = %s\n", g.first_name);
}
There may be more errors related to struct auditorium_seating *a, but you didn't post that code.
No, you must copy data to your structure, as you allocated memory inside it.
Read compiler errors and use it to fix your program.
I am trying to create an array of structures containing some basic information. My code is as follows:
typedef struct {
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip;
char phone[15];
int accountId;
} Customer;
int main(void) {
int i, customer_number, _zip, _accountId;
struct Customer customer_list[9];
char _firstName[30], _lastName[30], _street[35], _city[20], _state[3], _phone[15];
for (i = 0; i < 10; i++) {
customer_number = 0;
printf("Enter data for customer %d: \n", customer_number);
printf("Enter First Last Phone: ");
scanf("%s%s%s", &_firstName, &_lastName, &_phone);
printf("\nEnter Address (Street City State ZIP): ");
scanf("%s%s%s%d", &_street, &_city, &_state, &_zip);
Customer customer_list[i] = {[i].firstName = _firstName}
}
return 0;
}
It seems like this should work, however I am getting an error saying " i must have constant value" Can anyone push me in the right direction? Thank you!
Customer customer_list[i] = {[i].firstName = _firstName}
This line needs to be
strcpy(customer_list[i].firstName, _firstName);
EDIT: assignment should be done by strcpy()
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.
goal: ask user for the number of agencies, create said agencies, and for each agency ask for the number of employees, and create those employees.
part of this will require, I think, nested structure, something like
typedef struct agence agence;
struct agence
{
char nom[20];
int nmbrEmp;
struct employe
{
char mat[20];
int nmbrEnf;
int ANC;
double SB;
double RCNSS;
}
};
is this n the right path, and how do you proceed to create the number of agencies/employees once the users gives you the number required of each.
C unlike C++ cannot have nested types, you will have to declare them separately.
struct employe
{
char mat[20];
int nmbrEnf;
int ANC;
double SB;
double RCNSS;
};
struct agence
{
char nom[20];
int nmbrEmp;
struct employe * employees; // pointer to an array of employees
};
Then use dynamic memory and populate them:
struct agence * agencies;
size_t num_agencies = 100;
agencies = calloc(sizeof(*agencies), num_agencies);
for (/* read egancies somehow */) {
agencies[i].nmbrEmp = number_employees;
agencies[i].employees = calloc(sizeof(agencies[i].employees[0]), number_employees);
// write agencies[i].employees[j] ...
}
Consider this Example :
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date
{
int date;
int month;
int year;
}doj;
}emp1;
Accessing Nested Members :
Accessing Month Field : emp1.doj.month
Accessing day Field : emp1.doj.day
Accessing year Field : emp1.doj.year
Here is the Code pad which will demonstrate the above example
enter link description here
In c, you cannot have unnamed nested structures [otherwise, nested types]. They have to be a named structure. You need to write something like
typedef struct agency
{
char nom[20];
int nmbrEmp;
struct employee
{
char mat[20];
int nmbrEnf;
int ANC;
double SB;
double RCNSS;
} emp [20];
}agency;
However, here you are limited to the static input of a size 20, that means, you cannot have more than 20 employee records. To liberate from this limitation , the best approach is to use a pointer to an employee structure, an in runtime, allocate memory based on the number of the employee given by the user.
Here's an example you might consider. This is a template of what you have to do, you are left with manipulating employees, agencies name and so on. (Note that there's no error checking, which is bad.)
#include <stdio.h>
#include <stdlib.h>
/* TODO: modify members */
typedef struct {
int id;
int salary;
} employee;
/* TODO: modify members */
typedef struct {
char name[20];
employee* emps;
int emps_count;
} agency;
/* return sum of id and salary for first employee from agency */
int dumb_calc(agency ag) {
return ag.emps[0].id + ag.emps[0].salary;
}
int main(void)
{
int num_ag;
int num_emps;
int i, j;
printf("enter num of agencies:\n");
scanf("%d", &num_ag);
agency* agencies = malloc(sizeof(agency) * num_ag);
for (i = 0; i < num_ag; ++i) {
/* TODO: modify single agency name */
sprintf(agencies[i].name, "agency %d", i+1);
printf("enter num of employees for agency %d\n", i+1);
scanf("%d", &num_emps);
agencies[i].emps = malloc(sizeof(employee) * num_emps);
agencies[i].emps_count = num_emps;
for (j = 0; j < num_emps; ++j) {
/* TODO: modify single employee */
agencies[i].emps[j].id = j+1;
agencies[i].emps[j].salary = 1000*(j+1);
}
}
/* TODO: change printing style */
for (i = 0; i < num_ag; ++i) {
printf("agency name: %s\n", agencies[i].name);
printf("num of employees: %d\n", agencies[i].emps_count);
/* result will always be the same */
printf("sum of id and salary for 1st emp: %d\n", dumb_calc(agencies[i]));
}
/* remember to free what you've alloc'd */
for (i = 0; i < num_ag; ++i) {
free(agencies[i].emps);
}
free(agencies);
return 0;
}