I need ideas for C project

it is a lot faster than python lol

I did and this is what I got:

typedef struct node {
    int val;
    struct node * next;
} node_t;
node_t * head = NULL;
head = (node_t *) malloc(sizeof(node_t));
if (head == NULL) {
    return 1;
}

head->val = 1;
head->next = NULL;

Can somebody explain this to me? Specifically the asterick and malloc?

The * is usually used to show that something is a pointer to an instance of something, I think

why don’t you move this to a separate thread and we can talk more about it?

Um, should I call not-ethan?

no like make a new post asking questions about the code

malloc stands for memory allocate, but I’m not really sure how to explain it. As @9pfs1 said, the * symbol is for using pointers which are also kinda weird. I’m not good enough at C to explain it so that’s all I can really say about it.

okay well I guess I’ll explain it here

typedef struct node creates a struct, (think class in python – it’s not really, but it’s similar)

In it, it includes a val, which stores an integer (an item in the list), and struct node*, which is a pointer to the next item in the list.

Then, we create a new list node_t* head = NULL;
We set the value by allocating a certain amount of memory (the size of the type of node – this is how you are supposed to do it) by doing
head = (node_t*) malloc(sizeof(node_t));
you don’t have to cast it with (node_t*), but in this case they do. read this for more on casting malloc.

if (head == NULL) {
    return 1;
}

is optional, this just checks if malloc succeeded, because if it didn’t it would return a null pointer. This would happen if you ran out of memory in a program.

Then,


head->val = 1;
head->next = NULL;

sets the struct’s values. If you wanted to add another item, I guess you would do

node_t* next_item = malloc(sizeof(node_t));
head->next = next_item;

next_item->val = 1;
next_item->next = NULL;

It’s worth noting that trying to use null pointers will usually crash programs.

no it won’t

only if you dereference it will it segfault

That fits my definition of use.

int* a = NULL;

is this using

1 Like

No, it definitely is not.

Nah, Assembly cannot be compared to binary.

1 Like

wait are you saying w3schools isn’t good, or an IRC server

w3schools isn’t the best, but IRC is

at everything, not just C might I note.

wdym w3schools isn’t the best. you liar!

@RandomDreamWalker201
A programming language

Maybe a text adventure?