3
#include <stdio.h>

typedef struct{
    unsigned int _1_:7;
    unsigned int _2_:7;
    unsigned int _3_:7;
    unsigned int _4_:7;
    unsigned int _5_:7;
} grades;

// ---other part of code---

grades student;
int main(){
    scanf("%u\n", student._1_)
    // all the others...
    return 0;
}

I don't know the way to do it. I tried to put & in front of student._1_ but it didn't work. If someone could show me how to do it, not only with bitfields but also with normal structs and enums.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    Why are you using bit-fields at all? It would be far more sensible to use `unsigned char _1_;` etc. I debate the wisdom of names of the form `_1_` — I would not use them. And it looks like you really need `unsigned char grades[5];` — instead of structure at all. – Jonathan Leffler Aug 17 '21 at 16:02
  • 1
    Putting a `'\n"` at the end of `scanf()` will cause issues. Use `scanf("%u", &some_tmp_unsigned)`. – chux - Reinstate Monica Aug 17 '21 at 16:02
  • 2
    As @chux-ReinstateMonica says, [What is the effect of trailing white space in a `scanf()` format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string) shows that the trailing newline in the `scanf()` format string is a very bad idea. – Jonathan Leffler Aug 17 '21 at 16:04
  • I used bitfields because I had to store a number up to 100, thought it would be more efficient to use bitfields (and wanted to try what I've recently learned), and yes ,'_1_' is ugly sorry:° – XxCharbonChainsxX Aug 17 '21 at 16:10
  • @XxCharbonChainsxX `int main(){` is wrong. It has to be `int main(void){` if no parameters. – 0___________ Aug 17 '21 at 16:21
  • @0___________: You are overstating your case. Since the C11 standard shows the use of `int main() { … }` (see [§ The `sizeof` and `_Alignof` operators ¶8 Example 3](http://port70.net/~nsz/c/c11/n1570.html#6.5.3.4p8) for one of several such examples), it is not necessary to use `int main(void)`. It is definitely preferred, but the paramerless version is allowed and not wrong — unless you compile with GCC and options such as `-Wstrict-prototypes` or `-Wold-style-definition` and `-Werror` (which I do). – Jonathan Leffler Aug 17 '21 at 16:27
  • @JonathanLeffler I would rather suspect an error in the examples :) 5.1.2.2.1 Program startup. Old-style (non-prototype) declarations and definitions of functions other than `main` are still legal, but they're officially obsolescent. – 0___________ Aug 17 '21 at 16:36
  • 1
    Old-style function definitions are legal, so they are not an error, @0___________. They are undesirable, but they are not an error. And I would not accept your claim of "error in the examples" (though I do recognize that examples are not normative). However, I fear that we need to agree that we disagree. – Jonathan Leffler Aug 17 '21 at 16:41
  • 1
    @JonathanLeffler . IMO 5.1.2.2.1 Program startup is quite precise – 0___________ Aug 17 '21 at 16:46

4 Answers4

4

Scanning to a temporary variable should help.

unsigned int tmp = 0;
scanf("%u\n", &tmp);
student._1_ = tmp;

BTW The error message should have informed you about the error reason.

273K
  • 29,503
  • 10
  • 41
  • 64
4

Bitfields cannot have their address taken, and therefore you can't use scanf to write directly to them. You need to write to a temporary, then copy the value to the bitfield.

unsigned tmp;
scanf("%u", &tmp);
student._1_ = tmp;
dbush
  • 205,898
  • 23
  • 218
  • 273
0

final program I wrote, but now I have a ERROR with j & i variable in person.grades[1]

#include <stdio.h>


float average;
int i;
int j;

typedef struct{
    char* name_and_year[25];
    short grades[6];
} student;

student person;

void average_calculator(student *x){

    for(j = 0; j > 6; j++)
        average = (x->grades[0] + x->grades[1] + x->grades[2] + x->grades[3] + x->grades[4])/5.0;
    if(average >= 50){
        puts("You passed the year congratulations");
        x->grades[5] = 1;}
    else{
        puts("You didn't pass the year");
        x->grades[5] = 0;}
    printf("Your average is %.2f\n",average);
} 

void student_reader(){
    puts("Enter your full name & your year");
    scanf("%24[^\n]", person.name_and_year);
    puts("what were your grades?\n");
    for(i = 0; i > 6; i++)
        scanf("%hi", &(person.grades[i]));
}

int main(){

    student_reader();
    average_calculator(&person);

    return 0;
}
0

FINAL code for the readers, thx everybody

#include <stdio.h>

float average; short i;

typedef struct{
    char* name_and_year[25];
    short grades[6]; } student;

student person;

void average_calculator(student *x){

    for(i = 0; i < 5; i++){
        average += person.grades[i]/5;
    }
    if(average >= 50){
        puts("You passed the year congratulations");
        x->grades[5] = 1;}
    else{
        puts("You didn't pass the year");
        x->grades[5] = 0;}
    printf("Your average is %.2f\n",average); } 

void student_reader(){
    puts("Enter your full name & your year");
    scanf("%24[^\n]", person.name_and_year);
    puts("what were your grades?\n");
    for(i = 0; i < 5; i++){
        scanf("%hi", &(person.grades[i]));
    } }

int main(){

    student_reader();
    average_calculator(&person);
    return 0; }