所以我有一个队列实现为一个链接列表,其中每个节点的数据是一个存储一堆东西的结构,但我想访问该字符串。
我将在下面发布我的代码,但是我在一个单独的queue.c文件中创建和管理队列,该文件包含在我的main.c文件中。我从中传递struct连接以将连接存储在队列中。
所以我试图确保我实际上正确地将其存储在队列中,但我不知道如何访问它。
在第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", ¤tConnection->port);
/*
Depending on the protocol type, it stores hops or connection name.
*/
if(strcmp(currentConnection->protocolType, "TCP") == 0)
{
printf("Hops: ");
scanf(" %d", ¤tConnection->protocol.hops);
}
else if(strcmp(currentConnection->protocolType, "UDP") == 0)
{
printf("Connection Name: ");
scanf(" %s",¤tConnection->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);
}
queue.c
#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;
}
}
queue.h
#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
编辑:评论不是真正的最新顺便说一句。