i need to input name to a variable (first name*) in a struct
with a malloc
i don't understand why to program is fail to run.
im inserting the name (for example David)
and its should gets the name and put it in temp array
and then to resize the pointer first_name*
and copy the string temp to first_name*
someone can help me understand why its doesn't work?
look for the function "ReadPerson".
typedef struct{
int day, month, year;
} Date;
typedef struct{
char *first_name, *last_name;
int id;
Date birthday;
} Person;
void ReadDate(Date *a)
{
printf("insert day, month, year\n");
scanf("%d%d%d", &a->day, &a->month,&a->year);
}
void ReadPerson(Person *b)
{
char temp_first_name[21];
char temp_last_name[21];
printf("insert first name:\n");
gets(temp_first_name);
b->first_name = (char*)malloc(strlen(temp_first_name)+1);
strcpy(b->first_name,temp_first_name);
//need to check malloc (later)
printf("insert last name:\n");
gets(temp_last_name);
b->last_name = (char*)malloc(strlen(temp_last_name)+1);
strcpy(b->last_name, temp_last_name);
//need to check malloc (later)
printf("insert id\n");
scanf("%d",&b->id);
printf("insert person's birthday:\n");
ReadDate(b);
}
Thanks.
i don't understand why to program is fail to run
Well, it's because you're trying to substitute incompatible types and decent compiler should have told you about that.
Let's look at the end of function void ReadPerson(Person *b):
{
...
ReadDate(b); // error here
}
As you can see b is of type Person * and you pass it to the function void ReadDate(Date *a) which expects a Date * type.
So it is probably a simple typo, just change to this: ReadDate(&b->birthday);.
Related
Hi friends I am practicing structures. I have these two functions one returns the structure and I copy that to the local struct in main. My second function changes those local struct members by entering different entities.
Now I have printed result after calling each function, to my surprise I notice that printed result after both the function are same. I am unable to understand what is happening here…can you guys please explain me…thanks!
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[30];
float marks;
};
struct student read_student();
void read_student_p(struct student student3);
void print_student(struct student student2);
int main()
{
struct student student1;
student1 = read_student();
print_student(student1);
read_student_p(student1);
print_student(student1);
system("pause");
return 0;
}
//This is my first function
struct student read_student()
{
struct student student2;
printf("enter student name for first function: \n");
scanf("%s",&student2.name);
printf("enter student marks for first function:\n");
scanf("%f",&student2.marks);
return student2;
}
//function to print
void print_student(struct student my_student)
{
printf("Student name in first function is : %s\n",my_student.name);
printf("Student marks in first function are: %f\n",my_student.marks);
};
//My second function
void read_student_p(struct student student3)
{
printf("enter student name for second function: \n");
scanf("%s",&student3.name);
printf("enter student marks for second function: \n");
scanf("%f",&student3.marks);
}
Do you mean to write
void read_student_p(struct student* student3)
^
{
read_student_p(&student1);
^
You need to pass a pointer to read_student_p if you want to modify the struct that you are passing. Currently it is passed by value, and the modifications are lost.
Considering the _p suffix, I expect that this was intended..
When you do this:
read_student_p(student1);
And the method looks like this:
void read_student_p(struct student student3)
{
printf("enter student name for second function: \n");
scanf("%s",&student3.name);
printf("enter student marks for second function: \n");
scanf("%f",&student3.marks);
}
Structs in C are passed by value, not by reference.
So what read_student_p does is take a copy of the struct you pass in (student1), edit the copy, and then do nothing.
One solution would be to return the changed version of the struct. Another version would be to pass a pointer to a struct, and edit the struct via pointer (so that you're editing the same copy of the struct directly).
In the second function read_student_p you were calling by value, which is to say, you defined a new struct variable tmp in the function, and copied the value of student1 to this tmp value. All the modifications that you've done were on tmp value, which wouldn't affect student1.
I was trying to pass one of the 20 "database" structs I made
here is my prototype for the function "add"
void add(struct database test);
I want to pass my database struct and for now i'll just call it "test"
Here is my database structure
struct database
{
char ID[6];
char Duration[3];
};
main()
{
char menu;
struct database employee[20]; // I make 20 employee variables
int a = 0; /*A counter I use to edit certain structs
ie a=2 "employee[a]" = "employee[2]" */
I then call the function like this:
add(employee[a]);
a++; /*Once it exits this I want it to go to the next employee
variable so I increment the counter */
The actual function looks like this:
void add(struct database test)
{
static int a = 0;
printf("\nPlease enter a 5 digit employee number: ");
scanf("%s", test[a].ID);
a++
}
while doing this I get the error:
Error E2094 Assignment.c 64: 'operator+' not implemented in type 'database' for arguments of type 'int' in function add(database)
It says the error is at
scanf("%s", test[a].ID);
Thanks in advance for any help, and I apolgise if I have formatted this wrong, still learning for to use stack overflow, so sorry!
This is what you need to do in order to get it right:
void add(struct database* test)
{
printf("\nPlease enter a 5 digit employee number: ");
scanf("%s",test->ID);
}
int main()
{
...
int a;
struct database employee[20];
for (a=0; a<sizeof(employee)/sizeof(*employee); a++)
add(&employee[a]); // or add(employee+a);
...
}
add(struct database test) declares a struct database as parameter. This is not an array, so you cannot index it.
So
test[a]
is invalid.
Also the int a inside add() is different from the int a defined in main(). Inside add() the latter a is hidden by the former a.
Also^2 you are passing to add() a copy of the array's element declared in main(). So any modifications done to test in side add() are lost when returning from add(). They won't be visible in the array declated in main().
I've recently been introduced to struct and in experimenting, I encountered a problem that I can't seem to find a solution for.
I want to create a structure with several members but I want the names for each member to be automatically generated to avoid tedious process of doing this manually. So far I have my structure and a function which I want to use to create a member.
STRUCT
struct Customers
{
char name[30];
int age;
}
PROTOTYPE
void newCustomer(Customers *Customer);
FUNCTION
void newCustomer(Customers *Customer)
{
char gender;
int age;
scanf_s("%c", 1, &gender);
scanf_s("%d", &age);
Customer->gender = gender;
Customer->age = age;
}
MAIN
int main()
{
int noOfCustomers;
int i = 0;
printf("How many customers will you be entering? : ");
scanf_s("%d", &noOfCustomers);
for(i = 0; i < noOfCustomers; i++)
{
Customers i;
newCustomer(&i);
}
return 0;
}
Basically I want to do something like this... where each member is automatically represented by an ID or number. I do understand that the above example won't work because i is being declared locally within the loop as a sructure member and not an int but I want to know if there is a method of achieving this.
Thanks in advance for any help :)
You can use static integer variable, call itoa to convert it to string, and then, increment.
Ok firstly I'll explain my assignment. For this assignment I have to use dynamic memory allocation which I am having no problems with. What I am having a problem with is figuring out the correct way to work my assignment. For my assignment I need to create a program that prompt the user to enter how many students they have then ask for the following information; Student ID, Birthdate, and Phone number. I need to use a loop to prompt the user to enter all the students information. I need to create a loop that will scan through all the student IDs and find the oldest student using their birthdate (The loop must be able scan through more then 3 students).
Here is my code, I've gotten some suggestions and even bits of code from you guys, but after implementing them I'm even more confused on what I should do. Please take a look at it and critique me.
EDIT: I also added in on the code where I'm receiving and error
Thank you.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int * studentData= NULL;
int * studentDataType=NULL;
int students;
int studentID;
int year;
int month;
int day;
long long phone;
printf("How many students are you entering records for:\n");
scanf("%d", &students);
studentData=(int*)malloc((sizeof(int)*students));
struct studentDataType
{
int studentID;
int year;
int month;
int day;
long long phone;
};
//invalid operands to binary * (have 'int' and 'int *')
studentDataType *studentData = (studentDataType*)malloc(numberOfStudents *sizeof(studentData));
for (int i = 0 ; i < students ; ++i)
{
printf("%d, %d, %d, %d, %d\n", studentID, year, month, day, phone);
}
}
You're redefining studentData
int * studentData= NULL;
then later
studentDataType *studentData = (studentDataType*)malloc(numberOfStudents * sizeof(studentData));
You should declare the studentDataType struct first (outside of main() ) then use it in your original declaration
To see the task it is better at least for the first time to write some block-scheme of what you have to do in a program. In your case:
Read data from user (each structure).
Increase array size, add new structure.
Loop 1-2 until input user finish adding new people (needs some condition here to finish).
Find necessary structure and print it.
So the first step is to read information from user. You can use scanf():
In the simplest way you can do that step-by-step for each field:
#include <stdio.h>
...
int value;
scanf("%d", &value);
...
In case of success this function should return number of items it reads (1 in your case).
For phone you should use scanf("%ld", &phone).
To resize array use function realloc() (#include :
realloc(&ptr_to_array, new_size);
Each elements of the array is a pointer to structure "student".
Next steps are similar.
The first problem is that you have variable names the same as the name of the type. Although you can have that in C to a certain extent, like:
typedef int x;
x foo (x x)
{
return x;
}
It might be a good idea not to do this for the readability purposes. So in your case you have:
int * studentData= NULL;
int * studentDataType= NULL;
which is a variable name, then you have:
struct studentDataType ...
which is a name of the type (should be used as struct studentDataType, not without struct as you do); finally
studentDataType *studentData = ...
is treated by the compiler as an operation on two variables, not a type declaration as you would expect. So your memory allocation needs to be:
struct studentDataType *studentData = malloc(numberOfStudents *sizeof(struct studentData));
Which brings a problem that you redefine studentData, which you declared in the beginning of the program, and "numberOfStudents" is not defined, probably you wanted to write "students" instead.
As for the reading data with scanf, see the previous comment.
Hi friends I am practicing structures. I have these two functions one returns the structure and I copy that to the local struct in main. My second function changes those local struct members by entering different entities.
Now I have printed result after calling each function, to my surprise I notice that printed result after both the function are same. I am unable to understand what is happening here…can you guys please explain me…thanks!
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[30];
float marks;
};
struct student read_student();
void read_student_p(struct student student3);
void print_student(struct student student2);
int main()
{
struct student student1;
student1 = read_student();
print_student(student1);
read_student_p(student1);
print_student(student1);
system("pause");
return 0;
}
//This is my first function
struct student read_student()
{
struct student student2;
printf("enter student name for first function: \n");
scanf("%s",&student2.name);
printf("enter student marks for first function:\n");
scanf("%f",&student2.marks);
return student2;
}
//function to print
void print_student(struct student my_student)
{
printf("Student name in first function is : %s\n",my_student.name);
printf("Student marks in first function are: %f\n",my_student.marks);
};
//My second function
void read_student_p(struct student student3)
{
printf("enter student name for second function: \n");
scanf("%s",&student3.name);
printf("enter student marks for second function: \n");
scanf("%f",&student3.marks);
}
Do you mean to write
void read_student_p(struct student* student3)
^
{
read_student_p(&student1);
^
You need to pass a pointer to read_student_p if you want to modify the struct that you are passing. Currently it is passed by value, and the modifications are lost.
Considering the _p suffix, I expect that this was intended..
When you do this:
read_student_p(student1);
And the method looks like this:
void read_student_p(struct student student3)
{
printf("enter student name for second function: \n");
scanf("%s",&student3.name);
printf("enter student marks for second function: \n");
scanf("%f",&student3.marks);
}
Structs in C are passed by value, not by reference.
So what read_student_p does is take a copy of the struct you pass in (student1), edit the copy, and then do nothing.
One solution would be to return the changed version of the struct. Another version would be to pass a pointer to a struct, and edit the struct via pointer (so that you're editing the same copy of the struct directly).
In the second function read_student_p you were calling by value, which is to say, you defined a new struct variable tmp in the function, and copied the value of student1 to this tmp value. All the modifications that you've done were on tmp value, which wouldn't affect student1.