-
Redeclaration Error
I wasted my whole day trying to find this silly error but didn't get through
I am trying to compile
Code:
/*
file: filesearch.c
*/
#include <string.h>
#include <windows.h>
#include "filesearch.h"
#include "stack.h"
void getCurrentPath(char *path)
{
GetCurrentDirectory(MAX_PATH,path);
}
void searchpath(char *path,char *pattern,struct strData **filestack)
{
HANDLE hsearch;
WIN32_FIND_DATA FileData;
char tempdata[MAX_PATH];
struct stkData *dirstack,tempstack;
initstack(&dirstack);
initstack(filestack);
if(SetCurrentDirectory(path)){
if((hsearch=FindFirstFile(pattern,&FileData)) != INVALID_HANDLE_VALUE){
do{
if(!(FileData.cFileName == "." || FileData.cFileName == "..")){
if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
strncpy(tempstack.data,FileData.cFileName,strlen(FileData.cFileName));
push(&dirstack,tempstack);
}else{
strncpy(tempstack.data,FileData.cFileName,strlen(FileData.cFileName));
push(filestack,tempstack);
}
}
}while(FindNextFile(hsearch, &FileData));
FindClose(hsearch);
}
while(!isEmpty(&dirstack)){
tempstack = pop(&dirstack);
searchpath(tempstack.data,pattern,filestack);
}
}
}
these are the header files
Code:
#ifndef _FILESEARCH_H
#define _FILESEARCH_H
void getCurrentPath(char *);
void searchpath(char *,char *,struct strData **);
#endif
Code:
#ifndef _STACK_H
#define _STACK_H
#define PATHLENGTH 260
struct stkData{
char data[PATHLENGTH];
int status;
struct stkData *next;
};
void initstack(struct stkDat..........);
int isEmpty(struct stkDat..........);
void push(struct stkDat..........,struct stkData);
struct stkData pop(struct stkDat..........);
#endif
I am getting this error
Code:
D:\Project\C\stack>lcc filesearch.c
Error filesearch.c: 14 redeclaration of 'searchpath' previously declared at d:\project\c\stack\filesearch.h 4
1 errors, 0 warnings
-
what is a struct strData?
-
Given the name of the parameter, I guess "strData" should have been "stkData"
> FileData.cFileName == "." || FileData.cFileName == ".."
This is not how you compare strings in C
> strncpy(tempstack.data,FileData.cFileName,strlen(F ileData.cFileName));
This isn't going to append a \0 on to the end of the buffer.
Nor is it going to protect you from buffer overflow.
strncpy(tempstack.data,FileData.cFileName,sizeof(t empstack.data));
tempstack.data[sizeof(tempstack.data)-1] = '\0';
> push(&dirstack,tempstack);
I hope this copies tempstack to something you allocate, otherwise all your stack elements are going to be referring to the same local variable.
-
-
>I hope this copies tempstack to something you allocate, otherwise all your stack elements are going to be referring to the same local variable
I think I am doing right
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"stack.h"
void initstack(struct stkData **top){
*top = NULL;
}
int isEmpty(struct stkData **top){
return (*top == NULL);
}
void push(struct stkData **top,struct stkData data){
struct stkData *newdata;
newdata = malloc(sizeof(struct stkData));
strcpy(newdata->data,data.data);
newdata->status = data.status;
newdata->next = *top;
*top = newdata;
}
struct stkData pop(struct stkData **top){
struct stkData *temp, temp2;
isEmpty(top);
temp = *top;
temp2 = *temp;
*top = (*top)->next;
free(temp->data);
free(temp);
return temp2;
}
Tell me is there any glitch in this code.
-
Yeah, your pop() function is way off.
For example
- calling isEmpty() without testing the result
- calling free on something which was never allocated
-