1

I have a struct TREE defined this way:

typedef struct TREE {
    NODE *head;
} TREE;

and a struct NODE defined as:

typedef struct NODE {
    char boss[30];
    char name[30];
    struct NODE *firstChild;
    struct NODE *secondChild;
    struct NODE *thirdChild;
    struct NODE *fourthChild;
} NODE;

In my main, I have:

TREE companyStructure;
TREE *treeptr;
treeptr = &companyStructure;
strcpy(treeptr->head->name, "Ben");

But this gives me a segmentation fault. Can someone help me explain why this is the case? Is there some memory management that I'm not doing that I need to be doing?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Nick
  • 95
  • 8
  • 3
    Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). You must not dereference `treeptr->head` before initializing it (`companyStructure.head`). – MikeCAT Aug 15 '16 at 14:28
  • Possible (too little possibility to vote for now, I think) duplicate of [c - Why do I get a mysterious crash or "segmentation fault" when I copy/scan data to an uninitialized pointer? - Stack Overflow](http://stackoverflow.com/questions/37549594/why-do-i-get-a-mysterious-crash-or-segmentation-fault-when-i-copy-scan-data-to) – MikeCAT Aug 15 '16 at 14:30
  • 1
    Where do you create the `NODE` structure that `treeptr->head` points to? Post that code, too. – Andrew Henle Aug 15 '16 at 14:30
  • Don't mix `struct ` and `typedef` names for the same type! That's just confusing to any reader. And use all-upercase names only for macros and enum-constants. – too honest for this site Aug 15 '16 at 14:31
  • 1
    It looks to me like the member of treeptr called head has not been initialized. You have to set companyStructure.head to something first. – mdewit Aug 15 '16 at 14:37
  • Thanks, all. My issue was, as suspected, just that I hadn't initialized companyStructure.head. – Nick Aug 15 '16 at 15:02

1 Answers1

2

Till,

treeptr = &companyStructure;

Things look good. But then considering the fact that you have

NODE *head;

you need to allocate memory for head. So most likely you've missed

treeptr->head = malloc(sizeof *treeptr->head);

before doing

strcpy(treeptr->head->name, "Ben");

Also, check [ this ] on why should you use strncat instead of strcpy.

Community
  • 1
  • 1
sjsam
  • 21,411
  • 5
  • 55
  • 102
  • 1
    Yeah, that was exactly what I was missing. As a novice with C, I understand how to use pointers but having it nested within the struct was muddying my thinking process. Thanks a lot. I'll check out the link. – Nick Aug 15 '16 at 15:08
  • @Nick : You're welcome, I'm sure you'd take no time to master it – sjsam Aug 15 '16 at 15:11
  • 2
    `strncpy` is not a safer version of `strcpy`. Never use this function, it does not do what you think it does. It is very error prone. You can use `strlcpy` for the purpose of truncating the copy to fit in the destination. `strncpy` does not do that. See http://en.wikipedia.org/wiki/Strlcpy – chqrlie Aug 15 '16 at 17:45
  • @chqrlie : Thankyou for the edit, appreciated. Agree with what you said, I just had the buffer overflow in mind but the other problems are apparent. – sjsam Aug 15 '16 at 17:51