2010-05-28 6 views
2

Essayer de comprendre pourquoi cela ne fonctionne pas. Je continue à obtenir les erreurs suivantes: gauche « -> nextNode » doit pointer vers la classe/struct/union/type générique
(également toutes les lignes avec -> dans la fonction new_math_struct)C problème, gauche de '->' doit pointer vers class/struct/union/type générique?

fichier d'en-tête

#ifndef MSTRUCT_H 
#define MSTRUCT_H 

    #define PLUS 0 
    #define MINUS 1 
    #define DIVIDE 2 
    #define MULTIPLY 3 
    #define NUMBER 4 

    typedef struct math_struct 
    { 
     int type_of_value; 
     int value; 
     int sum; 
     int is_used; 
     struct math_struct* nextNode; 
    } ; 

    typedef struct math_struct* math_struct_ptr; 
#endif 

fichier C

int get_input(math_struct_ptr* startNode) 
{ 
    /* character, input by the user */ 
    char input_ch; 
    char* input_ptr; 

    math_struct_ptr* ptr; 
    math_struct_ptr* previousNode; 

    input_ptr = &input_ch; 
    previousNode = startNode; 

    /* as long as input is not ok */ 
    while (1) 
    {    
     input_ch = get_input_character(); 
     if (input_ch == ',') // Carrage return 
      return 1; 
     else if (input_ch == '.') // Illegal character 
      return 0; 


     if (input_ch == '+') 
      ptr = new_math_struct(PLUS, 0); 
     else if (input_ch == '-') 
      ptr = new_math_struct(MINUS, 0); 
     else if (input_ch == '/') 
      ptr = new_math_struct(DIVIDE, 0); 
     else if (input_ch == '*') 
      ptr = new_math_struct(MULTIPLY, 0); 
     else 
      ptr = new_math_struct(NUMBER, atoi(input_ptr)); 

     if (startNode == NULL) 
     { 
      startNode = previousNode = ptr; 
     } 
     else 
     { 
      previousNode->nextNode = ptr; 
      previousNode = ptr; 
     } 
    } 
    return 0; 
} 

math_struct_ptr* new_math_struct(int symbol, int value) 
{ 
    math_struct_ptr* ptr; 
    ptr = (math_struct_ptr*)malloc(sizeof(math_struct_ptr)); 
    ptr->type_of_value = symbol; 
    ptr->value = value; 
    ptr->sum = 0; 
    ptr->is_used = 0;  
    return ptr; 
} 

char get_input_character() 
{ 
    /* character, input by the user */ 
    char input_ch; 

    /* get the character */ 
    scanf("%c", &input_ch);  

    if (input_ch == '+' || input_ch == '-' || input_ch == '*' || input_ch == '/' || input_ch == ')') 
     return input_ch; // A special character 
    else if (input_ch == '\n') 
     return ','; // A carrage return 
    else if (input_ch < '0' || input_ch > '9') 
     return '.'; // Not a number   
    else 
     return input_ch; // Number 
} 

l'en-tête du fichier C contient uniquement une référence à l'en-tête struct et les définitions des fonctions. Language C.

Répondre

6

Puisque votre math_struct_ptr contient déjà le déclarateur de pointeur, vous n'avez pas besoin de le spécifier au point utile. Laissez tomber le *:

math_struct_ptr ptr; 

Ou écrire

struct math_struct *ptr; 
2

math_struct_ptr * est une math_struct ** - vous créez un pointeur vers un pointeur avec des étoiles imbriquées. L'avantage de votre typedef est que vous n'avez pas besoin de mettre un * après math_struct_ptr, donc vous pouvez simplement le laisser

1

Vous TypeDef

typedef struct math_struct* math_struct_ptr; 

Donc, si vous utilisez math_struct_ptr*, vous obtenez un pointeur vers un pointeur. Vous voulez probablement juste math_struct_ptr dans ce cas. Je ne cacherais pas le pointeur avec typedef, mais je vois que beaucoup de gens le font. Je trouve que c'est juste déroutant. C'est probablement une question de goût.

+0

C'est plus qu'une question de goût. 'const math_struct_ptr' et' const math_struct * 'sont deux choses différentes. – jamesdlin

0

Ils sont comme vous typedef décide, vous allez utiliser:

struct math_struct * * 

plutôt que

struct math_struct * 

Il suffit d'utiliser le math_struct_ptr TypeDef directement.

Questions connexes