#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    [problems With Compilation]


    I'm having problems with this program:

    #include <stdio.h>
    #include <stdlib.h>

    int tamanho(struct node *head);

    int main(int argc, char *argv){

    struct node{
    int data;
    struct node* next;
    };

    struct node *head = NULL;
    struct node *dois = NULL;
    struct node *tres = NULL;

    head = malloc(sizeof(struct node));
    dois = malloc(sizeof(struct node));
    tres = malloc(sizeof(struct node));

    head->data = 1;
    head->next = dois;

    dois->data = 2;
    dois->next = tres;

    tres->data = 3;
    tres->next = NULL;

    printf("%d",tamanho(head));
    }

    int tamanho(struct node *head){

    struct node *aux = head;
    int cont = 0;

    while(aux!=NULL){
    cont++;
    aux = aux->next;
    }

    return cont;
    }

    In aux = aux->next the compiler tells that aux is an undefined struct node. Can anyone help plz????
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,393
    Rep Power
    1871
    1. Please use [code][/code] tags when posting code, so it looks like this.
    Code:
    struct node{
        int data;
        struct node* next;
    };
    Next, move those 4 lines I posted to be above your prototype for the function
    int tamanho(struct node *head);

    The problem is, your struct declaration (within main) is local only to main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    Tks.

IMN logo majestic logo threadwatch logo seochat tools logo