问题描述:

I am new to C, and I am facing this problem when practicing.

The struct:

 typedef struct {

char name[20];

int score[3];

int no;

} Student;

The initialize:

Student *pStudent;

strcpy(pStudent->name,"Kven");

pStudent->score[0]=1;

pStudent->score[1]=2;

pStudent->score[2]=3;

pStudent->no=1;

If the "pStudent" is not a pointer I can get it right. But if it is a pointer, I will always get a "bad access" error. How to solve it if I must use a pointer? Thank you.

网友答案:

A variable of type pointer stores the address in memory of another variable (it points to another variable).

Your variable pStudent is not initialized. It doesn't point to anything. In fact, it contains some garbage value that, most of the times, is the address of a memory area where the program is not allowed to write. This is why when you attempt to write there (the call to strcpy()) the operating system slaps your program over its "hand" and terminates it with the error message you mentioned.

How to correctly use pointers

One usage of pointers is to point to a certain variable that is statically allocated (defined in the program):

Student s, *pStudent = &s;

This example declares the variable s of type Student and the variable pStudent that points to it. Notice it is initialized with &s which is the address of s. Now, your code uses pStudent to initialize the fields of s.

Another usage of pointers is to dynamically allocate memory during runtime:

Student *pStudent;
pStudent = malloc(sizeof(Student));

In this situation, pStudent is initialized with the address of a new variable of type Student that is created during runtime (it doesn't have a name) by calling malloc(). malloc() allocates a block of memory (of the specified size) and return its address.

When you don't need it, you have to free the memory allocated by malloc() (using free()) for reuse:

free(pStudent);

After this call, the value of the variable pStudent doesn't change, it still points to the same address but it is invalid and using it produces undefined behaviour. You have to put the address of another Student structure in pStudent (using one of the two ways presented here) before using it again.

网友答案:
Student *pStudent;

should be

Student *pStudent = malloc(sizeof(Student));

Allocate memory for the pointer before writing data once done using it

free(pStudent);
网友答案:
Student *pStudent;

creates a pointer of type Student*. This pointer is not initialized and points to some "random" location. Allocate memory for it using malloc(or calloc) from stdlib.h:

pStudent = malloc(sizeof(Student));

and then free it after its use using:

free(pStudent);
网友答案:

You don't allocate any memory, you just allocate a pointer. You'll need to study pointers and how they work.

Fix the code like this:

Student student;
strcpy(student.name,"Kven");
student.score[0]=1;
student.score[1]=2;
student.score[2]=3;
student.no=1;

Alternatively you could allocate memory dynamically. But it doesn't make any sense in this case (and yet I bet at least 3 people will post answers telling you to do that...). Plus you really need to study pointers before doing dynamic allocation.

Student *pStudent = calloc(1, sizeof(Student);

if(pStudent == NULL)
{
  // error handling
}

strcpy(pStudent->name,"Kven");
pStudent->score[0]=1;
pStudent->score[1]=2;
pStudent->score[2]=3;
pStudent->no=1;

...

free(pStudent);
网友答案:

You need to allocate memory for your structure first.

You can allocate memory with:

Student *pStudent = malloc(sizeof(Student));

And then you must release it at the end with

free(pStudent);
相关阅读:
Top