This is how you would do this:
cpp
#include <iostream>
#include <cstring>
using namespace std;
typedef struct
{
char name[20];
int age;
int id;
float gpa;
} studentType;
int main() {
studentType student;
strcpy(student.name, "Andy");
cout << "Student Name: " << student.name << endl;
system("pause");
return 0;
}
There are several problems with just using the pointer.
#1. The memory allocated to hold the data is not inside the structure, which means that operations that do things like copy the data become complicated.
#2. If you assign the pointer to a const char * (like any literal string) then you can not manipulate it. For example
student.name[0]=0 is a quick way to set the name to an empty string, but this would cause the program to crash if the pointer pointed to a string literal.
#3. Operations to write the data to a file as a record are complicated since the pointer value may change (it is never a good idea to write a pointer to a file).
There ARE reasons to use pointers in structures, dynamic/arbitrary size for one.