The Horror of pointers...

Joined
Jan 11, 2025
Messages
4
Reaction score
0
Hello People,

I'm quite a bit struggling with Pointers. I just can't get my head around the whole matter of it.
The studybooks start to explain it from a startof point which is already beyond my knowledge so I was lagging behind right from the start.

Of course, after drilling myself by reading the chapters over and over again, along with helpfull youtube content, there eventualy is a certain basic knowledge on this in my head now, but I need some clarification or ratification of my assumptions and that I understand things right.

Also, if one has links to proven to be good Youtube content on this, I would be gratefull.
I found a video by an Indian woman explaining it in a way I could manage somewhat. But the accent, even though only less than normaly noticable, was distracting in a way I therefor needed a fraction of a second to understand the sentence, before being able to catch up.
:confused:

I have a piece of code which contains a few pointer thingy's. So something to tear appart to see what it is doing.
Can someone explain step by step what is going on here?

#include <stdio.h>
void twofold (int *single, int *duble); //"double" is a prohibited word. Therefor "duble"
void twofold (int *single, int *duble)
{
*duble = 2 * *single;
*single = 4 * *duble;
}
void main()
{
int n=3, result ;
twofold (&n, &result);
printf("%6d %6d", n, result);
n = n+1;
twofold(&n, &result);
printf("%6d %6d", n, result);
}
 
Joined
Sep 21, 2022
Messages
216
Reaction score
32
My C has been rusting for about 30 years, so I might make a mistake here. I was taught C and 8086 assembly at the same time. Assembly is needed to get a complete understanding of C. (It was at the time.)
Code:
01 void twofold (int *single, int *duble)
single is defined as a pointer (*) to an int.
The first argument should be the address of an integer, you can manipulate it with the pointer variable named single.
duble is defined as a pointer (*) to an int.
The second argument should be the address of an integer, you can manipulate it with the pointer variable named duble.
Code:
02 {
03 *duble = 2 * *single;
Multiply what single is pointing at by 2, assign the result to what duble is pointing at.
Code:
04 *single = 4 * *duble;
Multiply what duble is pointing at by 4, assign the result to what single is pointing at.
Code:
05 }
06 void main()
07 {
08 int n=3, result ;
09 twofold (&n, &result);
Call twofold, the first argument is the address (&) of n, the second argument is the address (&) of result.
Code:
10 printf("%6d %6d", n, result);
11 n = n+1;
12 twofold(&n, &result);
13 printf("%6d %6d", n, result);
}

A quick desk check:
08 n=3 result=?
09 calling twofold
03 n=3 result=6
04 n=24 result=6
10 print
11 n=25 result=6
12 calling twofold
03 n=25 result=50
04 n=200 result=50
13 print
The important thing to understand at the beginning is how to print the value of a variable. Then, if something unexpected is happening in your program, you can write a small program that does one thing, checking your assumptions.
 
Joined
Sep 4, 2022
Messages
143
Reaction score
16
hello ,

by C/C++ and so 'pointers' : you are handling 'memory adresses'.

remenber 'pointers' grant you to compute at 'memory' level.
It's really memory slots uses when you invokate 'pointers / adresses / references' .

It's calling 'left' value, do you already hear about that ?

where in one var :
name = "random string" ;

you have the left value 'name' , and the right value 'random string'.

Pointers deal with 'left value' by using 'memory adresses' , not the 'name' left value, but the adress of 'name'.

Pointers are closer to the memory !
 
Joined
Jan 11, 2025
Messages
4
Reaction score
0
I kinda gottit the way WhiteCube explained.

Although I had major trouble figuring it out properly, I left out the detail that it does not physically transfer the data of a variable outside the loop, but rather gives the address to the function so that the function knows where the values should be adjusted . address.
The * indicates that the variable is a pointer (address).
When using the & the value of the contents of the pointer is viewed.
Is this vision correct?

By leaving out the fact it is the address passed trough and just focussing on the idea of using variables, passing them on to another part of the code, and to manupulate them before returning them, it is easier to manage in my head.
When I have that skill under control, I might go one step back in understanding they are adresses, not physical chunks of data.
 
Joined
Sep 4, 2022
Messages
143
Reaction score
16
It's some 'air' required when approaching new insight about coding.
Pointers are another 'access ways' to vars.


'var name' lead to 'var value'
'pointer' lead to 'var value' , but faster. shorter path by pointer, less computing , less operations.
At this point, both look same.

'var name' is 'an adress' , 'the adress' leads to 'var value'. ( 3 elements involved )
'pointer' leads to 'var value'. ( 2 elements ).

by 'pointer' , you reach the 'var value' in 1 step.
by 'var name' , you reach the 'var value' in 2 steps.


It's at 'ASM and hardware level' you will see the 'how' , and the 'gain' too.

using vars by name, is invokating 3 parts ( name -> adress --> value )
by pointers , you use 2 parts ( adress -> value )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,466
Latest member
DrusillaYa

Latest Threads

Top