Solved

Some help with simple code

Posted on 2003-12-01
8
301 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
Save on storage to protect fatherhood memories

You're the dad who has everything. This Father's Day, make sure your family memories are protected. My Passport Ultra has automatic backup and password protection to keep your cherished photos and videos safe. With up to 3TB, you have plenty of room to hold the adventures ahead.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 415
reading tzdatabase for timezone definitions 5 129
Why does module make constant click noise ? 9 125
C#, VS15, StructLayout 1 123
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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

863 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

23 Experts available now in Live!

Get 1:1 Help Now