Solved

Some help with simple code

Posted on 2003-12-01
8
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Digital Output does not have continuity to ground? 3 124
Unable to start eclipse ? 17 174
Windows Updates failing due to Diskpart not configured correctly 8 156
asp.net web app 3 58
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

738 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