Solved

Some help with simple code

Posted on 2003-12-01
8
302 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

770 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