1

Le us consider the following piece of code in C:

#include <stdio.h>
#define SCORES 3

typedef struct struct_student{
       char name[16];
       float scores[SCORES];
       float average;
} STUDENT;

int main(){
    STUDENT student, *p;
    int i;
    p = &student;
    printf("Name:\n");
    gets((*p).name);
    for(i=0;i<SCORES;i++){
        printf("score:\n");
        scanf("%f", &(*p).scores[i] );
        (*p).average = (*p).average + (*p).scores[i];
    }
    (*p).average = (*p).average/SCORES ;
    printf("%s has average %f\n",student.name, student.average);
    return 0;
}

It is working well. But I think that there is another way to specify the scanf:

scanf("%f", &(*p).scores[i] );

I mean. This seems to be overcomplicated. I would like to know if there is another way to do this using the "dot notation". That is, I know that there is the "arrow notation" that is much easier. But I think that even using the "dot notation" there is some better way to do this. Notice that I'm trying to understand how to use the "dot notation", so I'm not interested in solutions with the "arrow notation".

  • 7
    `&((*p).scores[i])` ==> `p->scores+i` . More cryptic code doesn't make more better code. If, for some bizarre reason, want to avoid `->`, then `(*p).scores+i`. Unrelated, get rid of `gets` usage. It's so evil it was *removed* from the standard library with good reason. – WhozCraig May 27 '19 at 16:29
  • 4
    Welcome to Stack Overflow! [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/2173917) – Sourav Ghosh May 27 '19 at 16:36
  • 5
    Unrelated to your problem, but ***never ever*** use the `gets` function. It's [a dangerous function](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) and have even been removed from the C specification. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude May 27 '19 at 16:36
  • ...which will now allow you to enter a name containing a space such as `Da Silva` but will need the newline [removing](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221). – Weather Vane May 27 '19 at 16:40
  • @WhozCraig, I know that using the arrow is better...But I'm trying to understand the "dot notation". I won't use this for implementing programs. It's just for understanding. – kolmogorovyuri May 27 '19 at 17:05
  • Ok...So, There is no better way to do that scanf using the "dot notation"? – kolmogorovyuri May 27 '19 at 17:06
  • 2
    There is a better way, but you made the dot notation harder by introducing the unnecessary pointer. `scanf("%f", &student.scores[i] );` – Weather Vane May 27 '19 at 17:28
  • If you have a structure pointer, use the arrow notation (there isn't a better way to deal with it using the dot notation because you'd have to use `(*ptr).member` instead of `ptr->member`). If you have an actual structure, use the dot notation (use `var.member`; it isn't better to try and force that into using the arrow notation, though it can be done: `(&var)->member`). If you have an array of structures, use `array[i].member` rather than `(*(array + i)).member` or `(array + i)->member`. Don't fight the language; go with the flow. – Jonathan Leffler May 27 '19 at 19:21

0 Answers0