2014-11-07 2 views
-3

Я пытаюсь инициализировать указатель queueADT под названием initAmigo. Видимо, я никогда не создать, если структура не делает указатели для (недействительных * данные)void Указатель на структуру вызывает ошибку «разыменование» void * 'pointer'

причин, почему я не могу поставить какие-либо данные в ничтожных * данных в структуре узла:

Asuumption 1: Заберите пустота * данные и сказать User_struct (из .h файла)

Предположение 2: RegisteredData не должен быть указателем, так как параметр в que_Insert запрашивает указатель)

Предположение 3: в функции AddUser, que_insert (initAmigo, registeredData); RegisteredData должны быть вставлен в пустоте * узел данных был на самом деле указатель на указатель на структуру

Имея ссылку очереди пустоты * создает следующее сообщение об ошибке:

amigonet.c: In function 'findUser': 
amigonet.c:247:21: warning: dereferencing 'void *' pointer [enabled by default] 
    if (currNode->data->name == name) { //If front is the name being searched 
        ^
amigonet.c:247:21: error: request for member 'name' in something not a structure or union 
amigonet.c:253:22: warning: dereferencing 'void *' pointer [enabled by default] 
    if (currNode->data->name == name) { 

.c файл просто должен создать структуру очереди пользователей (QueueADT initAmigo)

typedef struct node { 
            //name of data userStruct (just for referencing) 
    void* data; 
    struct node *next; 
}node; 


struct queueStruct { 
    struct node *front;        /* pointer to front of queue */ 
    struct node *rear;        /* pointer to rear of queue */ 
    int (*cmprFunc)(const void*a,const void*b);  /* The compare           function used for insert */ 

}; 

typedef struct queueStruct *QueueADT;  //typedef inserted for pointers, 
               //name is QueueADT 

#define _QUEUE_IMPL_ 
#include "queueADT.h" 

/// create a queue that is either sorted by cmp or FIFO 
//function with two void 
QueueADT que_create(int (*cmp)(const void*a,const void*b)) { 

    QueueADT new; 
    new = (QueueADT)malloc(sizeof(struct queueStruct));  //NOTE: NO POINTERS HERE 
                  //use pointers in Single lines 
    if (cmp == NULL) { 
     new->front = NULL; 
     new->rear = NULL; 
     new->cmprFunc = NULL; 

    } else { 
     new->cmprFunc = cmp; 
     new->front = NULL; 
     new->rear = NULL; 
    } 
    return (new); 
} 

//free the queue once the nodes have been cleared from the queue 
void que_destroy(QueueADT queue) { 
    if (queue->front == NULL) { 
     free(queue); 
    } else { 
     que_clear(queue); 
     free(queue); 
    } 
} 

//passes a real pointer for dynamic allocation 
//set a temp to the front to be deleted, then front becomes the next and delete the temp 

void que_clear(QueueADT queue) { 


    while (queue->front->next != NULL) { 
     struct node *temp; 
     temp = (struct node*)malloc(sizeof(struct node)); 
     temp = queue->front; 
     queue->front= queue->front->next; 

     free(temp); 
     } 
} 


//if the cmpr function returns a positive then put in in before the b node in cmp(curr, temp) 
void que_insert(QueueADT queue, void *data) { 
    struct node *temp; 
    temp = (struct node*)malloc(sizeof(struct node)); 
    temp->data= data; 
    temp->next = NULL; 
    node *currNode;    //simply a pointer 
    currNode = queue->front; 
    if (queue->front != NULL) {    //+1 or more in Q 
      if (queue->cmprFunc == NULL) {  //if the cmp_func is FIFO 

       queue->rear->next = temp; 
       queue->rear= temp; 
       queue->rear->next=NULL; 
       if (queue->front == queue->rear) { 
        currNode->next = temp; 
        queue->rear = temp; 
        temp->next= NULL; 
        } 
      } else { 

       while (currNode->next != NULL){  //2 or more 
        if (((*(queue->cmprFunc))(currNode->data, temp->data) >= 0 
          && (currNode == queue->front))) { 
          temp->next = currNode; 
          queue->front=temp; 
          break; 
          } 
        if (((*(queue->cmprFunc))(currNode->next->data, temp->data) >= 0)) {    

          temp->next = currNode->next; 
          currNode->next = temp; 
          break; 
        } else { 
         currNode = currNode->next;   //move past front and possibly rear 
         if (currNode->next == NULL) {  //currNode is rear of queue 
          currNode->next = temp; 
          queue->rear = temp; 
          temp->next = NULL; 
          break; 
         } 
              //exit_failure 
        } 
       } 
       if (queue->front == queue->rear) {  //exactly 1 node in queue 

        if (((*(queue->cmprFunc))(currNode->data, temp->data) >= 0)) { 

          temp->next = queue->front; 
          queue->front = temp; 
         } else { 

          queue->front->next = temp; 
          queue->rear = temp; 

         } 
        } 
      } 
    } else {            
     queue->front = temp; 
     queue->rear= queue->front; 
     queue->front->next= queue->rear->next = NULL; 

     } 
    } 


//removes the front 
void *que_remove(QueueADT queue) { 
    if (queue->front != NULL) {  //if the size of queue is greater than 1 
     node *currNode;     // dont make new node, pointer 
     currNode = queue->front; 
     void *data = currNode->data; 
     if (queue->front == queue->rear) { //if size of queue is 1 
      free(currNode); 
      queue->front = queue->rear = NULL; //set both to NULL 
     } else { 
      queue->front= currNode->next; 
      free(currNode); 
     } 
     return data;  
    } 
    return NULL; 
} 

bool que_empty(QueueADT queue) { 
    if (queue->front == NULL) { 
     return true; 
    } else { 
     return false; 
     } 
} 


QueueADT initAmigo; 

//struct queueStruct *initAmigo 
//QueueADT initAmigo; 

//make a Queue with front and rear 
    //compare function with A-Z 

struct Friends_struct { 
    struct queueStruct *amigos_Queue; 
}; 


void create_amigonet() { 
    //makes the same thing as que_Create 
    //(Friends)malloc(sizeof(struct Friend_struct)); 

    initAmigo = que_create(NULL); 
    printf("%lu Founded size of an.c\n\n\n\n\n\n\n\n\n\n\n", sizeof(initAmigo)); 
} 


//The add user will find a new user and make the name of the user to a amigonet 
//add Usernode to queue initAmigo 
//make sure set to first and display queue 

void addUser(const char *name) { 
    //create Usernode and void *data (user w/ friends) 
    //check to see if User already exists 
    if (findUser(name) != NULL) { 
     return; 
    } 
    //create data for node, by initializing memorry for a userStruct 
    struct User_struct *registeredData; 
    registeredData = (struct User_struct*)malloc(sizeof(struct User_struct));  //first user Data 

    registeredData->name = name; 

    //create a Friends 
     //put this in file create F 
    struct Friends_struct *initAmigos; 
    initAmigos = (struct Friends_struct*)malloc(sizeof(struct Friends_struct));  //NOTE: NO POINTERS HERE 
    //set Friends list to Null 
    initAmigos->amigos_Queue = que_create(NULL); 

    //put User with empty Friends struct 
    registeredData->amigos = initAmigos; 

    //void que_insert(QueueADT queue, void *data) 
    que_insert(initAmigo , registeredData); 
    printf("%s User was inerted \n", name); 

} 
//Find a user in the init Amgio 
//start at front as temp, go to next until name found 

User *findUser(const char *name) { 
    struct node *currNode; 
    currNode = initAmigo->front; 
    printf("%lu Founded size of an.c\n\n\n\n\n\n\n\n\n\n\n", sizeof(initAmigo)); 
    if (initAmigo->front == NULL) { 
     return NULL; 
    } else { 
     if (currNode->data->name == name) { //If front is the name being searched 
      return currNode->data; 
     } 
       //Loop after front 
     while (currNode->next != NULL) { 
      currNode = currNode->next; 
      if (currNode->data->name == name) { 
       return currNode->data; 
      } 
     } 
     node *rear = currNode; 
     if (rear->data->name == name) { 
      return rear->data; 
     } 
    } 
    //User Not Founded 
    return; 
} 
#if 0 
void addAmigo(User *user, User *amigo) { 

    //check to see if node exits within friends 
    //go to use friends list 
    //traverse the node, check data for amigo->name == data->name 

    if ( user->amigos->amigos_Queue->front != NULL) { 
     node *currNode = user->amigos->amigos_Queue->front; 
     if (currNode->data->name == amigo->name) { 
      return ; 
     } else { 
      //loop to determine if User friend queue front to rear has amigo already 
      while (currNode->next != NULL) { 
       currNode = currNode->next; 
       if (currNode ->data->name == amigo->name) { 
        return; 
       } 
      } 
     } 
    } 
    que_insert(user->amigos->amigos_Queue, amigo); 
} 


void removeAmigo(User *user, User *ex_amigo) { 
    //Pre Condition: Amgio exists in user 


    //go to user 
    //set user as currNode 


    //either create a function in queueADT remove 
    // use a prev and curr and next and traverse, remove, connect 
    //preconditions: front is not examigo 
    // rear is not examigo 
    // amigo is friend of user 

} 


size_t separation(const User *user1, const User *user2) { 

    return; 
    //queue a DFS 
    // queue a BFS 
    // 
} 
//search using a queue BFS 
//search using a DFS 

//number of users and their names 
void dump_data() { 
    if (initAmigo->front != NULL) { 
     node *currNode = initAmigo->front; 
     printf("%s: Amigos!\n", currNode->data->name);  //prints the name of the first node 
     while (currNode->next != NULL) { 
      currNode = currNode->next; 
      printf("%s: Amigos!\n", currNode->data->name); 
     } 
    //while loop of each user set to currNodw 

    //set a variable for currNodeAmigo and print the names of them inside ^^^ 
    } 
} 

void destroy_amigonet(){ 
    //clear Friend Queue and destroy for each User Friend 
    //destroy initAmigo queue 
} 
+1

В чем вопрос? – GreenAsJade

+0

Вопросы заключаются в следующем: Почему это говорит, что я разделяю данные void *, когда в que_Insert я помещаю структуру User_struct там? Как вставить структуру без ошибки? Была ли моя проблема в AddUser, когда я инициализировал void * Data в FindUser, когда я не использовал указатель с -> data-> name? – arrowinfedex

ответ

1

проблема выглядит следующим образом:

typedef struct node { 
    void* data; 
    struct node *next; 
}node; 

... 

struct node *currNode; 

if (currNode->data->name == name) { //If front is the name being searched 
    return currNode->data; 
} 

Тип currNode->data: void *. Это неполный тип, что означает, что он не может быть разыменован. Компилятор понятия не имеет, что делать, на что он указывает. Вам нужно конвертировать указатель void в указатель на значащий тип.

Вы не определили User тип в коде вы в курсе, но я предполагаю, что вы хотите что-то вроде этого:

User *user = currNode->data; 

if (user->name == name) { 
    return user; 
} 

Вы также должны сделать аналогичные изменения в других местах в одной и той же функции.

+0

Мне действительно нравятся данные void *, однако он использовался для моего последнего проекта, и я могу фактически изменить его на реальное имя структуры и изменить все параметры с этим. Вы бы на самом деле работали или создавали бы другой указатель на указатель? – arrowinfedex

+0

Пользователь определен в файле h как 'typedef struct User_struct {' 'const char * name;' 'Друзья amigos;' '} Пользователь;' Функция AddUser изменила данные void *, чтобы указать на эту структуру, поэтому я так и думал. – arrowinfedex

+0

@arrowinfedex: Если вы хотите, чтобы функция 'que' была общей (способной обрабатывать какой-либо объект), то вы _need_ член' void * data'. Если вы заботитесь только об одном типе объекта, то использование правильного типа во всех случаях обеспечит лучшую безопасность типов. Это ваш вызов. –

Смежные вопросы