如何访问队列中节点中的结构?C

如何访问队列中节点中的结构?C,c,struct,queue,C,Struct,Queue,因此,我将一个队列实现为一个链接列表,其中每个节点的数据都是一个存储大量内容的结构,但我希望访问字符串 我将在下面发布我的代码,但我在一个单独的queue.c文件中创建和管理队列,该文件包含在我的主.c文件中。从中我将结构连接传递到,以将连接存储在队列中 所以我试图确保我在队列中正确地存储了这个,但我不知道如何访问它 在90号线上,我试着 printf("%s",networkQueue->front.Connection.ipAddress); 但它给出了一个错误 networ

因此,我将一个队列实现为一个链接列表,其中每个节点的数据都是一个存储大量内容的结构,但我希望访问字符串

我将在下面发布我的代码,但我在一个单独的queue.c文件中创建和管理队列,该文件包含在我的主.c文件中。从中我将结构连接传递到,以将连接存储在队列中

所以我试图确保我在队列中正确地存储了这个,但我不知道如何访问它

在90号线上,我试着

    printf("%s",networkQueue->front.Connection.ipAddress);
但它给出了一个错误

networkManager2.c:90:33: error: request for member ‘Connection’ in something not a structure or union
  printf("%s",networkQueue->front.Connection.ipAddress);
                                 ^
因此,我想知道如何访问队列中的内容,我想我只是被所有不同的指针之类的东西搞混了

networkManager2.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "queue.h"
#define TRUE 1
#define FALSE 0

struct connection
{
    /*
    IP Address can be stored in two formats, IPv4 or IPv6. 
    IPv4 is 15 chars long and of the format ###.###.###.### in regular digits
    IPv6 is 25 chars long and of the format ####:####:####::####:### in hex
    */
    char* ipAddress[26];
    int port; //port value from 0-65535
    char* protocolType[2];
    union
    {
        char* connectionName[6]; //UDP only, 5 letter name to the connection
        int hops; //TCP only, 0-255 declaring the number of hops for the connection.
    }protocol;  
};

/*
Defines Connection as a type struct connection
*/
typedef struct 
{
    char* ipAddress[26];
    int port;
    char* protocolType[2];
    union
    {
        char* connectionName[6]; 
        int hops; 
    }protocol;  
} Connection;

Connection* processConnection()
{
    /*
    Where the information for the connection is input.
    */
    Connection *currentConnection = malloc(sizeof(Connection));
    printf("IP Address: ");
    gets(currentConnection->ipAddress);
    printf("Protocol Type: ");
    gets(currentConnection->protocolType);
    printf("Port: ");
    scanf(" %d", &currentConnection->port);

    /*
    Depending on the protocol type, it stores hops or connection name.
    */
    if(strcmp(currentConnection->protocolType, "TCP") == 0)
    {
        printf("Hops: ");
        scanf(" %d", &currentConnection->protocol.hops);
    }
    else if(strcmp(currentConnection->protocolType, "UDP") == 0)
    {
        printf("Connection Name: ");
        scanf(" %s",&currentConnection->protocol.connectionName);
    }

    return currentConnection;
}

/*
This stores all the connections within a linked list queue. 
It takes in the queue that was created within the main, and also the new 
connection it wishes to add to the queue. It sends this to manage queue 
function from the queue.c file. 
*/
void storeConnection(Connection *incomingConnection, Queue *networkQueue)
{
    manageNetworkQueue(networkQueue,incomingConnection);
    printf("%s",networkQueue->front.Connection.ipAddress);
}

int main()
{
    int running = 0; //variable to check if the user wants to input more connections
    char* holder[1];
    Queue *networkQueue = malloc(sizeof(Queue));
    *networkQueue = createNetworkQueue();

    /*
    Inputs connections and stores them in an array of connections. The array can
    only hold up to 5 connections and replaces the oldest each time if its full. 
    */
    do
    {
        Connection *incomingConnection = processConnection();
        storeConnection(incomingConnection, networkQueue);

        printf("Continue?: ");
        scanf(" %d",&running);
        gets(holder); //avoid the scanf problem.
    }while(running == 0);
    free(networkQueue);
}

编辑:顺便说一句,评论不是最新的。

void*Connection;连接类型为void您必须强制转换它前面是指针。更改为->我认为使用malloc分配的内存没有正确释放。freenetworkQueue不会释放队列中的所有节点。您必须注意释放网络队列中的所有节点
#include <stdlib.h>
#include <stdio.h>
struct node
{
    void *Connection;
    struct node *previous;
    struct node *next;
}node;

typedef struct Queue
{
    struct node *front;
    struct node *end;
}Queue;

Queue createNetworkQueue(Queue *networkQueue)
{
    networkQueue->front=NULL;
    networkQueue->end=NULL;
    return *networkQueue;
}

void manageNetworkQueue(Queue *networkQueue, void *incomingConnection)
{
    struct node *newNode = malloc(sizeof(node));
    if(networkQueue->front = NULL)
    {
        networkQueue->front=newNode;
        networkQueue->end=newNode;
        newNode->Connection=incomingConnection;
        newNode->next=NULL;
        newNode->previous=NULL;
    }else
    {
        newNode->Connection=incomingConnection;
        newNode->next=NULL;
        newNode->previous=networkQueue->end;
        networkQueue->end->next=newNode;
        networkQueue->end=newNode;
    }
}
#ifndef QUEUE_H_
#define QUEUE_H_

struct node
{
    void *Connection;
    struct node *previous;
    struct node *next;
}node;

typedef struct Queue
{
    struct node *front;
    struct node *end;
}Queue;

Queue createNetworkQueue();
void manageNetworkQueue(Queue *networkQueue, void *incomingConnection);

#endif