将元素添加到包含 char 指针的结构

Adding an element to a struct that contains a char pointer

好吧,我确信这不是一个太难的问题,但在尝试了这么长时间之后我迷路了,所以这里首先是代码,是我的结构的声明。

struct GraphicElement {
    char* fileName;
    struct GraphicElement* pNext;
  };
    struct RasterGraphic {
    struct GraphicElement* GraphicElements;
  };

然后我调用一个负责初始化的函数,我认为它工作正常。

int main(void)
{
char response;
BOOL RUNNING = TRUE;
struct RasterGraphic RG;
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
InitRasterGraphic(&RG);

init函数如下

void InitRasterGraphic(struct RasterGraphic* pA)
{

pA->GraphicElements = malloc(sizeof(struct GraphicElement));

if (pA->GraphicElements == NULL) return;
//pA->GraphicElements = 0;
//pA->GraphicElements->pNext = NULL;
//pA->GraphicElements->fileName = NULL;
pA->GraphicElements->fileName = (char*)malloc(sizeof(char));

return;
}

所以下一部分代码是我要问我的问题的地方。我有一个函数需要用户输入某种字符串。我想接受该输入并将其添加到下一个元素。因此,每次用户调用他们输入的函数时,都会将其添加到下一个元素中。我现在的代码一团糟,我知道它肯定可能离我已经尝试了很多事情很远了。我相信我需要使用 realloc。我无法在这里解决的是如何获取输入并添加它,以便稍后我可以按顺序打印所有元素。因此,例如用户调用函数 3 次并输入 "hello" "world"。稍后我想循环打印 图 1 是 "hello" 图 2 是 "world"

void InsertGraphicElement(struct RasterGraphic* pA)
{
char response[256];
printf("Insert a GraphicElement in the RasterGraphic\nPlease enter the GraphicElement filename: ");
scanf("%s", &response[0]);
pA->GraphicElements->fileName = realloc(pA->GraphicElements, 256*sizeof(char));
pA->GraphicElements++;
strcpy(pA->GraphicElements->fileName, &response);
if (pA->GraphicElements == 1){

    printf("\nThis is the first GraphicElement in the list\n");
    }

return;
}

以下是您的代码中的几个问题。

  1. 我相信 InitRasterGraphic 应该只初始化 pA->GraphicElements = NULL 而不是 malloc 初始化它。因为同样应该在 InsertGraphicElement 函数中处理。

  2. pA->GraphicElements->fileName = (char*)malloc(sizeof(char)); 这只会分配大小为 charmemory 并且 allocing 和 [=21= 没有意义]ing.

  3. scanf("%s", &response[0]); 应该是 scanf("%s", &response[0]);.

  4. pA->GraphicElements++; 这是错误的,应该多使用一个成员来维护列表中的节点数。

因此,在更正上述问题后,您的代码将如下所示。

您的 RasterGraphic 将如下所示。

struct RasterGraphic {
    int numNodes;
    struct GraphicElement* GraphicElements;
  };

您的 InitRasterGraphic 将如下所示。

void InitRasterGraphic(struct RasterGraphic* pA)
{

    pA->GraphicElements = NULL;
    pA->numNodes = 0;

   return;
}

您的 InsertGraphicElement 将如下所示。

void InsertGraphicElement(struct RasterGraphic* pA)
{
   struct GraphicElement *newNode = malloc(sizeof(*newNode));
   if (newNode == NULL) return;

   newNode->fileName = malloc(256*sizeof(char));
   if (newNode->fileName == NULL) return;
   newNode->pNext = NULL;


   printf("Insert a GraphicElement in the RasterGraphic\nPlease enter the GraphicElement filename: ");
   scanf("%s", newNode->fileName);


   if (pA->GraphicElements == NULL)
   {
      pA->GraphicElements = newNode;
   }
   else
   {
      struct GraphicElement *tempHead = pA->GraphicElements;
      while(tempHead->pNext != NULL)
      {
         tempHead = tempHead->pNext;
      }
      tempHead->pNext = newNode;
   }

   pA->numNodes++;
   if (pA->numNodes == 1){
      printf("\nThis is the first GraphicElement in the list\n");
   }

  return;
}