Hi,

I'm having some very hard time with my header files when working with linked lists.
I'm not too sure what I need to define in them except for the prototype functions.
The way to create strucs within the header files confuse me.

This first file is the header for the file that works with the linked lists.

//
//  linked.h
//  
//
//  Created by  on 12-03-06.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#ifndef _linked_h
#define _linked_h
#define BOOLEAN int
struct NODE
{
    char username[50];
    char password[50];
    char usertype[50];
    struct NODE *next;

}*head = 0;
void createList();

BOOLEAN add(struct NODE *p);

BOOLEAN valid (char username[50], char password[50]); a

#endif

As you can see, I choose to declare the struct in the header file. Is there anything wrong with this?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linked.h"
#include "cipher.h"
#define MAXLENGTH 50
#define ENCRYPT 50
void createList(){
  static const char filename[] = "password.csv";
  FILE *file;
  file = fopen(filename, "r+");

  if (file == NULL){
    printf("NO PASSWORD FILE");
  exit(0);
	}
  size_t i=0, size = 1000;
  char line[MAXLENGTH];
  while(i<size && fgest(line,sizeof(line),file)){
     NODE *input;
    input = (NODE*) malloc(sizeof(NODE));
    sscanf(line, "%[^','],%[^','],%s", input->username, input->password, input->usertype);
    decrypt(input->username,ENCRYPT);
    decrypt(input->password,ENCRYPT);
    decrypt(input->usertype,ENCRYPT);
    if ( add(input) == 0){
      printf("Error: was unable to initialize password validation!!");
      exit(0);
    }
    ++i;
}

BOOLEAN add(struct NODE *p){
  struct NODE *temp1;
  temp1 = (NODE*) malloc(sizeof(NODE));
  temp1 = head;
  while(temp1->next !=NULL){
    temp1 = temp1->next;
  }
  struct NODE *temp2;
  temp2 = (NODE*)malloc(sizeof(node));
  temp2->username =p->username; //fill in later
  temp2->password =p->password; //fill in later
  temp2->usertype =p->usertype; //fill in later
  temp2->next = NULL;
  temp1->next = temp2;
} 

BOOLEAN valid(char username[50], char password[50]){
  struct NODE *temp1;
  temp1 = head;
  while (temp1 != 0){
     if((strcmp(temp1->username, username) == 0) && (strcmp(temp1->password, password) == 0)){
       return 1;
     }
     temp1 = temp1->next;
  }
  return 0;
}

I also constantly get errors on how NODE isn't declared. Also when i try to say
temp2->username =p->username;
temp2->password =p->password;
temp2->usertype =p->usertype;

I've been stuck on this for hours. :(
Any help is appreciated.
Thanks

Member Avatar for MonsieurPointer

First:

temp2->username =p->username;
temp2->password =p->password;
temp2->usertype =p->usertype;

will never work. You cannot just copy char arrays like that; what you are doing is attempting to assign memory addresses. To copy C-strings, you will need to use the strcpy function.

Second: what is a doing here? Is it a typo?

BOOLEAN valid (char username[50], char password[50]); a

Third: This statement

while(i<size && fgest(line,sizeof(line),file)){
NODE *input;

will not work, because you need to define NODE as struct NODE.

I would highly recommend using a typedef (if you have learned about that) which will help reduce the amount of typing (that way you will only have to write NODE and not struct NODE each time it is required).

Thanks!
I have a slight problem after fixing those problems.
It's starting to complain how my *head is declared in the header file. I include this header into 2 more .c files.
The solution that I tried for this was to take the *head = NULL out and replace it with

extern struct NODE *head;

and initialized it within the .c file. Now its giving me a error I've never seen before.

Undefined symbols for architecture x86_64:
  "_head", referenced from:
      _add in ccAyvsLn.o
      _valid in ccAyvsLn.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Member Avatar for MonsieurPointer

What is the reason for declaring *head in your header file? I'm asking, because it causes complications: since you defined it in the header file, and protected it using #ifdef/#endif, it will not be visible in your other source (C file).

Now a question out of curiosity: what kind of compiler / IDE are you using?

The reason I declared it there was because I wanted it to be available to all the functions within the .c file I posted.

The functions here are going to be used in another file that contains the main function.

I'm not sure exactly which compiler I'm using but its on Mac OSX and came with XCode.

Thanks for the link!

It compiles perfectly until I try the gcc login.o ...etc. step
It gives me the same error as before.

I'm not too sure what you mean by compiling two different programs, or both sources linked to each other.

In the linked.h file, I have changed the head into

extern struct NODE *head;

and have initialized it the way the link told me to but inside another file, login.c, in the main function.

head = NULL;

The reason I want to initialize head as a global is because I want to avoid passing it to the linked list functions every time they need to run.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.