2

I have a project file in C where I declare a structure in file.H, and 2 files includes file.h,
the content of .H file:

#ifndef SORT_H
#define SORT_H

struct lnode {
 int data;
 struct lnode *next;
} *head, *visit;

void add(struct lnode **q, int num);


/*typedef struct lnode NODE; */



#endif

++++ Content of ADD.C +++++

#include "SORT.H"

void add(struct lnode **q, int num)
{
     struct lnode *temp;

     temp = *q;

     /* if the list is empty, create first node */
     if(*q == "") {
      *q = malloc(sizeof(struct lnode));
       temp = *q;
     } else {
      /* go to last node */
      while(temp->next != "")
       temp = temp->next;

       /* add node at the end */
       temp->next = malloc(sizeof(struct lnode));
       temp = temp->next;
     }

     /* assign data to the last node */
     temp->data = num;
     temp->next = "";
}

++++ Content of SORT.C ++++

#include "SORT.h"
#include<conio.h>
#include<stdio.h>

void main(){

    struct lnode *newnode = NULL;
    char choice;
    int lim, i = 0, num;

    clrscr();

     /*get max value*/
     printf("Input no. of values: ");
     scanf("%d",&lim);

     for(i = 0; i <= lim; i++){
        printf("[%i] = ",i);
        scanf("%d",&num);
        add(&newnode, num);
     }


    head = newnode;
    for(;;){
        printf("\n\n\nMENU: \n");
        printf("[A]Selection Sort\n[B]Insertion Sort\n[C]Exchange Sort\nChoice: ");
        scanf("%s",&choice);

        switch(toupper(choice)){
            case 'A':
                      clrscr();
                      printf("Selection Sort: \n");
                      break;
            case 'B':
                      clrscr();
                     printf("Insertion Sort: \n");

                      break;
            case 'C':
                      clrscr();
                        printf("Exchange Sort: \n");
                      break;
            case 'D':
                      exit();
                      break;
            default:
                      clrscr();
                      printf("Incorrect choice!\n\n\n");
                      break;
        }
    }
}

And i get the linker_error: _head and _visit is being duplicated from file1.c to file2.c.

and i need this structure in my 2 files, anyway for me to do this, also if you need more information just say so.

hushpuppies
  • 57
  • 1
  • 6

1 Answers1

2

Variables head and visit are defined as global variables in the header file which is included in two files. Hence they are double defined. You will need to declare those vars in the header and define them in one of .c files.

#ifndef SORT_H
#define SORT_H

struct lnode {
   int data;
   struct lnode *next;
};

extern struct lnode  *head, *visit;
...
#endif

and in SORT.C

struct lnode  *head, *visit;
Marian
  • 7,402
  • 2
  • 22
  • 34
  • thanks this worked for me, though why re declare the struct inside the store.c file? thanks – hushpuppies Jan 12 '14 at 08:47
  • @hushpuppies In the C file `struct lnode *foo` defines foo as being a pointer to struct lnode. In the .h file you declare what `struct lnode` is – Brandin Jan 12 '14 at 11:00