2

screenshot

#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 '->'?|

SSpoke
  • 5,239
  • 8
  • 66
  • 112
  • The error message does not seem to match the code shown. There is no `&st` nor `&stun` in the code. Please check that and post the **exact** code. – kaylum Dec 13 '20 at 05:36
  • `st.srn=i+1` should be `st[i].srn=i+1`. Not sure if that's your bug though. – Nate Eldredge Dec 13 '20 at 05:38
  • And for the next question you're going to have as soon as you get your code to compile and run: https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – Nate Eldredge Dec 13 '20 at 05:39
  • @kaylum please refer to the screenshot. i have run the code multiple times and yet it keeps giving me the same error. even though &st is not in the code. – Mello kingTV Dec 13 '20 at 05:47
  • 1
    You are probably not compiling what you think you are. Copy the code to another file and remove the content from the original file. Then try to compile again to see if you still get the same error. – kaylum Dec 13 '20 at 05:52
  • The code you posted doesn't result in the error you claim it does. The answer addresses the problem in the code you posted, which is the best we can do. – ikegami Dec 13 '20 at 05:53

1 Answers1

1

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;
}
SSpoke
  • 5,239
  • 8
  • 66
  • 112