Solved

Some help with simple code

Posted on 2003-12-01
8
303 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use pointers 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.

828 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