Richard said:
I'm not trying to solve a problem. I'm just trying to understand C.
In a function without pointer arguments, the argument(s) of that function
are set by value passing:
f(int i)
{
printf("%d", i);
}
int main()
{
int i;
f(i);
return 0;
}
I just don't understand why this is (a little bit) different with pointer
arguments.
It's not different at all really.
Grab:
1. A piece of paper.
2. A pen.
3. A house on a street.
The piece of paper is now your pointer variable, (int *p for example).
The pen is your assignment operator.
The house is your int object (int house for example).
But aside from that, if you want to tell anyone how to get to your house
do you pass them the whole house? I wouldn't think so (but you never
know). So what do you do? You write it down on a piece of paper. No, you
do not write down the house, you write down the address of the house.
Now your friends can come over after you give them that piece of paper.
Can they do anything to your house, like enter it, set it on fire just
by having your piece of paper? No they can't, they have to go to your
house first.
So essentially, the house has (at least) two aspects to it, the house
itself and its address. You have to make it clear which you want. With
variables in C it's the same thing. They have a value and an address.
You have to be clear on which you want. The & operator seems apt in
telling the compiler what it is you require. The piece of paper as well,
you have to be clear on whether you want to change the piece of paper
or the thing that is at the address it holds. In other words, how do you
tell your program to set fire to the piece of paper, how about whatever
is at the address written down? Well you simply say, set fire to this
piece of paper, or in the latter case you say set fire to what resides
at the address written down on this piece of paper.
See, the second statement a bit complex than the first. So should
require some more syntax as well. Also the syntax makes sense when
you consider that pointers are completely normal variables. When you
want something else than the value stored in a variable then you
need to tell the compiler that.
HTH.