Mfw programming is addicting

>mfw programming is addicting

>being this new

I dont know man. Sometimes when something does not work, i really get friggin pissed.

That's good user. Hope you say that after battling a bug two weeks, being pushed by exes to deliver software unpolished that isn't even intresting

Enjoying what you do is key

>mfw using amphetamines while programming

today I spent an hour trying to figure out how to make a folder for some fonts on github
unrelated, do we brainlets have hope is we do still genuinely love learning?

...

...

i'm learning C and i really like it but pointers are kinda hard to wrap my head around. I also don't really see the point of casting as most of the C books say it's generally not a good idea to cast.

executives or executables or ex-girlfriends

In the same boat here. I know what pointers are but idk how it makes my life easier. You're correct on casts too. Everyone says they're bad but they use
int *foo == (foo*)malloc (sizeof (bar)* y)

or something like that. That cast is supposed to remove compiler error but idk

Exeguttors

>pointers are kinda hard to wrap my head around
they just point to an address. * gets the data there

From what i read they can be used to alter the values of variables from other functions. kinda like a global variable in python.

Remember when you were younger and you couldn't stop reading books? Programming is similar. You'll start to tire.

Pointers allow you to write functions that modify their arguments. That's how the scanf family of functions work. They also allow you to work with memory on the heap.

like supposedly i could do something like this"

int main(void) {
int *a;
a = 32;
doStuff();
return 0;
}

int doStuff(void) {
&a = 8;
return *a;
}

or something like that.

You not right in the head

No, a pointer is a reference to the location of something in memory. What you may be thinking of is passing a value into a function by reference by using a pointer to a variable as an argument to the function. This lets you modify the contents of that location in memory by dereferencing the pointer and doing something with it. What you have there is wrong because 'a' exists in two seperate scopes and 'a' isn't even declared in doStuff(). Additionally, you are setting a pointer to an int to the address of 32 in main, not the actual value of the int at that location.

10/10

yeah until you start trying to tackle thousands lines of code

So it's possible for me to pass a value to a function and let that function modify it's value for use in the calling function?

Pls gib example
This is what confuses me. The definition of pointers is easy but good lord it is so confusing to do stuff with it.

cd code

Pointers are a way to pass data around without making the CPU expend the time and effort to copy complex data structures onto the call stack. Calling a function and passing the struct copies it onto the stack. Using a pointer lets the function just access the struct on the heap, so it can be faster. It also allows the function to modify the struct because it has a pointer to the same struct that the caller has access to, not a copy of it. Pointers are also necessary to implement more sophisticated data structures.

For example, a linked list is a type of data structure similar to an array except it has no length constraints. You can tack on as many elements as you want without resizing it. An ordinary array is a contiguous block of memory, but a linked list has each piece of the list at a different memory address with a pointer to the next piece.

typedef struct node {
int val;
struct *node next;
}

You wouldn't even be able to define this data structure without pointers. Functions that operate on instances of this data structure would accept a pointer to the head element. For example:

// Returns the length of the linked list given a pointer to the head of the list.
int computeLength(node *head) {
if (!node) {
return 0;
}
return 1 + computeLength(node->next);
}

>Pls gib example
I wrote up like twenty lines and then realized I'm wasting my time. Go look it up.

Noob here. Whats the point in using them if I can operate with normal int, char, etc. All my programs work just fine and barely take up memory.

Close, you don't pass the value, you pass where the value is in memory. The callee can do stuff with that location and it will affect the value in the caller.

#include

void add_one(int *a)
{
*a = *a + 1;
}

void main(void)
{
int a = 1;
add_one(&a);
printf("%d\n", a);
}

Shit, I meant "if (!head) {" and "head->next".

Thanks for making it clear user.

read google "call by value vs call by reference"

thanks.

Interesting except I barely understood what you/user wrote but I do wanna know more. What's the best place for dumb nogs like me where I can learn about these call stacks n shiet? It seems like I skipped the basics of C.

google

In this case, when you pass something as parameter to a function you are really making a copy of the value passed in that the callee function can use. Passing by value means shoving the value of the variable in as a parameter. If you have:
void myFunc(int a);
...
int b = 2;
myFunc(b);

myFunc will only be using a copy of the value passed into the function. However, if you pass in a pointer to a place in memory like so:
myFunc2(int *a);
...
int c = 2;
myFunc2(&c);

You end up giving myFunc2 a copy of the memory address of 'c'. Because myFunc2 now knows the address of 'c', it can modify the value at the address and thus the original value of c rather then a copy passed in. This is passing by reference, as in reference to the original one.

Whoops I forgot to put void in front of the forward declaration for myFunc2.

I believe that if you aren't you're doing it wrong.

>Drinks 60mg Ritalin dissolved in 120mg coffee shots

>Code just happens

At that point you disconnect from reality and you're productivity goes through the roof

>tfw only interested in tech as far as it can get me a comfy desk job where I don't have to break my back and have energy to focus on my hobbies/interests in what free time I have

I'm not gonna make it huh?

I have ADD and even 20mg Ritalin makes me paranoid and anxious as fuck. How can you stand 60? Are you nuts?

Effect fades as you take more and more

It happens. But to be a programmer it helps to have an optimistic streak.

It'll compile this time ... fuck. Now it'll compile ... fuck. Okay, everything fixed this time ...

That's my secret kid I'm always paranoid

That feeling of relief when all of your test cases pass again after a major refactoring or feature addition is amazing.