'(struct student *)&st' is a pointer; did you mean to use '->'?| - c

#include<stdio.h>
#include<string.h>
#define MAX 50
struct student
{
int srn;
char stu_name[30];
char course[18];
char addr[50];
};
int main()
{
struct student st[MAX];
int i;
for (i = 0; i < MAX; i++)
{
printf("\nEnter name of the student %d : ", st[i].srn=i+1);
scanf("%s", st[i].stu_name);
printf("\nEnter course of the student %d : ", i+1);
scanf("%s", st[i].course);
printf("\nEnter address of the student %d : ", i+1);
scanf("%s", st[i].addr);
}
for (i = 0; i < MAX; i++)
{
printf("\nname of student %d is %s", i+1, st[i].stu_name);
printf("\ncourse is %s", st[i].course);
printf("\naddr is %s", st[i].addr);
}
return 0;
}
i wrote this code for a school project but codeblocks keeps giving me this error.Anyone know the solution?
main.c|21|error: '(struct student *)&st' is a pointer; did you mean to use '->'?|

You cannot assign values in the same line where you are printing them, thats the whole problem
try this instead might work
st[i+1].srn
instead of
st[i].srn=i+1
The end code should look something like this
int main()
{
struct student st[MAX];
int i;
for (i = 0; i < MAX; i++)
{
st[i].srn = i+1;
printf("\nEnter name of the student %d : ", st[i].srn);
scanf("%s", st[i].stu_name);
printf("\nEnter course of the student %d : ", i+1);
scanf("%s", st[i].course);
printf("\nEnter address of the student %d : ", i+1);
scanf("%s", st[i].addr);
}
for (i = 0; i < MAX; i++)
{
printf("\nname of student %d is %s", i+1, st[i].stu_name);
printf("\nname of student %d is %s", st[i].srn, st[i].stu_name);
printf("\ncourse is %s", st[i].course);
printf("\naddr is %s", st[i].addr);
}
return 0;
}

Related

I am getting segmentation fault for this code in visual code arm64?

I am getting segmentation error?? PLS HElP
#include <stdio.h>
#include <string.h>
void display(char n2[], int x2[], char c1[], int p);
void display(char n2[], int x2[], char c1[], int p)
{
printf("Student Name : %s \n", n2[p]);
printf("Student Roll No : %d \n", x2[p]);
printf("Student Class : %s \n ", c1[p]);
}
int main()
{
char n[50], c[5], n1;
int y, x[8], x1;
int p1 = 0;
printf("Enter the number of students: \n");
scanf("%d", &y);
fflush(stdin);
for (int i = 0; i < y; i++)
{
fflush(stdin);
printf("Enter the Student Name : \n");
scanf("%s", n[i]);
fflush(stdin);
printf("Enter the Student Class : \n");
scanf(" %s", c[i]);
fflush(stdin);
printf("Enter the Student Roll No : \n");
scanf(" %d", &x[i]);
fflush(stdin);
}
fflush(stdin);
printf("Enter the Student Name and Roll Number :\n");
scanf("%s %d", &n1, &x1);
for (int i = 0; i < y; i++)
{
if ((n[i] == n1) && (x[i] == x1))
{
p1 = i;
}
else
{
printf("No Such Entry!!");
}
}
display(n, x, c, p1);
return 0;
}
The error occurs here:
scanf("%s",c[i]);
You are trying to store a string into a char (n[i]).
You should define n and the others as an array of strings instead of a string, for example:
char n[50][ 50 ], c[50][50], n1[50];
Also, you can't compare strings like you do on this line:
if ((n[i]== n1) && (x[i] == x1))
Use strcmp instead to compare strings.

Why can't I print all the character types of variables in array?

#include <stdio.h>
#include <conio.h>
int main() {
char name[20];
int age[20], i, size;
printf("Enter number of students: ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("\nEnter Student # %d name: ", i+1 );
scanf("%s", &name[i]);
printf("\nEnter Student # %d age: ", i+1 );
scanf ("\n%d", &age[i]);
}
printf("---");
printf("\n Student Information");
for (i = 0; i < size; i++)
{
printf("\nStudent # %d :\n", i+1);
printf("Name: %c", name[i]); //how do i print the array here?
printf("\nAge: %d", age[i]);
}
return 0;
}
For some reason, I can't display the whole characters that are inputted, and instead, my program produces nothing. What's causing this and how can I fix it?
starboy_b already answered your question, but I want to highlight security issues in your code:
Keep in mind that using scanf to get input like you do is introducing dangerous vulnerabilities to your program.
Why? :
scanf does not know how much allocated space your variables have and does not prevent the user from entering more characters than your buffer can contain. This lead to buffer overflow and can be used to run attacker code.
Your better off e.g. using readline or fgets to get input from user.
#include <stdio.h>
#include <conio.h>
int main() {
char name[20][100];
int age[20], i, size;
printf("Enter number of students: ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("\nEnter Student # %d name: ", i+1 );
scanf("%s", &name[i]);
printf("\nEnter Student # %d age: ", i+1 );
scanf ("\n%d", &age[i]);
}
printf("---");
printf("\n Student Information");
for (i = 0; i < size; i++)
{
printf("\nStudent # %d :\n", i+1);
printf("Name: %s", name[i]); //how do i print the array here?
printf("\nAge: %d", age[i]);
}
return 0;

Why when I call the struct it only shows the last input I give

I tried to make a program in C to list a province and its city, but when I call the struct it only shows the last data I input.
#include <stdio.h>
int main()
{
FILE *f_structure;
int n, i, j, k;
k=3;
struct
{
char province[30], citya [30], cityb [30], cityc [30];
} list;
f_structure = fopen("city List.dat", "wb");
printf("Please input the amount of province: ");
scanf("%d", &n); getchar();
for (i=1; i<=n ;i++)
{
printf("Province : ");
gets(list.province);
printf("city : ");
scanf("%s", &list.citya);
printf("city : ");
scanf("%s", &list.cityb);
printf("city : ");
scanf("%s", &list.cityc);
getchar();
fwrite(&list, sizeof(list), 1, f_structure);
}
f_structure = fopen("city List.dat", "rb");
for(j=0;j<=k-1;j++)
{
printf("Province no %d : %s \n city : %s \n city : %s \n city : %s \n",j+1, list.province, list.citya, list.cityb, list.cityc);
}
fclose(f_structure);
return 0;
}

Multipurpose Payroll System C

I have a program using unions and structures that calculates pay for an hourly worker or a salary worker. For an hourly worker the hours they work must be less than 80. I implemented that in my code but I get this warning and do not know how to fix it.
Below is my code:
#include <stdio.h>
#include <ctype.h>
#define MAXSIZE 4000
union Employee { //union
struct HourlyPaid { //Struct for Hourly Workers
char name[20];
char Gender[20];
int HoursWorked;
float HourlyRate;
} HourlyPaid;
struct Salary { //struct for Salaried workers
char name[50];
int age;
float salary;
float bonus;
} Salary;
} Employee;
int main(void)
{
int i = 0;
int n = 0;
char option[2];
char s[MAXSIZE];
while(1)
{
puts("\nMULTIPURPOSE PAYROLL SYSTEM");
puts("Please Select an Option to Continue:");
puts("Option A: Calculating Pay for an HOURLY worker");
puts("Option B: Calculating Pay for an SALARIED worker\n");
puts("PRESS ANY OTHER BUTTON TO EXIT\n");
fgets(option, MAXSIZE, stdin);
//putchar(option);
if((option[0]=='a')||(option[0]=='A')) //Condition for Hourly Workers
{
puts("Please enter how many salaries you wish to input:\t");
scanf("%d", &n);
struct HourlyPaid x[n];
for(i = 0; i < n; i++)
{
fputs("Enter Name: ", stdout);
scanf("%s", x[i].name);
fputs("Enter Gender: ", stdout);
scanf("%s", x[i].Gender);
fputs("Enter Hours Worked: ", stdout);
scanf("%d", &x[i].HoursWorked);
fputs("Enter Hourly Rate: ", stdout);
scanf("%f", &x[i].HourlyRate);
}
if (&x[i].HoursWorked > 80 )
{
puts ("Sorry please enter a value less than 80 for hours worked");
}
else
for (i = 0; i < n; i++)
{
printf("\n\tCalculated Salary #%d\n", (i+1));
puts("\t===============================");
printf("\n\tName %d.............%s \n", (i+1),x[i].name);
printf("\tGender %d...........%s \n", (i+1), x[i].Gender);
printf("\tHours Worked %d.....%d \n", (i+1), x[i].HoursWorked);
printf("\tHourly Rate %d......%.2f \n", (i+1), x[i].HourlyRate);
printf("\n\tTotal Salary %d.....%.2f \n", (i+1), (x[i].HourlyRate*x[i].HoursWorked));
puts("\t===============================");
} //End of for
} //End of if
else if((option[0]=='b')||(option[0]=='B')) //Condition for Salaried Workers
{
puts("Please enter how many salaries you wish to input:\t");
scanf("%d", &n);
struct Salary x[n];
//int i;
for(i = 0; i < n; i++)
{
printf("Enter Name %d: ", i+1);
scanf("%s", x[i].name);
printf("Enter Age %d: ", i+1);
scanf("%d", &x[i].age);
printf("Enter Salary %d: ", i+1);
scanf("%f", &x[i].salary);
printf("Enter Bonus %d: ", i+1);
scanf("%f", &x[i].bonus);
}
for (i = 0; i < n; i++)
{
printf("\n\tCalculated Salary #%d\n", (i+1));
puts("\t============================");
printf("\n\tName %d is..........%s \n", (i+1),x[i].name);
printf("\tAge %d is...........%d \n", (i+1), x[i].age);
printf("\tSalary %d is........%.2f \n", (i+1), x[i].salary);
printf("\tBonus %d is.........%.2f \n", (i+1), x[i].bonus);
printf("\n\tTotal Pay %d is.....%.2f \n", (i+1), (x[i].bonus+x[i].salary));
puts("\t============================");
}
} //End of else if
else
{
return 0;
}
fgets(option, MAXSIZE, stdin);
} //End of While
} //End of Main
Try
for(i = 0; i < n; ) // note no i++ here
{
fputs("Enter Name: ", stdout);
scanf("%s", x[i].name);
fputs("Enter Gender: ", stdout);
scanf("%s", x[i].Gender);
fputs("Enter Hours Worked: ", stdout);
scanf("%d", &x[i].HoursWorked);
fputs("Enter Hourly Rate: ", stdout);
scanf("%f", &x[i].HourlyRate);
if (x[i].HoursWorked > 80 )
{
puts ("Sorry please enter a value less than 80 for hours worked");
} else {
i++; //note increment i only if the value is good
}
}
This is not an ideal solution. you may need to enter all details again if HoursWorked > 80
The reason why it does not work is not related to the presence or the absene of the &sign, but because your logic is a bit awkward.
You want this:
for (i = 0; i < n; i++)
{
fputs("Enter Name: ", stdout);
scanf("%s", x[i].name);
fputs("Enter Gender: ", stdout);
scanf("%s", x[i].Gender);
fputs("Enter Hours Worked: ", stdout);
scanf("%d", &x[i].HoursWorked);
do
{
fputs("Enter Hourly Rate: ", stdout);
scanf("%f", &x[i].HourlyRate);
if (x[i].HoursWorked > 80)
{
puts("Sorry please enter a value less than 80 for hours worked\n");
continue;
}
} while (0);
}
If the user enters a number greater then 80, the program simply displays "Sorry, please..." and then asks the user again to enter the number of hours worked.

program crashing using 2D dynamic string array in c

I am having a problem with this code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
int num=0;
printf("Enter number of entries: ");
scanf_s("%d", &num);
char **firstname=NULL;
char **lastname=NULL;
float *score=NULL;
int i;
firstname = malloc(num * (sizeof(char*)));
for (i = 0; i < num; i++)
{
firstname[i] = malloc(21);
}
lastname = malloc(num * (sizeof(char*)));
for (i = 0; i < num; i++)
{
lastname[i] = malloc(21);
}
score = ((float*)malloc(num* (sizeof(float))));
for (i = 1; i <= num; i++)
{
printf("Enter the first name of entry %d: \n", i);
scanf_s("%s", firstname[i], 21);
printf("Enter the last name of entry %d: \n", i);
scanf_s("%s", lastname[i], 21);
printf("Enter the score of entry %d: \n", i);
scanf_s("%f", score[i]);
}
for (i = 0; i < num; ++i)
{
printf("%s, %s, %f", firstname[i], lastname[i], score[i]);
}
return 0;
}
I have edited the code with the suggested changes, and now it prints random characters and a random value for the first and last name, as well as the score. the code also crashes if there is more than one iteration of input (after asking for the score of the first entry, it crashes before asking for the first name of the second entry.)

Resources