smttsp
smttsp

Reputation: 4191

what is the meaning of (void*)&

I have a piece of code saying

clock.start();
for (unordered_map <uint32_t,HostTraffic>::iterator internalIPItr = hostTraffic.begin();
     internalIPItr != hostTraffic.end();
     ++internalIPItr)
  {
     if (!pgBulkInserter.insert(NULL, internalIPItr -> first,
                                internalIPItr -> second.inboundFlows,
                                (void*)&(internalIPItr -> second.outboundPortIPs),
                                internalIPItr -> second.roles)) {
        return -1;
     }
     clock.incrementOperations();
  }

My problem is I don't understand the meaning of

(void*)&(internalIPItr -> second.outboundPortIPs).

You can consider for(....) as

for (int internalItr = beginning -> end)

where the type of internalItr is unordered_map and internalItr->second gives an instance of HostTraffic.

Upvotes: 1

Views: 2460

Answers (4)

OmnipotentEntity
OmnipotentEntity

Reputation: 17131

Already answered is what it does (returns a void*)

But you should be forewarned this this type of casting is called Type Punning and it's considered Undefined Behavior. (Though, it works in all of the compilers I know with the appropriate compile options set or unset.)

Upvotes: 2

user529758
user529758

Reputation:

internalPltr is a pointer to a structure, wich has a member named second which has a member named outboundPortIPs. The & (addressof) operator returns its memory address, then (void *) casts (performs an explicit type conversion on) that address to a void pointer.

Upvotes: 1

kol
kol

Reputation: 28688

The pointer to the address of internalIPItr->second.outboundPortIPs, given by &, is converted to void*.

Upvotes: 3

simonc
simonc

Reputation: 42165

You're passing a pointer to internalIPItr->second.outboundPortIPs to a function that takes void*.

The & takes the address of internalIPItr->second.outboundPortIPs (i.e. gives you a pointer to it). (void*) casts that pointer to void.

Upvotes: 1

Related Questions