0

Essentially I am having a very basic problem... A little new at C. I'm making a node structure that stores a name (string) and its type (char, either 'D' or 'F'). The string works fine, the char doesn't seem to.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


char line[128];
char command[16], pathname[64];
char dirname[64], basename[64];

typedef struct {
  char name[64];
  char type;
  struct NODE *childPtr, *siblingPtr, *parentPtr;
} NODE;

NODE *root, *cwd;

initialize(){
  root = malloc(sizeof(NODE));
  strcpy(root->name, "/");
  root->type = 'D';
}  

main()
{
  while(1){
    printf("Input a command: ");
    gets(line);
    printf("Command inputed -> %s\n", line);
    printf("Root's name -> %s\n", root->name);
    printf("Root's type -> %c\n", root->type);
  }
}

Now when I do this, it prints out the name just fine, but Segmentation Faults on the line that should print the type. I also tried defining type with "root.type = 'D';" as well.

EDIT: Copy pasted exact code now. Some things have no use since I am just testing the first part of it, still a work in progress.

bock.steve
  • 221
  • 2
  • 12

3 Answers3

1

You have an error when declaring your struct... You misspelled struct as 'strcut'.

You also need to do

root = malloc(sizeof(*root));
...
free(root);//should be the last line
return 0;

EDIT: DON'T USE gets() ever, it is almost certain that you will run into problems...

EDIT 2 : function initialize() should be given return type void. Then you have to call the function in main. Should look like this.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


char line[128];
char command[16], pathname[64];
char dirname[64], basename[64];

typedef struct {
  char name[64];
  char type;
  struct NODE *childPtr, *siblingPtr, *parentPtr;
} NODE;

NODE *root, *cwd;

void initialize(){
  root = malloc(sizeof(*root));
  strcpy(root->name, "/");
  root->type = 'D';
}

main()
{
    initialize();
  while(1){
    printf("Input a command: ");
    fgets(line,128, stdin);
    printf("Command inputed -> %s\n", line);
    printf("Root's name -> %s\n", root->name);
    printf("Root's type -> %c\n", root->type);
    if (line[0] == 'q' && strlen(line)==2)break;//added so I can break loop....
  }
  free(root);
}
LearningCODE
  • 165
  • 1
  • 2
  • 13
  • That doesn't answer the actual question. Put it as a comment by all means but not an answer. – kaylum Sep 17 '15 at 22:34
  • @AlanAu I cannot comment... i would have but I do not have the reputation yet... thanks for the downvote – LearningCODE Sep 17 '15 at 22:41
  • Upvote for `gets()`. and for stating that all memory should be freed at some point. It may not answer the question, but it is useful information nonetheless. – dreamlax Sep 17 '15 at 22:44
  • I didn't downvote. But the rules are there for a reason. If you haven't got enough rep for comments then the correct response is not to work around the rules by posting non-answers as answers. The correct response is to get more rep points and not comment until you do. – kaylum Sep 17 '15 at 22:45
  • actually, the inclusion of the call to `initialize()` is the answer to the problem – user3629249 Sep 19 '15 at 07:18
-1

you where missing some header files and you misspelled struct, you should also free the allocated memory for the structure.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char name[64];
    char type;
} NODE;

NODE *root;

int main()
{
    root = malloc(sizeof(NODE));
    strcpy(root->name, "test");
    root->type = 'D';
    printf("Name = %s\n", root->name);
    printf("Type = %c\n", root->type);
    free(root);
    return 0;
}
kyle k
  • 5,134
  • 10
  • 31
  • 45
  • I didn't include the headers when i typed this up (didn't copy paste, on seperate comp), and struct is correctly spelled in the code, was just a typo here. – bock.steve Sep 17 '15 at 22:30
  • All valid problems you have identified. But how do any of those cause a seg fault? – kaylum Sep 17 '15 at 22:31
-3

typedef strcut should be struct --> that should be a compilation error

and that should do it

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • No to 2, read this: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Fantastic Mr Fox Sep 17 '15 at 22:30
  • @Ben That is very strange. That was what I learned at the university. Also the compiler gives me an error for not casting, though I included stdlib, care to explain? – CIsForCookies Sep 17 '15 at 22:32
  • 1
    @CIsForCookies `C` does not require `malloc` result to be cast. But `C++` does. – kaylum Sep 17 '15 at 22:35
  • Was about to rewrite that. I compiled it on g++ instead of gcc and thought it wouldn't matter. thx. BTH, can you explain why C++ require but C doesn't? – CIsForCookies Sep 17 '15 at 22:38
  • 1
    @ClsForCookies It is simply a fundamental difference between C and C++. In C, implicit conversion from pointer to void to another pointer type is allowed, but C++ introduces a lot of ways to increase type safety, there is little need to store things in `void*` when compared to C. – dreamlax Sep 17 '15 at 22:41