0

Having a hard time telling where an asterisk is related to a pointer.

Here are a few examples of code that are confusing me.

typedef struct X { int i_; double d_; } X;
X x;
X* p = &x;
p->d_ = 3.14159;  // dereference and access data member x.d_
(*p).d_ *= -1;    // another equivalent notation for accessing x.d_

On line five, we have (*p).d_ *= -1;. Why are there two asterisks? What does their position mean?

int x = 2;
int* p_x = &x;  // put the address of the x variable into the pointer p_x
*p_x = 4;       // change the memory at the address in p_x to be 4
assert(x == 4); // check x is now 4

On line 2int* p_x = &x;, we make a new pointer, but on line 3 *p_x = 4;, the pointer syntax is still using an asterisk. Why is this the case?

Credit: What does "dereferencing" a pointer mean? for code examples.

Note: Question was "In C pointer syntax, why are asterisks used intermittently?". I changed it to help future visitors.

Community
  • 1
  • 1
Bool1989
  • 11
  • 5
  • 1
    Just as English words can have different meanings in different contexts, the asterisk may mean different things. There is no single "translation" for the asterisk. By the way, the asterisk in `*=` has nothing to do with pointers. – M Oehm Dec 02 '15 at 08:01
  • 2
    The `*` in `*=` has nothing to do with pointers. That's just multiplication. – user2357112 Dec 02 '15 at 08:01

4 Answers4

5

(*p).mem is the same as p->mem
the * in *= is multiplication, its the multiply-assign operator, and has nothing to do with pointers

sp2danny
  • 7,488
  • 3
  • 31
  • 53
  • well, i feel stupid. Why do they use the asterisk for both pointers and multiplication? That seems like a good way to code harder to read. I think @ would be better for pointers, why don't they use it instead? – Bool1989 Dec 02 '15 at 08:09
  • hmm, I guess when I learn how to make a compiler, I will make a variant of C called @C that uses @ instead of * for pointers. It seems like It would be much more intuitive that way. – Bool1989 Dec 02 '15 at 08:17
  • @Bool1989 - There's one more case. Divide operation with pointer could look like a comment start if there's no space a = b /*p_x versus a = b / *p_x – rcgldr Dec 02 '15 at 10:28
1

On line 2int* p_x = &x;, we make a new pointer, but on line 3 *p_x = 4;, the pointer syntax is still using an asterisk. Why is this the case?

A pointer is simply a variable that holds the address to something else as its value. A non-pointer holds an immediate value. (e.g. int a=5;) A pointer on the other hand, in that example, would hold the address where the value 5 is located in memory (e.g. int *b = &a;).

If you want the value referenced by a, you simply provide a (e.g. int c = a;) However, with the pointer, if you simply attempted to assign c the value of b you would be assigning a memory address to c. To obtain the value at the address referenced by the pointer b, you must dereference b. Therefore to assign c the value pointed to by b you use the dereference operator '*' (e.g. int c = *b;).

The same holds true if you want to assign a new value to the memory address pointed to by b. If you wanted to replace the value for a (currently 5) but using the pointer b, you could do *b = 7; (which basically says replace the value at the address currently pointed to by b with 7.

Let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • So, quick question. Because the Answer was different than what I thought it would be. Should I change the Question to "How do I tell if the use of an asterisk is related to a pointer or not?"? It seems like it would be more inline what the answers are saying. – Bool1989 Dec 02 '15 at 08:33
  • 1
    You can. It can be helpful to future visitors that search for the same topic. good question, pointers are always something you just have to beat your head against enough times before the lightbulb winks one. – David C. Rankin Dec 02 '15 at 08:39
  • ok, I did it. feel free to edit it to make it look better. But only if you want to. – Bool1989 Dec 02 '15 at 08:49
0
int a = 10;
a *= -1;
printf("%d\n", a); /* -10 */

+=, -=, *=, /= and more...; they are called "compound assignment operators".

a *= -1 is a shorthand for a = a * -1.

On line 2 int* p_x = &x;, we make a new pointer, but on line 3 *p_x = 4;, the pointer syntax is still using an asterisk. Why is this the case?

Google for "pointers tutorial" and read some of them (especially ones with nice illustration.) For example

nodakai
  • 7,773
  • 3
  • 30
  • 60
0

1) Why are there two asterisks? What does their position mean?

It is similar to,

(*p).d_ = (*p).d_ * -1;

Just like to multiple a variable with some value

a = a * 1;

OR

a *= 1;

Pointers are variables which point to some memory location.

So,

int x = 2;
int* p_x = &x;  // put the address of the x variable into the pointer p_x
*p_x = 4;       // change the memory at the address in p_x to be 4
assert(x == 4); // check x is now 4

In this. Lets say value of x is stored in some memory. Pointer p_x is made to point to that memory by using the & operator on x. & gives the memory where the variable is stored.

Now, for a pointer, you can use it without the *, that would give you the address where it is pointing at.

If you use it with a * , it would dereference the pointer, meaning it would give the value which is stored in that memory location pointed by that pointer.

Haris
  • 12,120
  • 6
  • 43
  • 70