Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9274179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:17:39+00:00 2026-06-18T16:17:39+00:00

So I had to create a bit of code that would make a list

  • 0

So I had to create a bit of code that would make a list and do various things to it. Specifically print it, sort it, and see if a value were in it. Did that, ran fine. Now I’ve got to take those functions and split them up into separate files, then use gcc -c (which I’m not super sure I’m using correctly) to get .o files, plus a .o for the test program. Then I have to use gcc to link my .o files together into an executable. The prompt says it will recognize the .o’s and realize how to link them.

So here are my questions:
Why is the below code returning errors( in ways which will be defined below)?
and
what exactly am I supposed to be writing into the command line to link these guys?

So the code is as follows:
(first the .h files, then the main .c file)

node.h

typedef struct Node{
    int data;
    struct Node *next;
    struct Node *prev;
}node;

print.h

#include<stdio.h>
#include"node.h"
void print(node *pointer){
    if (pointer == NULL){
        return;
    }

    printf("%d ",pointer->data);
    print(pointer->next);
}

init.h

#include<stdio.h>
#include"node.h"
int init(node *pointer,int find){
    pointer = pointer->next;

    while (pointer != NULL){
        if (pointer->data == find)//found find
        {
            printf("The data is in the list.");
            return 1;
        }
        pointer = pointer->next;// Search in the next node.
    }

    //find is not found
    printf("The data is not in the list.");
    return 0;
}

sort.h

#include<stdio.h>
#include"node.h"

void swap (node *x, node *y){
    int temp = x->data;
    x->data = y->data;
    y->data = temp;
}

void sort(node*pointer){
    int i;

    while (pointer->next != NULL){
        if (pointer->data>pointer->next->data){
            swap(pointer,pointer->next);
        }

        pointer = pointer->next;
        sort(pointer);
    }
}

list.c

#include<stdio.h>
#include<stdlib.h>
#include"node.h"
#lnclude"print.h"
#include"sort.h"
#include"init.h"
int i;
node *p;
node *n;

void insert(node *pointer, int data){
    //go through list till ya find the last node
    while (pointer->next != NULL){
        pointer = pointer->next;
    }

    //allocate memory for new node and put data in it
    pointer->next = (node *)malloc(sizeof(node));
    (pointer->next)->prev = pointer;
    pointer = pointer->next;
    pointer->data = data;
    pointer->next = NULL;
}

int main(){
    //start is used to point to the first node
    //temp is for the last node
    node *start, *temp;
    int z;
    start = (node *)malloc(sizeof(node));
    temp = new;
    temp->next = NULL;
    temp->prev = NULL;

    for (z = 0; z < 10; z++){
        insert(start,(3*10) - z);
    }

    init(start,12);
    init(start,3);
    init(start,27);
    init(start,7);
    print(start);
    sort(start);
    print(start);

}    

Now the code all ran perfectly fine when it was all together, with the exception of node.h (that was always a separate file). The .h files will compile perfectly, but when I try to compile the .c file it returns errors claiming that I am trying to redefine node in each .h file. Could this be because I am including it in each .h file?

I am also getting errors that I am trying to pass inappropriate arguments to the init function, which does not seem to the case. But I might just be over looking things.

Thank you in advance for any help.

EDIT: changed variable in main from new to start
Errors when typing “gcc list.c”

In file included from init.h:2:0,
                 from list.c:4:
node.h:1:16 error: redefinition of'struct Node'
node.h:1:16 note: originally defined here
node.h:5:2  error: conflicting types for 'node'
node.h:5:2  note: previous declaration of 'node' was here

In file included from sort.h:2:0;
                 from list.c:5:
node.h:1:16 error: redefinition of'struct Node'
node.h:1:16 note: originally defined here
node.h:5:2  error: conflicting types for 'node'
node.h:5:2  note: previous declaration of 'node' was here

list.c: In function 'main':
list.c:41:1: warning: passing argument 1 of 'init' from incompatible pointer type[enabled by default]
init.h:4:5: note: expected 'struct node *' but argument is of type 'struct node *'

(and then there are errors for each of the separate init calls in main)

list.c:45:1: warning:passing argument 1 of 'print' from incompatible pointer type [enabled by default]
print.h:3:6: note expected 'struct node *' but argument is of type 'struct node *' 

(and then there is another one for the second print function)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:17:40+00:00Added an answer on June 18, 2026 at 4:17 pm

    Put inclusion barriers in the .h files. Eg, for sort.h

    #ifndef INCLUDE_SORT_H
    #define INCLUDE_SORT_H
    
    // contents of file
    
    #endif
    

    EDIT

    Actually I just looked at your code more closely. You have defined functions in .h files which is not the thing to do. Really I see no reason to separate this into separate files at all.

    So combine them all into a single file and compile with:

    gcc -o list -Wall list.c
    

    If you really want separate files, then put the functions into C files and the structure and prototypes into a .h file (which you include into each C file). Then compile and link using something like:

    gcc -o list -Wall list.c node.c main.c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So basically the assignment was we had to create a doubly linked list that's
I'm having trouble understanding the following bit of code that I was hoping would
Last night I had a script go a bit crazy and create a bunch
If I had to create a content inventory for a website that doesn't have
I recently had an idea to create my own String class to make using
I had a program freeze (sql developer specifically) that had some unsaved files in
I had some code that worked fine removing punctuation/numbers using regular expressions in python,
I have the following code in front that gives a bit of blurb and
I am trying to make a modal object library that will create and open
OK, here is how to re-create the problem had: Create a new project, using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.