好了朋友们。我可以看到我正在做的这个项目的终点线。我试图弄清楚为什么当我尝试返回数组的值(即出队)时我的数组吐出 NULLS。我相信我的入队函数有效,并且也相信它从地址中获取值指针所指。这是我的代码。我故意不包括我的主要功能只是因为我不想让电路板超载。但是,如果需要查看它来诊断问题,请告诉我。
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// the capacity of the queue
#define CAPACITY 10
// a queue
typedef struct
{
// the index of the first element in the queue
int head;
// storage for the elements in the queue
char* strings[CAPACITY];
// the size of the queue
int size;
}
queue;
// declare a queue (as a global variable)
queue q;
/*
* Puts a new element into the queue into the "end" of the data structure
* so that it will be retrived after the other elements already in the
* queue.
*/
bool enqueue(char* str)
{
int rear = 0; // back of the queue
if (q.size==CAPACITY)
{
return false;
}
else
{
rear = (rear + 1) % CAPACITY;
q.strings[rear] = str;
printf("%s", q.strings[rear]);
q.size++;
return true;
}
}
/**
* Retrieves ("dequeues") the first element in the queue, following the
* the "first-in, first-out" (FIFO) ordering of the data structure.
* Reduces the size of the queue and adjusts the head to the next element.
*/
char* dequeue(void)
{
char *charHead = NULL;
if (q.size)
{
charHead = malloc(sizeof(char)) ;
char *chpointer = malloc(sizeof(strArray[12]));
q.head++;
q.head = q.head%CAPACITY;
charHead = q.strings[q.head];
return charHead;
}
// Return null character if queue is empty
return NULL;
}