Roger Costello
Roger Costello

Reputation: 3209

What does *(int *) variable = value mean?

I am reading the online book Object-Oriented Programming with ANSCI-C

Page 6 shows an implementation of a set. There is an add() function that takes two parameters: a set and an element. The element is added to the set. The implementation uses an array of pointers:

static int heap [MANY];

Here is the beginning of the add() function:

void * add (void * _set, const void * _element)
{ 
    int * set = _set;
    const int * element = _element;

    if (* element == MANY)
        * (int *) element = set — heap;

It is that last line:

* (int *) element = set — heap;

that I do not understand. It appears that the right-hand side is subtracting pointers, is that right? Is that common to subtract pointers? On the left-hand side it appears that (int *) is casting element to a "pointer to an int", is that right? The star at the beginning appears to indicate "the value of the thing being pointed at", is that right?

Sorry, I am lost. What is the code saying?

Update (a question based on the responses)

Thank you for the outstanding responses!

I have a follow-up question about this expression: set - heap

heap is the name of an array. set is one of the cells in heap, say, heap[2]. So, the expression is subtracting the pointer to heap from the pointer to heap[2], is that right? I used a printf() statement to output heap and output set, here's what it output:

heap = 4227136
set  = 4227140

So, set - heap equals 4. Does that mean set is pointing at heap[4] (i.e., 4 cells from the start of the array)? Is the pointer to heap[i+1] always guaranteed to be 1 more than the pointer to heap[i]?

I output the value of element after this statement was executed:

* (int *) element = set — heap;

As just discussed, the value of set - heap is 4. The output says the value of element is 1. How can that be, shouldn't the value of element be 4?

Upvotes: 7

Views: 486

Answers (2)

P.P
P.P

Reputation: 121427

Since *element is a const int, it can't directly be assigned.

So the cast

    * (int *) element = set — heap;

converts element to int * and then dereferences, then assigns the result of set - heap.

However, this is only valid if:

  • the original object that _element points to is a modifiable object. Otherwise, casting away the const is undefined behaviour.
  • that points to an int object.

Yes, set - heap is subtracting two pointers. But it could be invalid because:

  • the result of subtracting two pointer yields a ptrdiff_t which may or may not fit in an int type.

  • the subtraction is only valid if the two pointers point to the same array object or one past the last element of the array object. See 6.5.6 Additive operators/9

In summary, the code does what you're alluding to with your questions. But it needs to meet the conditions noted above to be valid.

Upvotes: 5

klutt
klutt

Reputation: 31459

It appears that the right-hand side is subtracting pointers, is that right?

Yes

Is that common to subtract pointers?

Yes

On the left-hand side it appears that (int *) is casting element to a "pointer to an int", is that right?

Yes. The reason to do that is to override the const qualifier. This is a VERY dangerous thing to do.

The star at the beginning appears to indicate "the value of the thing being pointed at", is that right?

Yes

Upvotes: 3

Related Questions