-5

in the file compiler is not allocating proper memory to the struct variable. on debugging it is raising EXC_BAD_ACCESS (code=1, address=0x8) error.

updated problem detailsi.stack.imgur.com/32HcW.png

  • 4
    You never initialized any of the pointers. – Barmar May 18 '20 at 07:33
  • 5
    Please post code, errors, sample data or textual output here as plain-text, not as images that can be hard to read, can’t be copy-pasted to help test code or use in answers, and are barrier to those who depend on screen readers. You can edit your question to add the code in the body of your question. For easy formatting use the `{}` button to mark blocks of code, or indent with four spaces for the same effect. The contents of a **screenshot can’t be searched, run as code, or copied and edited to create a solution.** – tadman May 18 '20 at 07:34
  • 1
    Could you post text instead of the image? – glglgl May 18 '20 at 07:34
  • 2
    It's not the compiler's job to allocate memory. As a C programmer that's *your* job. Use `malloc` or `calloc`. – tadman May 18 '20 at 07:35
  • 2
    1) Don't post images of code. 2) Find a tutorial on dynamic allocation. – klutt May 18 '20 at 07:40
  • sorry guys, i didnt explained by problem well but i am trying again to properly explain my problem. Actually with the same defination for defining variable head2 memory gets allocated but when i use same syntax again to define variable head below it valid memory is not allocated. – dipesh digwal May 20 '20 at 08:32

1 Answers1

0

You never initialized head, so it points to some random location. You should do something along the lines of head = (doubly_linked_list*)malloc(sizeof(doubly_linked_list)) first. This will allocate a new doubly_linked_list struct and assign it to head. And of course you probably need to free it later

  • Why do you suggest to [cast the result of `malloc()`](https://stackoverflow.com/q/605845/296974)? – glglgl May 18 '20 at 07:39
  • @glglgl Because `malloc` returns `void*` and here `doubly_linked_list*` is needed – Yaroslav Fyodorov May 18 '20 at 07:42
  • 1
    The cast is not needed – klutt May 18 '20 at 07:45
  • 1
    @YaroslavFyodorov Well, people are basically free to vote however they want. Personally I would not say that adding unnecessary clutter and duplication is a matter of style. Or if it is, then it's bad and good style. Furthermore, it would be much better to write `head = malloc(sizeof *head)` instead. I can also imagine that you got downvoted because this is a pretty poor question and answering them encourages such questions. – klutt May 18 '20 at 07:57
  • 1
    @klutt Ok, lesson learned - skip stupid beginners questions (parse as you like). I was always taught to cast malloc's result but I guess I never wrote in the really hardcore C. Also I never even saw this syntax `malloc(sizeof *head)`. TIL. – Yaroslav Fyodorov May 18 '20 at 08:08
  • 1
    @YaroslavFyodorov The only case where you need the cast is if you're using a C++ compiler to compile your C code. But then you're coding C++ and not C. The syntax with `sizeof *head` is vastly superior. It will automatically be correct in all situations. And well, I was only suggesting the bad question as a reason for downvote. I don't know for sure if that were the case. – klutt May 18 '20 at 08:15