gusgxrha
gusgxrha

Reputation: 195

The good old beginnerkiller: Pointers

I am currently studying C and reached the point (haha...) where I am learning about pointers. I think I know a bit about them already and I think I get the concept of them.

If I have a pointer named "c" and an integer named "a" with the value of 5 and I do the following:

*c = a;

I set the value of the pointer c (because I am using the asterik symbol) to the value of a, which is 5. So *c is 5 after that and c is equal to the memory address of a - Correct?

What about the following then:

c = &a;

I just pass the memory address in which the value of a is stored to the pointer. Are both operations equal? From my point of view they do the same - Is that correct?

Upvotes: 0

Views: 104

Answers (4)

Hassan Ali
Hassan Ali

Reputation: 1

If you are using *c=a it will assign 5 to memory address where c is pointing, and if you change value of a it will not affected to value of c.

While if you use c=&a than c started to pointing to memory address of a and of you changed the value of a it will change the value where c is pointing(*c).

Upvotes: 0

drRobertz
drRobertz

Reputation: 3638

If I have a pointer named "c" and an integer named "a" with the value of 5 and I do the following:

*c = a; I set the value of the pointer c (because I am using the asterik symbol) to the value of a, which is 5. So *c is 5 after that and c is equal to the memory address of a - Correct?

No, the contents of the variable that c is pointing to will get the value of the variable a. The pointer itself is not changed.

What about the following then:

c = &a; I just pass the memory address in which the value of a is stored to the pointer.

This changes the pointer c itself to point to the variable a.

Are both operations equal? From my point of view they do the same - Is that correct?

No, the first changes the value of whatever c points to, the second one changes what the pointer points to.

An important difference is that the first (*c = a) requires that c is valid -- i.e. actually points to an object). The second one makes c a valid pointer, overwriting its previous value.

Upvotes: 1

giant_teapot
giant_teapot

Reputation: 717

*c = a;

You will end up with this:

+---+     +---+    +---+
| A |     | C |    | ? |
+---+     +---+    +---+
| 5 |     | @ |    | 5 |
+---+     +-+-+    +-+-+
            |        ^
            |        |
            +--------+

Howerver with:

c = &a;

you'll end up with:

+---+     +---+
| A |     | C |
+---+     +---+
| 5 |     | @ |
+---+     +-+-+
  ^         |
  |         |
  +---------+

So in in both cases, you'll have *c == 5, but what differs is what c points to.

Upvotes: 4

FELIPE_RIBAS
FELIPE_RIBAS

Reputation: 411

Almost. Keep in mind that the first one you are saying: "take the portion of memory where 'c' points to and makes it equal 5". Therefore, you need to assign which portion of memory is that, before setting it to 5. So you actually have two different variables ('a' and the one 'c' points to).

In the second case you are just assigning 'c' to point to 'a'. So yea, you will again point to a value 5 but now you have only one variable 'a' and a pointer pointing to that same space of memory

Upvotes: 1

Related Questions