c - 正确返回指针

标签 c pointers

我用 C 创建了一个 ADT。它基本上是一个链表,其中。列表中的每个节点都有一个存储项目的成员变量。

void *item

我有一个成员函数用于添加到列表和删除以及其他。

在我的 main.c 文件中,我调用了 ListAdd() 函数,该函数接受一个列表并将一个项目添加到下一个可用节点。 ListFirst() 函数转到列表的前面并返回存储在该节点的项目。

return list->item

我的主要功能是存储一个结构,我命名为 pcb,作为每个节点的项目。 所以这是我的问题。当我测试 ADT 时,ListFirst() 函数在返回 void *item 之前具有该项目的所有正确成员数据。但在我的 main.c 文件中,所有信息都不正确。有谁知道为什么会发生这种情况?

我想因为该项目是空的 * 我正在返回该项目的地址。所以在我的 main.c 文件中,代码如下所示:

typdef struct PCB{
        //member variables 
}pcb;

pcb *readypcb;
readypcb = (pcb *)ListFirst(listHead);
//print out data

所以我认为 readypcb 指针会指向 ListFirst() 返回的项目的地址。此外,我将返回值转换为 (pcb *),因为我返回了一个 void *

总而言之,我返回 void *item 之前的那一行正确地打印出了项目成员数据。然后在它分配给 readypcb 之后我再次打印数据,只是这一次它是不正确的。有谁知道发生了什么事吗?

这是你们需要的一些额外代码:

我的 PCB 数据结构的头文件:

typedef enum Priority{

    high,
    medium,
    low

}Priority;

typedef enum State{

    running,
    ready,
    blocked
}State;

typedef struct PCB{

    Priority priority;              //records the priority level of the PCB
    int pid;                    //The process id of the control block
    State state;                    //records the state of the PCB

    char received[200];             //records any messads sent to this PCB
    char replied[200];              //records the messages that another process replies with

}pcb;

然后是我的 main.c 文件,我在其中分配返回的项目:

int checkForReadyProcesses(listPtr highP, listPtr medP, listPtr lowP, pcb *readypcb){
    int retCode = 0;

    //check that the item counts for the ready queues are 0
    if(highP->itemCount > 0){
         readypcb = (pcb *)ListFirst(highP);
        //ListRemove(highP);

        if(TESTING_READY_QUEUES){
            printf("We have a PCB in the high priority queue\n");
        }

    }else if(medP->itemCount > 0){
        readypcb = (pcb *)ListFirst(medP);
        //ListRemove(medP);

        if(TESTING_READY_QUEUES){
            printf("We have a PCB in the medium priority queue\n");
        }

    }else if(lowP->itemCount > 0){
        readypcb = (pcb *)ListFirst(lowP);
        //ListRemove(lowP);

        if(TESTING_READY_QUEUES){
            printf("We have a PCB in the low priority queue\n");
        }

    }else{
        retCode = 1;
    }

    if(TESTING_READYPCB){
        printf("The ready address is: %p\n", readypcb);
        printf("The ready state is: %d\n", readypcb->state);
        printf("The ready priority is: %d\n", readypcb->priority);
        printf("The ready id is: %d\n", readypcb->pid);
    }

    return retCode;

}

最后是 LstFirst():

void *ListFirst(LIST *list){
    void *tempItem;

    list->currentItem = list->firstNode->item;
    list->currentNode = list->firstNode;

    //updates the position
    list->position = first;

    tempItem = ((list->firstNode->item));

    pcb curpcb = *(pcb *)list->firstNode->item;

    printf("the item has the adress: %p\n", list->firstNode->item);
    printf("the item pid is: %d\n", curpcb.pid);
    printf("the item state is: %d\n", curpcb.state);
    printf("the item priority is: %d\n", curpcb.priority);

    return list->firstNode->item;
}

List.h 文件:

typedef struct NODE *nodePtr;
typedef struct List *listPtr;

//define the node structire used for each node in the list except the head node
typedef struct NODE{    
    nodePtr nextNode;       //points to the next node in the list
    nodePtr previousNode;       //points to the previous node in the list
    void *item;         //holds the item for the node
} node;

//define the head structure for the List
typedef struct List{

    nodePtr firstNode;      //points to the first node of the list
    int itemCount;          //holds the number of items in the list
    nodePtr end;            //points to the end of the list
    void *currentItem;      //points to the current item
    nodePtr currentNode;        //points to the current node
    listPtr freedHeadNode;      //used for pointing to freedHead nodes when more than one is available

    //using an enum to represent where the current node is
    enum currentPos{
        beyondEnd = 2,
        end = 1,
        middle = 0,
        beforeStart = -1,
        first = -2,     //this represents the first item ever to be added
    } position;
} LIST;

//FUNCTIONS
LIST *ListCreate();         //creates a new list when called
int ListCount(LIST *list);      //returns the number of items in the list
void *ListFirst(LIST *list);        //returns the first item in the list
void *ListLast(LIST *list);     //returns a pointer to the last item
int ListAdd(LIST *list, void *item);    //adds an item to a list
void *ListCurr(LIST *list);     //points to the current item in the list
void *ListNext(LIST *list);     //points to the next item after the current spot
void *ListPrev(LIST *list);     //points to the previous item before the current node
int ListInsert(LIST *list, void *item); //inserts a node just before the previous node
int ListPrepend(LIST *list, void *item);//adds an item to the begining of the list
int ListAppend(LIST *list, void *item); //adds an item to the end of the list
void *ListRemove(LIST *list);       //removes and returns the current item from the list
void *ListTrim(LIST *list);     //returns and removes the last item of the list
void *ListSearch(LIST *list, int(*comparator)(void *, int), int comparatorArg);
void ListFree(LIST *list, void(itemFree)(void *));

我认为这应该有所帮助。如果您需要任何其他信息,请告诉我。

最佳答案

虽然我没有看到你所有的声明,但很可能你传递了列表头,头部得到更新,但无法返回。它无法返回的原因是因为您没有传递“双指针”。考虑:

void example(list *head) {
    if (head==NULL) head= malloc(sizeof(list));
    //...

在这里,您将内存分配给head,但head 是一个本地 变量;调用者看不到对其的任何更改。相反,做:

void example(list **head) {
    if (*head==NULL) *head= malloc(sizeof(list));
    //...

然后这样调用:

list *myList;
example(&myList);

关于c - 正确返回指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49403719/

相关文章:

c++ - Windows C/Sleep() 函数在时钟漂移期间将如何运行?

c - 将 shell 参数传递给 mac osx 上的 c 程序

c - memcpy regex_t 安全吗?

c - Scanf 函数中的错误

c - 直接给 C 指针赋值

将 char 转换为 1 个字符的空终止字符串

c - 指向指针的简单指针

c++ - 接收 0xC0000005 : Access violation reading location storing new pointer in pointer array

c - 指针算法的奇怪输出

c# - 在 C# 中使用结构调用 C 函数 - 我做错了什么?