Solved

Some help with simple code

Posted on 2003-12-01
8
299 Views
Last Modified: 2010-04-15
I have been given some simple code to get me used to using structs to create linked lists.
The code I was given is :

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

struct node *node_1, *node_2;

node_1 = malloc (sizeof(struct node));
node_2 = malloc (sizeof(struct node));

node_1->next_ptr=node2;


This creates 2 structs and sets one struct to point to the other, e.g creating the linked list.

So I tried to make a program to test out the code as follows

#include <stdio.h>





int main()
{

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


struct node *node_1, *node_2;

node_1 = malloc (sizeof(struct node));
node_2 = malloc (sizeof(struct node));

node_1->next_ptr=node2;

  return (0) ;
}


But when I compile i get
test.c: In function `main':
test.c:16: two or more data types in declaration of `node_1'
test.c:16: two or more data types in declaration of `node_2'
test.c:18: warning: assignment makes pointer from integer without a cast
test.c:19: warning: assignment makes pointer from integer without a cast
test.c:21: `node2' undeclared (first use in this function)
test.c:21: (Each undeclared identifier is reported only once
test.c:21: for each function it appears in.)

I cant see where Im going wrong as I used the exact test code I was given


0
Comment
Question by:welsh_boy
  • 4
  • 3
8 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9853938
@welsh_boy:

> So I tried to make a program to test out the code as follows

Here are some corrections:
      #include <stdio.h>

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

      int main()
      {
            struct node *node_1, *node_2;

            node_1 = malloc (sizeof(struct node));
            node_2 = malloc (sizeof(struct node));

            node_1->next_ptr=node2;

            return (0) ;
      }

Hope That Helps,
Dex*
0
 
LVL 1

Expert Comment

by:Gratch06
ID: 9853941
struct node {
    int data;
    struct node *next_ptr;
};   //missing a semicolon here.
0
 

Author Comment

by:welsh_boy
ID: 9853963
thanks for the quick reply Dex

I still get

gcc test.c -o test
test.c: In function `main':
test.c:12: warning: assignment makes pointer from integer without a cast
test.c:13: warning: assignment makes pointer from integer without a cast
test.c:15: `node2' undeclared (first use in this function)
test.c:15: (Each undeclared identifier is reported only once
test.c:15: for each function it appears in.)
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9853977
@welsh_boy:

> I still get

More corrections:
Here are some corrections:
     #include <stdio.h>

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

     int main()
     {
          struct node *node_1, *node_2;

          node_1 = (struct node*)malloc (sizeof(struct node));
          node_2 = (struct node*)malloc (sizeof(struct node));

          node_1->next_ptr = node_2;

          return (0) ;
     }

Dex*
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:welsh_boy
ID: 9854006
Thanks Dex, that works now, could u go over what you did to fix it?

Cheers
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 125 total points
ID: 9854031
Sure.  It's a small list.

1) I moved the definition of the struct to be OUTSIDE of main()
2) I added a semi-colon to the end of the struct definition (you always need that).
3) I changed this:
     node_1->next_ptr = node2;

To this:
     node_1->next_ptr = node_2;

(Node2 isn't a valid name, but node_2 is).

4) I type casted the malloc values by adding (struct node*) to the front of them.  This just tells the compiler to return the returned values as pointers to structs.

I think that's it...

Dex*

0
 

Author Comment

by:welsh_boy
ID: 9854045
great,
thats cleared a lot up for me..

Cheers again
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9854063
You're welcome!  Glad to be of help!

Dex*
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now