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



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

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

任何关于我如何使这项工作发挥作用的建议都将不胜感激
提前感谢!

// 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');
}

首先,这是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

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

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

要扫描字符串,您应该执行(注意,scanf_s是微软对普通scanf的特定更改,需要特殊参数):

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

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

除了sharth的答案外,

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

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

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

此外,正如sharth所指出的,您根本不需要*

甚至没有明确提到你在哪里出错,但从代码来看,我认为你在上出错了

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

将其更改为

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

相关内容

  • 没有找到相关文章

最新更新