1. ## a quick question about pointers

after my question yesterday about passing a 2d string array to a function i thought i had better go back and review the chapter on pointers.
Code:
```int main()
{
int x, y;
int *p_x = &x, *p_y = &y; // declare pointers to x and y

x = 5;
printf("x = %d\n", x);
printf("p_x = %X\n", *p_x);

*p_x = 9; // makes x = 9
printf("x = %d\n", x);
printf("p_x = %X\n", *p_x);

y = 3;
*p_x = *p_y; // makes x the value of y
y = 6;

printf("p_x = %X\n", *p_x);
p_x = p_y;      //assigns the address of y to the pointer p_x
printf("p_x = %X\n", *p_x);

return 0;```
the comments are what i got when the program ran not what i expected to get.

question is if i have to designate a pointer by putting an astrix in front of the variables name ie *p_x rather than p_x how come when you use the point like in the above examples its the opposite.

many thanks
coop

2. Read Stroustrup's answer to the FAQ Is ``int* p;'' right or is ``int *p;'' right? That FAQ is about style (in C++, but comparing with C), but reading the motivations behind the style in terms of syntax and type will help you understand what this is about.

So, you will see that p_x is a pointer (i.e., its type is pointer to int), so *p_x is an int, and then when declaring it we effectively declare that *p_x is an int; and in so doing declare that p_x is an int*

3. "point like"?

4. I don't understand what you're asking.

5. i was asking that if i have to declare a point by using the astrix ie int *p_x a pointer to x. how come *p_x is the value of x and not the memory address. "*p_x" is the declared variable and is assigned the address of x by writing *p_x = &x or at least appears to be. so if i wanted to print the value of x using the pointer i would of thought printf("%d", p_x) but that appears to give the memory address. if i want to print x's value with the pointer i have to give it the full *p_x which was the variable we stored the address in.... and round we go lol

6. Originally Posted by cooper1200
how come *p_x is the value of x and not the memory address.
I get what you're asking now. The asterisk is thrown around in the syntax for pointers quite liberally and means different things in different places. In the variable declaration as part of the declarator (the things like * or [] along with the identifier), the * means "this is a pointer." A pointer variable itself is just an unsigned integer type (though may be larger than an unsigned int) that holds the address of what you're pointing to which is why you have to use the & address of operator. The value of that variable is the address. In order to follow that address, you have to use the unary * operator, the dereference operator. On the left-hand side of an assignment operator, this means "store the value from the expression on the right side in this memory location" and in all other places it means "read the value from this memory location."

That's the what, what about the why? I think the point was to always make you aware that you're dealing with a pointer. If you have int a, b, c, d, *e, f, g, it's easy to forget that e is a pointer. If e=10 somehow changes f because e pointed to f, it can be very difficult to track down a bug. But always having to say *e = 10 is a constant reminder that e points to something, that it changes something else. They could have made things transparent by using the opposite syntax and doing away with the -> operator, using the . operator everywhere, but I think that would have been a detriment to the language.

Originally Posted by cooper1200
"*p_x" is the declared variable and is assigned the address of x by writing *p_x = &x or at least appears to be. so if i wanted to print the value of x using the pointer i would of thought printf("%d", p_x) but that appears to give the memory address. if i want to print x's value with the pointer i have to give it the full *p_x which was the variable we stored the address in.... and round we go lol
Here's where I think your confusion might be. In this case, *p_x = &x is, I'm assuming, part of a declaration. In that case, * is part of the declarator, it's a different syntax than the * unary dereference operator. I didn't even realize this was inconsistent until you pointed it out, it's not something you'll notice as you continue to use C. If it really bothers you, declare and assign to pointers in different statements.

7. if its the way it is its the way it is. just so i have this right in my thick skull. *p_x + 1 increments the value of x by one?????

8. Well... no. *p_x + 1 is an expression that evaluates to the value of x + 1, assuming p_x points to x. *p_x++ increments the value of x, or *p_x += 1.

Just remember that in declarations *p_x = 0 assigns the value 0 to p_x, meaning the address stored in p_x will be 0. But in all other statements, *p_x = 0 assigns 0 to whatever p_x points to. You'll get this with practice, you just need to repeat the pattern a few times.

9. Originally Posted by cooper1200
"*p_x" is the declared variable
p_x is the declared variable, of type int *. A pointer to an int. You can see it like this (int *)p_x. In the declaration int *p_x = &x its the same if you do it in diferent statements: int *p_x; p_x = &x, cause the declared variable is p_x without the *. After the declaration, * becomes the indirection operator that you will want to use for access and store/retrieve values to/from the place where p_x(the declared variable) is pointing

10. Originally Posted by gaxio
*p_x++ increments the value of x, or *p_x += 1.
*p_x++ is equivalent to *(p_x++), i.e., it post-increments the pointer, not what the pointer points to.

EDIT:
Originally Posted by cooper1200
"*p_x" is the declared variable and is assigned the address of x by writing *p_x = &x or at least appears to be.
*p_x is the declared variable of type int according to the grammar. Semantically though, p_x is the declared variable of type int* (i.e., pointer to int; recall that * cannot be part of a variable name, so *p_x isn't really a "variable"). In an initialisation (i.e., it is part of a declaration, in particular the declaration that is the definition of the variable), you have the two worlds of declaration and expression colliding: so now even though according to the grammar it looks like you're declaring *p_x, because you want to initialise it, the semantics matters: you're initialising p_x, not *p_x, hence it is correct to use &x, since that results in a pointer to x.

11. Originally Posted by laserlight
*p_x++ is equivalent to *(p_x++), i.e., it post-increments the pointer, not what the pointer points to.
Oops, I certainly shouldn't be making mistakes like that when trying to explain things

This is a very common mistake, though. They've tried to eliminate in more modern languages. In Go, you can't even use ++ in expressions, and Rust doesn't even have ++ and -- operators.

12. Originally Posted by gaxio
Oops, I certainly shouldn't be making mistakes like that when trying to explain things

This is a very common mistake, though. They've tried to eliminate in more modern languages. In Go, you can't even use ++ in expressions, and Rust doesn't even have ++ and -- operators.
After a while everyone makes mistakes responding to questions - As long as you admit that it was a mistake and not double down no one really cares.