0

Here I wrote some code with a pointer to a structure. I put in some typedef but I don't know how to use it with my pointer structure. I can't find any help on the internet. Always typedef structure, or pointer structure, but not with these 3 involved.

#include<stdio.h>
#include<stdlib.h>

typedef struct student{
    char NAME[20];
    int ID;
    float GRADE;
    char INSTRUCTOR[20];
}student;


int main()
{
    struct student Raf = {"Rafael Sunga", 1775, 1.35, "Kenneth Auxillos"};
    struct student *ptr_Raf; //declaring ptr to a structure
    ptr_Raf = &Raf; //asigning address of variable with & 
    
    printf("Student Name: %s\n", ptr_Raf->NAME);
    printf("ID: %d\n", ptr_Raf->ID);
    printf("Grade: %.2f\n", ptr_Raf->GRADE);
    printf("Instructor: %s\n", ptr_Raf->INSTRUCTOR);

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Please describe what makes you doubt. – Yunnosch May 11 '21 at 05:51
  • I'm not sure what you're asking. Does this code not work for you? – Retired Ninja May 11 '21 at 05:51
  • 1 pointer typedef. 2 structure pointer. What is the third? – Yunnosch May 11 '21 at 05:52
  • 4
    If I get you right, you want to define a type, that already is a pointer. That is possible via `typedef struct {} student, *pstudent;` But you should never ever do this. Hiding pointers in typedef is considered bad practice. – Gerhardh May 11 '21 at 05:52
  • See [Is it a good idea to typedef pointers](https://stackoverflow.com/q/750178/15168) – TL;DR, the answer is "No", with possible exceptions for function pointers. – Jonathan Leffler May 11 '21 at 06:05
  • thanks for the replies guys, I'll check into it, I kept searching where the f is the reply box and couldn't find it earlier lmao – Raikzel Bondesto May 11 '21 at 06:08
  • 1
    The code shown doesn't use the typedef name `student`; it only uses the structure tag `struct student`. You could replace either or both occurrences of `struct student` with just `student` inside `main()` and the meaning of the code would not change. Note that it is unusual to use all-capitals in the member names; all-capital notation is normally reserved for macros and enumeration constants. Use lower-case or mixed-case names for structure member names. – Jonathan Leffler May 11 '21 at 06:08

2 Answers2

1
#include<stdio.h>
#include<stdlib.h>

typedef struct student //old data type
{
    char NAME[20];
    int ID;
    float GRADE;
    char INSTRUCTOR[20];
}student; //new data type


int main()
{
    student Raf = {"Rafael Sunga", 1775, 1.35, "Kenneth Auxillos"};
    student *ptr_Raf; //declaring ptr to a structure
    ptr_Raf = &Raf; //asigning address of variable with & 
    
    printf("Student Name: %s\n", ptr_Raf->NAME);
    printf("ID: %d\n", ptr_Raf->ID);
    printf("Grade: %.2f\n", ptr_Raf->GRADE);
    printf("Instructor: %s\n", ptr_Raf->INSTRUCTOR);

}

I found the solution, I just removed the "struct" and left the student in, works fine, thannks for the help

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

The so-called name space rules of C are a bit confusing. When it comes to structs there are 3 different ones that apply: struct tag, struct member and ordinary identifiers.

  • struct student is a struct tag.
  • typedef ... }student; is an ordinary identifier (like any variable name).

Since these exist in different name spaces, we can use the same identifier. When you type struct student in your code, you refer to the struct by tag, if you just type student then the ordinary identifier. As you can tell it gets very confusing when they are given the same name, so we shouldn't do that.

How to declare structs is otherwise a style issue with no obvious right or wrong. The Linux world uses the struct name { ... }; style with struct tags. The rest of the world uses the typedef struct { ... } name; style.

Lundin
  • 195,001
  • 40
  • 254
  • 396