1

I have below code. I am getting error as 'list' undeclared (first use in this function). Please help me

#include <stdio.h>
#include <stdlib.h>
struct list{
int data;
struct list *next;
};
typedef struct list *head;

int main()
{
    struct list *start;
    int i;

    start = (list *) malloc(sizeof(struct list));
    printf("\nEnter the data : \n");
    scanf("%d", &i);
    start->data = i;
    start->next = NULL;
    while(list->next != NULL)
    {
        printf("%d ", list->data);
        list = list->next;
    }

    return 0;
}
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122

7 Answers7

3

You're using the type list instead of variable name start. Proper code:

while (start->next != NULL)
{
    start = start->next;
    // etc.
}
3

Don't cast the return type of malloc - there's no benefit of it and in this case you did it wrong!

start = (list *) malloc(sizeof(struct list));

should be

start = malloc(sizeof(struct list));

the type list * doesn't exist; you meant struct list *.

You can make it even more safe by writing

start = malloc(sizeof(*start));

this way you automatically malloc enough bytes for the (pointer) type of start, which is useful when you later change the type of start - the malloc call doesn't change a bit.

Community
  • 1
  • 1
Anthales
  • 1,158
  • 6
  • 10
1

The

start = (list *) malloc(sizeof(struct list));

includes an unnecessary typecast. Just do

start = malloc(sizeof(struct list));

However, your code has more trouble than this. I can answer your question best by asking a question of my own: in your mind, is list a type or an object?

If you answer this question, one suspects that you can fix your code. Good luck.

thb
  • 13,796
  • 3
  • 40
  • 68
1
#include <stdio.h>
#include <stdlib.h>
typedef struct list{
    int data;
    struct list *next;
} list;

typedef struct list *head;

int main()
{
    struct list *start;
    int i;

    start = (list *) malloc(sizeof(struct list));
    printf("\nEnter the data : \n");
    scanf("%d", &i);
    start->data = i;
    start->next = NULL;
    while(start->next != NULL)
    {
        start = start->next;
    }

    return 0;
}

you can define the type (list *)

ehanoc
  • 2,187
  • 18
  • 23
1

Your variable is named « start » and you called it « list ».

md5
  • 23,373
  • 3
  • 44
  • 93
0
start = (struct list *) malloc ...

You've missed struct in casting. Which is not neccessary at all as Anthales pointed out.

start = malloc ...
Agent_L
  • 4,960
  • 28
  • 30
0

One problem you had is you were re-using struct in declaring your struct pointer after you had created a typedef, struct list *start;. Also the struct and typedef cannot have the same name. You get this:

cc  -Wall test.c -o test
test.c: In function ‘main’:
test.c:13: error: ‘list_t’ undeclared (first use in this function)
test.c:13: error: (Each undeclared identifier is reported only once
test.c:13: error: for each function it appears in.)
test.c:13: error: ‘start’ undeclared (first use in this function)
test.c:13: error: ‘cur’ undeclared (first use in this function)
test.c:13: warning: left-hand operand of comma expression has no effect
test.c:16: error: expected expression before ‘)’ token

You can choose to use struct list everywhere and skip making using typedef. Use of typedef simplifies how your code reads as noted here: http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29#typedef

I've rewritten what you have just so I could compile it and understand it a little better, and so I could put some data into one node. I remember the whole struct typedef concept taking a little time to sink in, when I was learning C. So, don't give up.

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

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

typedef struct list list_t;

int main()
{
    list_t *start, *cur;
    int i;

    start = (list_t *) malloc(sizeof(list_t));

    if (NULL != start)
    {
        cur = start; /* Preserve list head, and assign to cur for list trarversal. */
        printf("\nEnter the data : ");
        scanf("%d", &i);
        cur->data = i;
        cur->next = NULL;
        cur = start;
        while(cur != NULL)
        {
            printf("%d ", cur->data);
            cur = cur->next;
        }
    }
    else
    {
        printf("Malloc failed. Program ending.");
    }

    return 0;
}
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131