fancy b
fancy b

Reputation: 3

Arrays of structures

#include<stdio.h>
#include<conio.h>
typedef struct Student
{
char nume[20],situatie[11];
int grupa,nr_credite;
} S;


void fct()
{ 
int n,i,c;
S st[100];

scanf("%d %d", &n, &c);

for(i=0;i<n;i++)
    scanf("%s %d %d", &st[i].nume, &st[i].grupa, &st[i].nr_credite);

for(i=0;i<n;i++)
    if (st[i].nr_credite>=n) st[i].situatie="Promovat";
                else st[i].situatie="Nepromovat";
}

int main()
{
fct();
return 0;
}

For the given code, this is the error I am getting.

Error: C:\Users\Rebekah\Downloads\e\main.c|20|error: assignment to expression with array type|

What am I missing here?

Upvotes: 0

Views: 57

Answers (1)

user2736738
user2736738

Reputation: 30936

st[i].situatie="Promovat";

Array is not modifiable lvalue. So you can't do that. Use strcpy instead (when you know the size is big enough to hold the copied string).

strcpy(st[i].situatie,"Promovat");

Also check the return value of scanf.

if( scanf("%d %d", &n, &c) != 2 ){
    fprintf(stderr,"%s\n","Error in input");
    exit(EXIT_FAILURE);
}

Also another thing that you did wrong

scanf("%s %d %d", &st[i].nume, &st[i].grupa, &st[i].nr_credite);
                 ^^^

It will be

scanf("%s %d %d", st[i].nume, &st[i].grupa, &st[i].nr_credite);

st[i].name decays into pointer to char but the &st[i].name is of type char(*)[]. scanf's %s format specifier expects char*.

Compile the code like this gcc -Wall -Werror progname.c. Try to get rid of all warnings and errors.

Upvotes: 1

Related Questions