[ home ] [ q / soy / qa / r / nate ] [ a / health / incel / int / mtv / pol / psy / raid / sci ] [ overboard ] [ rules ] [ bans ] [ wiki ] [ booru ] [ archive ] [ merch ] [ donate ]

/sci/ - Soyence and Technology

Name
Options
Comment
File
Embed
Password (For file deletion.)

File: 1690667906682.png (141.81 KB, 800x789, 45707 - SoyBooru.png)ImgOps

 10528

Please explain me pointers in C (with referencing and dereferencing).

 10529

also forgot to say when should I use them and why?

 10541

up

 10542

Marge

 10544

>>10542
What are you confused about, are you retarded?

 10545

>>10544
he likes to say buzzwords and doesnt know what it means

 10584

pointers are numbers that represent memory addresses. Dereferencing a pointer gets the value of memory at the address.

 10599

Let's say you go to work and you have to leave a grocery list for your trans girlfriend.
Let's also say you are autistic. You take a piece of paper and write a number 3 on it and place it in the closet, then you take another piece of paper and you write
int* POINTER_1 = in_closet;
int APPLE_NUM = *POINTER_1;
where in_closet is the location of the first paper. POINTER_1 simply shows WHERE something is. Dereferencing a pointer (*POINTER_1) means you need to go to the place described in POINTER_1 and read the value stored there (first paper).

 10601

* = value inside variable is a pointer to memory
& = address of variable is referenced

did I get this wrong chuds

 10610

>>10599
it's int * not int*. This may sound pedantic because it is but in C the pointer refers to the name not the type.

 10613

>>10601
Think about * and & as functions.
*var will return var's value (if var is an address in memory).
&var2 will return var2's address.
However you shouldn't confuse * in variable declarations with dereferencing.
int * var; just means var is a pointer.

 10638

up

 10639

>>10599
Gemmy example but wouldn't APPLE_NUM just hold the memory

 10640


 10685

heading to page 1

 10699

But why would you use pointers? Whats the gain over just referencing the original variable?

 10703

>>10699
Let's say you are passing a variable into a function and want said to function to change the variable.
If you just pass it by value (without pointer), the function would copy the value into a separate variable, use it in the function, and destroy it when you exist from function.
Copying variable into another variable is pretty slow and memory expensive (if you pass a 1gb structure by value, the program will use 2gb of memory when the function is executing). Also the only way to retrieve the changed value from function is to return it in the end, this means you pretty much can only change 1 variable in function, because C only lets you return 1 variable (or you would have to pack all those random variables into a different structure which is just retarded and tedious)

 11183

>>10703
Ah! So it reduces memory usage and effectively makes a variable global?

 12465

>>11183
yes. There are more advantages and some disadvantages to it, but this is the most widespread reason to use it.

 13488

up or something

 13490

>>10703
why would it copy itself in another variable without pointers?
like i still acces the variable under the same name, where does the original value ends?
Marge im kinda confused.

 13693

>>10528
thread currently heading to page 1

 13704

>>13490
If you read the disassembly of your program, you can see that args larger than 8 bytes (register size) are copied onto the stack. Pointers are at most 8 bytes, so they can fit in registers.



[Return][Go to top] Catalog [Post a Reply]
Delete Post [ ]
[ home ] [ q / soy / qa / r / nate ] [ a / health / incel / int / mtv / pol / psy / raid / sci ] [ overboard ] [ rules ] [ bans ] [ wiki ] [ booru ] [ archive ] [ merch ] [ donate ]