Structures and arrays in C - c

I must create a program focusing on the use of structures.
The program will ask the user to enter the number of students for whom he will later add specific information (name, surname, average grade).
For some reason, when I start the the program, I enter the number of students i want, after that I enter the info of the first student and then the program terminates.
Here's what I've tried.
#include <stdio.h>
#include <stdlib.h>
struct student
{
char *name;
char *surname;
float *average;
};
void inputs(struct student *a, int size);
int main()
{
int size;
printf("Enter the number of students: ");
scanf("%d", &size);
struct student *data;
data = (struct student *)malloc(size*sizeof(struct student));
if(data==NULL) {
printf("Cannot allocate memory. The program will now terminate.");
return -1;
}
inputs (data, size);
return 0;
}
void inputs(struct student *a, int size)
{
int j=0;
int i;
for(i=0; i<size; i++) {
printf("Enter the name of the student number %d: ", j+1);
scanf("%s", a->name);
printf("Enter the surname of the student number %d: ", j+1);
scanf("%s", a->surname);
printf("Enter the average grade of the student number %d: ", j+1);
scanf("%f", a->average);
j++;
a++;
}
}

This code works:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char name[30];
char surname[30];
float average;
};
void inputs(struct student *a, int size);
int main()
{
int size;
int i=0; // we need i
char string[100];
printf("Enter the number of students: ");
// scanf can be tricky lets use fgets
fgets( string , 99 , stdin );
size= atoi(string);
struct student **data; // array of struct
// get memory for the pointers to pointer
data=(struct student **) calloc( size , sizeof(char *));
// now each struct need space
for( i=0 ; i<size ; i++)
{
data[i] = malloc(sizeof(struct student));
if(data[i]==NULL) {
printf("Cannot allocate memory. The program will now terminate.");
return -1;
}// end if
}// end for loop
// input each student
for( i=0 ; i<size ; i++)
inputs (data[i], i );
return 0;
}
void inputs(struct student *a, int num)
{
int j=0;
char string[100];
// scanf can be tricky lets use fgets
printf("Enter the name of the student number %d: ", num+1);
fgets(a->name, 29, stdin);
printf("Enter the surname of the student number %d: ", num+1);
fgets( a->surname , 29, stdin);
printf("Enter the average grade of the student number %d: ", num+1);
fgets( string , 99, stdin);
a->average=atof(string);
}

Related

take full name and birthday as input to struct in C

I have to write program that takes from the user, his full name, number, and his birthday and put them in structure.
then I have to sort the inputs by birthday and display them to the screen.
the problem is when I enter the first Full Name the program crash and doesn't get any full name further.
this what I have approach to so far...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct ZNAK{
char full_Name[100];
int number;
int bday[3];
};
int Compare(const void *s1, const void *s2){
struct ZNAK *e1 = (struct ZNAK *)s1;
struct ZNAK *e2 = (struct ZNAK *)s2;
int res = memcmp(e1->bday, e2->bday, 3);
if(res < 1){
return e1->bday - e2->bday;
}
}
int main(){
struct ZNAK arr[4];
for(int i=0; i<4; i++){
printf("\nenter Full Name: ");
fgets(arr[i].full_Name, 100, stdin);
printf("\nEnter Phone Number: ");
scanf("%d", &arr[i].number);
printf("Enter Birth Day: ");
scanf("%d %d %d", &arr[i].bday[0], &arr[i].bday[1], &arr[i].bday[2]);
}
qsort(arr, 4, sizeof(struct ZNAK), Compare);
char last_name[50];
printf("enter the last name: ");
scanf("%s", last_name);
for (int x=0; x<4; x++){
printf("First Name: %s\n", arr[x].full_Name);
printf("Phone Number: %d\n", arr[x].number);
printf("Birth Day: %d %d %d\n", arr[x].bday[0],arr[x].bday[1],arr[x].bday[2] );
}
}

Why does my output gives a garbage value?

I am learning structure and function and I'm trying to pass a pointer to structure as an argument. My code is to input student name,age and marks. This is my program
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student {
char name[15];
int age;
double marks;
};
void display();
int main(){
struct student *s1;
s1 =(struct student *) malloc(sizeof(struct student));
for(int i=0; i<2; i++)
{
printf("\nEnter details of student %d\n\n", i+1);
printf("Enter name: ");
scanf("%s", s1->name);
printf("Enter age: ");
scanf("%d", &s1->age);
printf("Enter marks: ");
scanf("%f", &s1->marks);
}
display(&s1);
}
void display(struct student *s1){
printf("\nName\tAge\tMarks\n");
for(int i = 0; i<2;i++ )
{
printf("Name: %s",s1->name);
printf("\nAge: %d",s1->age);
printf("\nMarks: %.2lf",s1->marks);
}
}
the code runs but it gave a wrong outputs and garbage value. What did i do wrong?
There are several syntax error. I have included documentation in the code below.
Also, you are overwriting the content of s1 everytime when you are prompting user to input. Test it with two different data for example (name: "alex", age: 19, mark: 100.0 and name: "jason", age: 22, mark: 100.0) your program will output only the later.
To solve this, I would suggest you to allocate your student pointer before looping so that you have two distinct memory for each student and you wont overwrite them in this case.
Basically, each student struct is treated as an array and instead of s1->name you will dereference them as s1[i].name
display function declaration
void display(struct student *s1);
main function
int main()
{
int i;
struct student *s1;
s1 =(struct student *) malloc(sizeof(struct student) * 2); /* 2 = number of students */
for(i=0; i<2; i++)
{
printf("\nEnter details of student %d\n\n", i+1);
printf("Enter name: ");
scanf("%s", s1[i].name);
printf("Enter age: ");
scanf("%d", &s1[i].age);
printf("Enter marks: ");
scanf("%lf", &s1[i].marks); /* Instead of %f, it should be %lf */
}
display(s1); /* Instead of &, it should be s1 */
/* Free heap memory */
for(i=0; i<2; i++)
{
free(s1);
s1 = NULL;
}
return 0;
}
Display function, you need some modification in your display function as well
void display(struct student *s1)
{
int i;
printf("\nName\tAge\tMarks\n");
for(i = 0; i<2;i++ )
{
printf("%s\t",s1[i].name);
printf("%d\t",s1[i].age);
printf("%.2f\t",s1[i].marks); /* Instead of %.2lf, it should be %.2f */
printf("\n");
}
}
Struct for student
struct student
{
char name[15];
int age;
double marks;
};

Structure Array Size in C

I am quite new to programming. Memory allocation also, still confuses me. And our professor asked us to make an array of structures wherein users will input the array size. This is to know how many entries will the users enter in the telephone directory.
Here is it so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct TelDirectory {
char name[50];
char address[100];
char tel[20];
};
int main() {
int num, add, del, prev = 0;
char ask;
*//asking for the number of entries*
printf("Number of entries: ");
scanf(" %i", &num);
struct TelDirectory *entry[num];
Input(entry, prev, num);
Display(entry, num);
//Input and Displaying
void Input(struct TelDirectory *entry[], int prev, int num) {
int i;
for (i = prev; i < num; i++) {
entry[i] = (struct TelDirectory *)malloc(sizeof(struct TelDirectory) * num);
printf("\nEnter name (last, first, middle): ");
scanf(" %[^\n]", entry[i]->name);
printf("Enter address: ");
scanf(" %[^\n]", entry[i]->address);
printf("Enter telephone number: ");
scanf(" %[^\n]", entry[i]->tel);
}
void Display(struct TelDirectory *entry[], int num) {
int i, j;
printf("%i\n", num);
struct TelDirectory *temp;
temp = (struct TelDirectory *) malloc(sizeof(struct TelDirectory) * num);
for (i = 0; i < num; i++) {
for (j = i+1; j < num; j++) {
if (strcasecmp(entry[i]->name, entry[j]->name) > 0) {
temp = entry[i];
entry[i] = entry[j];
entry[j] = temp;
}
}
}
printf("\n\t\t\t\t\tInformation\n");
printf("------------------------------------------------------------------------------------------------\n");
printf("Name\t\t\t\t\tAddress\t\t\t\t\tTelephone Number\n");
printf("------------------------------------------------------------------------------------------------\n");
for (i = 0; i < num; i++) {
printf("%-30s\t\t%-30s\t\t%-30s\n", entry[i]->name, entry[i]->address, entry[i]->tel);
}
printf("------------------------------------------------------------------------------------------------\n");
}
And, we will also be asking the users if they will update the directory through either INSERTING more entries or DELETING entries. Is it possible to change the structure array size on run time?

I'm trying to create a program with a loop that prompts the user to enter data in the array elements

I'm trying to create a program with a loop that prompts the user to enter data in the array elements. And when the user no longer can enter data, print to screen the data entered in a last in, first out order.
And this is my attempt...
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
struct Name_Age
{
char Name[10];
int Age;
};
void printMe(struct Name_Age info)
{
printf("Name: %s\n", info.Name);
printf("Age: %d\n", info.Age);
}
int main()
{
int size = 0, i = 0, j = 0;
struct Name_Age * array_ptr = (struct Name_Age*)malloc((size + 1)* sizeof(struct Name_Age));
struct Name_Age myInfo = *array_ptr;
printf("Enter size of array: ");
scanf("%d\n", size);
for (i = 0; i < size; ++i)
{
printf("Enter Name: \n");
scanf("%s\n", myInfo.Name);
printf("Enter Age: \n");
scanf("%d\n", myInfo.Age);
}
printMe(myInfo);
return 0;
};
First, scanf("%d", &size) replace scanf("%d\n", size), put &size instead of size as argument(You need an address), and put the malloc things after this line of code, because you need an exact size value before malloc. Same thing with all the scanf stuffs.
As you want to print out all your input names and ages in order, I changed your code like this:
#include <stdio.h>
#include <stdlib.h>
struct Name_Age
{
char Name[10];
int Age;
};
void printMe(struct Name_Age *infoList, int size)
{
int i;
for (i = size-1; i >= 0; --i)
{
printf("Name: %s\n", infoList[i].Name);
printf("Age: %d\n", infoList[i].Age);
printf("\n");
}
}
int main()
{
int size, i;
printf("Enter size of array: ");
scanf("%d", &size);
struct Name_Age * array_ptr = (struct Name_Age*)malloc(size* sizeof(struct Name_Age));
for (i = 0; i < size; ++i)
{
printf("Enter Name: \n");
scanf("%s", array_ptr[i].Name);
printf("Enter Age: \n");
scanf("%d", &array_ptr[i].Age);
}
printMe(array_ptr, size);
return 0;
}
Try to test and compare with your code, questions are welcome.
You can use fgets to read strings and cast the string to digits with atoi. The storage is not complete, but with my changes you can read to structs and specify the size of the loop (TODO: Save a list of structs so that you actually can print a list of structs that you have specified.)
#include <stdio.h>
#include <stdlib.h>
struct Name_Age {
char Name[10];
int Age;
};
void printMe(struct Name_Age info) {
printf("Name: %s\n", info.Name);
printf("Age: %d\n", info.Age);
}
int main() {
int size = 0, i = 0, j = 0;
struct Name_Age *array_ptr = malloc((size + 1) * sizeof(struct Name_Age));
struct Name_Age myInfo = *array_ptr;
printf("Enter size of array: ");
char tmp[10];
fgets(tmp, 10,stdin);
size = atoi(tmp);
for (i = 0; i < size; ++i) {
printf("Enter Name: ");
fgets(myInfo.Name, 10,stdin);
printf("Enter Age: ");
fgets(tmp, 10,stdin);
myInfo.Age = atoi(tmp);
}
printMe(myInfo);
return 0;
};
Test
$ ./a.out
Enter size of array: 2
Enter Name: Superman
Enter Age: 25
Enter Name: Batman
Enter Age: 27
Name: Batman
Age: 27
See also this question about fgets fgets how to read int
Obviously using fgets() is a much better approach, but making the fewest amount of changes as possible to your code and still achieving your result is the following:
#include <stdio.h>
#include <stdlib.h>
struct Name_Age
{
char Name[10];
int Age;
};
void printMe(struct Name_Age *info);
int main(void)
{
int size, i;
struct Name_Age *array_ptr;
printf("Enter size of array: ");
scanf("%d", &size);
array_ptr = malloc(size * sizeof *array_ptr);
for (i = 0; i < size; ++i)
{
printf("Enter Name: ");
scanf(" %s", array_ptr[i].Name);
printf("Enter Age: ");
scanf("%d", &array_ptr[i].Age);
printf("\n");
}
for (i = 0; i < size; ++i)
printMe(&array_ptr[i]);
return 0;
}
void printMe(struct Name_Age *info)
{
printf("Name: %s\n", info->Name);
printf("Age: %d\n", info->Age);
}
Note that passing the struct by pointer to the function should be faster, note that you don't need the myInfo struct; you can just directly modify the elements of the array. Note the space before %s in the scanf() line, this is to discard any whitespace (including \n), this is done automatically for %d so it is only necessary for your strings (technically not the first iteration of the loop, but it doesn't fail if no whitespace is found). If you have any questions about why I made the changes I did, please feel free to comment on this answer!
example of fix
//struct Name_Age myInfo = *array_ptr;//Not necessary
printf("Enter size of array: ");
scanf("%d", &size);//remove `\n`, add `&`
//To ensure after the size has been determined
struct Name_Age * array_ptr = (struct Name_Age*)malloc(size * sizeof(struct Name_Age));//Cast is not necessary in C
for (i = 0; i < size; ++i)
{
printf("Enter Name: \n");
scanf("%s", array_ptr[i].Name);//remove `\n`
printf("Enter Age: \n");
scanf("%d", &array_ptr[i].Age);//need `&`
}
for (i = 0; i < size; ++i)
printMe(array_ptr[i]);

Can't read array of structure in C

I am trying to read members of an object like in the code below.
The issue is that the code can't read the second member (car[i].model) in the array and the third one (car[i].price), only the first one (car[i].manufacturer).
#include <stdio.h>
#include <conio.h>
struct machine
{
int price;
char manufacturer[30];
char model[30];
};
int main()
{
int i = 0, n;
printf("Introduce number of cars: ");
scanf_s("%d", &n);
struct machine car[100];
for (i = 0; i < n; i++)
{
printf("Data of the car nr. %d:\n", i+1);
printf("Manufacturer: ");
scanf_s("%s", car[i].manufacturer);
printf("Model: ");
scanf_s("%s", car[i].model); printf("\n");
printf("Price: ");
scanf_s("%d", &car[i].price); printf("\n");
}
for (i = 0; i < n; i++)
{
printf("Data of the car nr. %d:\n", i + 1);
printf("Manufacturer: %s\n", car[i].manufacturer);
printf("Manufacturer: %s\n", car[i].manufacturer);
printf("Model: %s\n", car[i].model);
printf("Price %d\n", car[i].price);
}
_getch();
}
scanf_s requires the buffer size to be specified for input parameters with format %s. The buffer size includes the terminating null. Adapt your code like this:
struct machine
{
int price;
char manufacturer[30];
char model[30];
};
struct machine car[100];
....
scanf_s("%s", car[i].manufacturer, 30 );
// ^^ buffer size
....
scanf_s("%s", car[i].model, 30 );
// ^^ buffer size
....
scanf_s("%d", &car[i].price); // no buffer size

Resources