0

I am rather new to C and have been following a book on C algorithms. A small section has provided details on adding a node to the beginning.

For example:

Algorithm:ADD_BEG (HEAD, ITEM)
1. [Create the new node]
a) Allocate memory for NEW node.
b) IF NEW = NULL then Print: “Memory not Available” and Return
c) Set NEW→DATA = ITEM
d) Set NEW→LINK = HEAD
2. [Make the HEADER to point to the NEW node]
Set HEAD = NEW
3. Return

Here is what I have attempted:

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

typedef struct node {
    int DATA;
    struct node * LINK;
} node_t;

node_t add_beg(struct node* head, int item){
    node_t* new =  NULL;
    new = (node_t *)malloc(5*sizeof(node_t));
    if (new == NULL){
        printf("Memory not available");
        return *new;
    }
    new ->DATA = item;
    new ->LINK = head;

    head = new;
    return *head;
}

Output:

gcc -o exercise_1 exercise_1.c

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What does the error refer to?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Emil11
  • 199
  • 9
  • 2
    You need a function called `main` as the entry point for your program. – Retired Ninja Dec 27 '22 at 20:37
  • [What are the valid signatures for C's main() function?](https://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function) shows what it should look like, and [How to compile C source code without a main function?](https://stackoverflow.com/questions/5764298/how-to-compile-c-source-code-without-a-main-function) talks about how to compile without one so you can link that code to a complete program later. – Retired Ninja Dec 27 '22 at 20:39
  • O.T. Are you sure that you want to `return` a `node_t` instead of a pointer to it? – David Ranieri Dec 27 '22 at 20:50
  • No need to initialize new to NULL when you assign it right after to the result of `malloc()`. You don't need the cast `(node_t *)`. Prefer using the variable rather than type to sizeof, i.e. `sizeof(*new)`. `new == NULL` is usually written simply as `!new`. Dereference new when it's null will segfault. Consider returning `node_t *` instead. – Allan Wind Dec 27 '22 at 20:51
  • A few side issues ... (1) You're leaking memory. You allocate `new` from `malloc` but then you discard that. (2) Returning `*new` when `new` is `NULL` dereferences a `NULL` pointer--this is UB. (3) You want to change `add_node` to return a `node_t *` (a _pointer_) instead of an _instance_ (4) You allocate 5 elements instead of just 1. – Craig Estey Dec 27 '22 at 20:56
  • Style guide: the dot `.` and arrow `->` operators bind very tightly because they are [postfix operators](http://port70.net/~nsz/c/c11/n1570.html#6.5.2.3). They should not be written with spaces around them. Writing `new ->LINK` is not idiomatic C and indicates that the coder is a tyro (newbie). Use `new->LINK`. (Also, be cautious about using `new` as a variable name. It is a reserved word in C++, which might make life more complex later, when using `new_node` would avoid any trouble. Not everyone would agree with this — hence "be cautious about using" rather than "do not use".) – Jonathan Leffler Dec 27 '22 at 21:51

0 Answers0