m0_74908430 2023-05-23 19:24 采纳率: 33.3%
浏览 35

C语言读取访问权限冲突

#无法用s_gets给结构中的数组赋值
#相关代码片

)

img


报错内容

img

把s_gets中find=‘\0'改掉后 不去掉换行符 就可以正确进行 留下则报错访问权限冲突 麻烦大家指正!!

list.h
#ifndef LIST_H
#define LIST_H
#include<stdbool.h>

#define TSIZE 45

struct file
{
    char title[TSIZE];
    int rating;
};

typedef struct file Item;

typedef struct node
{
    Item item;
    struct node* next;
}Node;

typedef Node* List;

void InitializeList(List* plist);

bool ListIsEmpty(const List* plist);

bool ListIsFull(const List* plist);

unsigned int ListItemCount(const List* plist);

bool AddItem(Item item, List* plist);

void traverse(const List* plist, void(*pfun)(Item item));

void Emptythelist(List* plist);

#endif


film3.c
#include<stdio.h>
#include<stdlib.h>
#include<list.h>
void showmovies(Item item);
char* s_gets(char* st, int n);
//* &movies 

int main(void)
{
    List movies; 
    Item temp;
       
    InitializeList(&movies);
    if (ListIsFull(&movies))
    {
        fprintf(stderr, "No memory available,BYe\n");
        exit(1);
    }
   
    puts("enter first movie title:");

    while (s_gets(temp.title, TSIZE) != NULL && temp.title[0] != '\0')
    {
        puts("enter your rating1-10: ");
        scanf_s("%d", &temp.rating);
        while (getchar() != '\n')
            continue;
        if (AddItem(temp, &movies) == false)
        {
            fprintf(stderr, "problem allocating memory\n");
            break;
        }
        if (ListIsFull(&movies))
        {
            puts("the list is now full:");
            break;
        }
        puts("enter next movie title,enter line to stop");
    }

    if (ListIsEmpty(&movies))
    {
        puts("no data entered");
    }
    else
    {
        puts("here is movie list:");
        traverse(&movies, showmovies);
    }
    printf("your enter %d movies\n", ListItemCount(&movies));
    Emptythelist(&movies);
    printf("Bye\n");

    return 0;
}

char* s_gets(char* st, int n)
{
    char* ret_val;
    char* find;
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');
        if (find)
        {
            *find = '\0';
        }
        else
        {
            while (getchar() != '\n')
                continue;
        }
    }
    return ret_val;
}

void showmovies(Item item)
{
    printf("movie: %s and rating: %d ", item.title, item.rating);
}
#include<stdio.h>
#include<stdlib.h>
#include<list.h>
static void copytonode(Item item, Node* pnode);

void InitializeList(List* plist)//(struct node **plist)
{
    plist = NULL;
}

bool ListIsEmpty(const List* plist)
{
    if (plist == NULL)
        return true;
    else
        return false;
}

bool ListIsFull(const List* plist)
{
    Node* pt;
    bool full;

    pt = (Node*)malloc(sizeof(Node));
    if (pt == NULL)//尝试为新项分配空间 若失败说明已满
        full = true;
    else
        full = false;
    free(pt);

    return full;
}

unsigned int ListItemCount(const List* plist)
{
    unsigned int count = 0;
    Node* pnode =* plist;

    while (pnode != NULL)
    {
        ++count;
        pnode = pnode->next;
    }
    
    return count;
}

bool AddItem(Item item, List* plist)
{
    Node* pnew;
    Node* scan = *plist;

    pnew = (Node*)malloc(sizeof(Node));
    if (pnew == NULL)
        return false;

    copytonode(item, pnew);
    pnew->next = NULL;
    if (scan == NULL)
        *plist = pnew;
    else
    {
        while (scan->next != NULL)
            scan = scan->next;
        scan->next = pnew;
    }
    
    return true;
}

void traverse(const List* plist, void(*pfun)(Item item))
{
    Node* pnode = *plist;

    while (pnode = NULL)
    {
        (*pfun)(pnode->item);
        pnode = pnode->next;
    }
}

void Emptythelist(List* plist)
{
    Node* psave;

    while (plist != NULL)
    {
        psave = (*plist)->next;
        free(*plist);
        *plist = psave;
    }
}

static void copytonode(Item item, Node* pnode)
{
    pnode->item = item;
}





  • 写回答

1条回答 默认 最新

  • 创意程序员 2023-05-23 19:49
    关注

    strchr()是查找字符出现的位置,与find声明时的类型 char * 不一样,强行赋值,相当于给这个指针一个未知的内存地址(strchr()返回的一个比较小的数),*find 是访问这个内存地址,试图修改,而这块内存被其它程序使用,不允许访问

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月23日

悬赏问题

  • ¥15 关于树的路径求解问题
  • ¥15 yolo在训练时候出现File "D:\yolo\yolov5-7.0\train.py"line 638,in <module>
  • ¥30 戴尔inspiron独显直连
  • ¥15 进行一项代码设计遇到问题
  • ¥15 Mutisim中关于74LS192N计数器芯片设计(计数器)
  • ¥50 fastadmin后台无法删除文件
  • ¥15 oracle查询Socket read timed out错误
  • ¥15 matlab支持向量机使用错误
  • ¥99 利用C/C++语言,使用TCP/IP协议,编一个简易聊天程序
  • ¥15 如何使用python 实现对串口/dev/ttyUSB0进行上锁,使得该串口只能在一个python脚本中使用,其他脚本不能操作这个串口