tags:

views:

189

answers:

7

Hi Folks, Just a quick question for folks who know a bit of c. Whats the significane fo a * at the start of an expression. as in...

If (this == thisThingOverHere)
ThisThing = *((WORD *) &Array[withThisPosition]);

You can assume WORD is a 16-bit unsigned and Array is an 8 bit byte array. Its surprisingly difficult to try and find out what is going on here.

Cheers

+6  A: 

dereference a pointer and get the corresponding value.

example:

#include <stdio.h>

int main(int argc, char **argv) { 
     int *i_ptr;
     int i, j;

     i = 5;
     i_ptr = &i;

     j = *i_ptr;

     printf("%d\n", j);
     return 0;
}

prints 5

In C, you have memory locations (think kind of "boxes"), and values go inside them. Each memory location has an address, and you can store this address into a "pointer variable", which is declared with the type, followed by a *. For example, i_ptr is pointer to integer (int *), while i and j are integers. This means that in memory, three "boxes" have been allocated, two will be ready to contain actual numbers (i and j) and one (i_ptr) will be ready to contain an address to another box, and you inform the compiler that the referred box will contain a number.

with & you get the address of the box from the box name: &i gives you the address of i, and you can store it into i_ptr.

When you have the address of the box and you want to to visit the box and get the value out you use *, e.g. *i_ptr gives you the value contained into the box at the address contained into i_ptr.

Stefano Borini
+2  A: 

this->value is the same as *(this).value it's to dereference the pointer.

JonH
+8  A: 
PP
"It's not that difficult to work out what is happening. Let's break it down."It is if you don't know what the operator is. And I believe his question was "What is this operator?"...
Pod
Touchy, Pod. Okay, for someone who hasn't spent a full hour in their lives learning C syntax, you're right. That star sure is tricky to understand! I hope my break down helps you to understand. I believe I did answer that.. perhaps you could read the answer before commenting. Thank you!
PP
Cheers PP, you were correct to assume i dont know my arse from my elbow when it comes to c. I'm trying to transfer this to java.
Nick
Ah, I see your Array is an 8-bit type. Possibly this contains a buffer from a network communication and the endian-type is being modified. Hence the view of two bytes as a single word. You should be aware of the architecture of your system (i.e. Intel chipsets typically are little-endian) as that will have a big influence on how the WORD is calculated.
PP
+1  A: 

C has an important and powerful concept of pointers. You should really learn about them - a good tutorial is available here.

If ptr is a pointer, then *ptr is the value it points to.

Eli Bendersky
A: 

You have some missmatched parens in your expression. I am assuming you meant:

ThisThing = *((WORD *) &Array[withThisPosition]);

the

(WORD *) &Array[withThisPosition]

part is of type pointer to WORD. The leading * deferences the pointer. So

*((WORD *) &Array[withThisPosition])

is of type WORD.

rschuler
+1  A: 

It's probably easiest to look at the expression in pieces, then put them together.

&Array[withThisPosition]

This much gets the address of the element at withThisPosition in the array Array. If you chose to, you could write it as: Array+withThisPosition instead.

(WORD *)

This it saying to take whatever comes after it (the bit above) and treat it as a pointer to a WORD.

The initial *, then says to dereference that pointer -- i.e. look at whatever it points at.

Overall, this is finding a position (withThisPosition) in Array, and looking at a WORD at that address, and assigning the value of that word to ThisThing.

Jerry Coffin
+1  A: 

The use of * in C is a little confusing because it has two different but related meanings. Your example is confusing because BOTH of those meanings are used in the same line of code.

When immediately adjacent to a type, the * is a modifier that indicates you are referring to a pointer to that type. This is most commonly seen in variable declarations, but can be used in casts as well, as you see here with (WORD *).

When not used with a type, * is a unary operator that dereferences a pointer. In other words, it takes the value of the operand, treats it as a memory location, and returns the contents of that location. The operator also determines what variable type the operand is pointer to, and cast the value at the memory location to that type. The compiler will usually do checking to ensure that the operand is in fact a pointer. In this case, the compiler knows &Array[withThisPosition]) is a pointer to a byte, because Array is an array of bytes.

Getting back to your question, USUALLY when * is at the beginning of a C statement, it is the dereference operator, but I won't guarantee this is always the case.

Pointers gave me way fewer headaches once I separated the two meanings of * in my brain.

Ben Gartner