在为C中的结构赋值时,为什么会出现访问冲突

时间:2013-11-18 03:44:53

标签: c pointers malloc

编译后,我收到此代码的访问冲突。我尝试了其他一些方法来实现这一点,我的结果不同,只是将打印的地址打印到打印字符串时打印的数字。

我要做的是声明一个指向结构的指针数组,这样我就可以存储许多学生的信息。

关于如何使这项工作得到任何建议将不胜感激 提前谢谢!

// Assignment 10 (2).cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <string.h>

#define SIZE 25
#define SIZE_FNAME 15
#define SIZE_LNAME 15

void clear(void);

/*********************************************************************************
Structure Student: Defines and holds all the variables for the information required
to run the Student Program

Input:
N/A

Output:
N/A
*********************************************************************************/
struct student
{
    char* firstName;
    char* lastName;
    double* gradeAvg;
};
typedef struct student;

/*********************************************************************************
Function Main:

Input:
N/A

Output:
N/A
*********************************************************************************/
int _tmain(int argc, _TCHAR* argv[])
{
    struct student *pStudent = (student*)malloc(sizeof(student));
    pStudent->firstName = (char*)malloc(sizeof(char) * SIZE_FNAME);
    pStudent->lastName = (char*)malloc(sizeof(char) *SIZE_LNAME);
    pStudent->gradeAvg = (double*) malloc(sizeof(double));
    if (pStudent == NULL)
    {
        printf("No mem");
        exit(0);
    }
    printf("Enter first name: ");
    scanf("%14s", pStudent->firstName, SIZE_FNAME);
    printf("%s", pStudent->firstName);
    clear();

    printf("Enter last name: ");
    scanf("%14s", pStudent->lastName, SIZE_LNAME);
    printf("%s", pStudent->lastName);
    clear();

    printf("Enter grade average: ");
    scanf("%lf", pStudent->gradeAvg);
    printf("%lf", *pStudent->gradeAvg);
    clear();
}
void clear(void)
{
    while (getchar() != '\n');
}

5 个答案:

答案 0 :(得分:3)

要为firstname字段分配内存,您应该这样做:

pStudent->firstName = malloc(sizeof(char) * SIZE_FNAME);

要扫描字符串,您应该执行此操作(注意,scanf_s是对正常scanf的微软特定更改,需要特殊参数)

scanf_s("%14s", pStudent->firstName, SIZE_FNAME);

您还需要为姓氏分配内存。

答案 1 :(得分:3)

首先,这是C文件,将其另存为file.c,而不是file.cpp。您的编译器会查看扩展以确定要使用的编译器。

我看到12个错误。

在下面的代码中查找^字符。

struct student *pStudent = (student*)malloc(sizeof(student));
                           ^^^^^^^^^^ remove this
pStudent->lastName = malloc(sizeof(char)*SIZE_LNAME); // add this line
*pStudent->firstName = (char)malloc(sizeof(char)*SIZE_FNAME);
^ remove this          ^^^^^^ remove this
if (pStudent == NULL)
    printf("No mem");
    // add an exit here, don't continue

printf("Enter first name: ");
scanf_s("14%s", *pStudent->firstName, SIZE_FNAME);
                ^ remove this         ^^^^^^^^^^ add this
printf("%d", pStudent->firstName);
         ^ should be s
clear();

printf("Enter last name: ");
scanf_s("14%s", *pStudent->lastName, SIZE_LNAME );
                ^ remove this        ^^^^^^^^^^ add this
clear();

printf("Enter grade average: ");
scanf_s("14%s", pStudent->gradeAvg);
         ^^^^ should be %lf
clear();
printf("%d", pStudent->gradeAvg);
         ^ should be %lf

答案 2 :(得分:0)

sharth的答案外,

scanf_s("14%s", *pStudent->lastName);

为此,首先将内存分配给姓氏,就像对firstname所做的那样。

答案 3 :(得分:0)

->的优先级高于*,因此*pStudent->firstName = ...的计算结果为*(pStudent->firstName) = ...,这会在尝试取消引用firstName时显然会导致访问错误,firstName可能值为0

另外,正如sharth所说,你首先不需要*

答案 4 :(得分:0)

即使没有明确提到你收到错误的地方,但看着代码,我认为你在

时遇到错误
*pStudent->firstName = (char)malloc(sizeof(char)*SIZE_FNAME);

将此更改为

pStudent->firstName = (char*)malloc(sizeof(char)*SIZE_FNAME);