C code problem, help!?

Hi, I am new to the C syntax but have experience in Java. Could someone check what is wrong with the syntax in my create_dir_tree function?

struct dir {

char *path; /* path to get to this dir, not including this */

char *name; /* current dir name */

int d; /* 3 <= depth level < MAXD */

/* the init_d is depth 0, its immediate subdirs are depth 1, etc. */

int b; /* 3 <= breadth or number of sub dirs < MAXB */

struct dir *up; /* points to the parent */

struct dir **down; /* points to the sub dirs of this dir. Note the TWO *'s */

struct dir *left; /* left sibling dir, use circular link */

struct dir *right; /* right sibling dir, use circular link */

};

int main(int argc, char **argv) {

struct dir *hp;

int b,d,n,flag;

char *init_d,tst_dir[100];

if (argc < 4) {

printf("usage: hw3 root_d d b\n");

return 1;

}

argv++; /* skip the command itself */

init_d = *(argv++);

d = atoi(*argv++);

b = atoi(*argv++);

create_dir_tree(&hp,init_d,d,b);

traverse_dir_tree(hp);

n = find_total_no_dirs(hp);

strcpy(tst_dir,"root_d/a/bb/ccc/dddd");

if ((flag = find_dir(hp,tst_dir)))

printf("dir %s found\n",tst_dir);

else

printf("no dir %s found\n",tst_dir);

return 0;

}

/* create directories breadth first */

void create_dir_tree(struct dir **hp,char *name,int d, int b) {

printf("create_dir_tree init_d=%s depth=%d breadth=%d\n",name,d,b);

(*hp)=malloc(sizeof(struct dir));

(*hp)->path = NULL;

(*hp)->name = name;

(*hp)->d = 0;

(*hp)->b = 1;

(*hp)->up = NULL;

(*hp)->left=NULL;

(*hp)->right=NULL;

struct dir **open;

struct dir *cp;

struct dir **tp;

open=hp;

int i;

for(i=0;i<d;i++){

while(open){

tp=open;

int j;

cp=*open;

for(j=0;j<b;j++){

printf("depth=%d breadth=%d\n",i,j);

struct dir *tmp=malloc(sizeof(struct dir));

tp++;

tp=&tmp;

tmp->d=i;

tmp->b=j;

if(j==0){

cp->down=&tmp;

}

else{

(*(cp->down))->right=tmp;

cp->down=&tmp;

free(tmp);

}

}

printf("made it out depth=%d breadth=%d\n",i,j);

open++;

}

}

}

For some reason the spacing is fine when I ask the question but can't seem to make it look nicer after asking the question. Thanks for any help.

1 Answer

Relevance
  • 8 years ago
    Favorite Answer

    Which line is flagged by the compiler as having a syntax error?

    Without trying to pretend I was able to follow your code, this line stood out:

    if ((flag = find_dir(hp,tst_dir)))

    Did you mean to use the equality operator ==, or did you intend to assign the value of flag within that expression? As it, it will assign flag with the return value of find_dir, and the expression will resolve to that value.

Still have questions? Get your answers by asking now.