![]() ![]() This is not useful for anything, except to declare function pointers (described later). Incidentally, C allows zero or more levels of parentheses around the variable name and asterisk: int (( not_a_pointer )), (* ptr_a ), (((* ptr_b ))) Just use your own judgment.įinally, I should point out that you can do this just fine: int * ptr_a, * ptr_b The absolute clearest is to keep every declaration on its own line, but that can take up a lot of vertical space. It's even clearer to put the non-pointer variables first: int ptr_b, * ptr_a This is the immediate improvement: int * ptr_a, ptr_b It's possible to do the single-line declaration in a clear way. Think of it as assigning each variable a base type (int), plus a level of indirection, indicated by the number of asterisks ( ptr_b's is zero ptr_a's is one). If you split the declaration of ptr_a and ptr_b into multiple statements, you get this: int * ptr_a int ptr_b It is not a pointer.Ĭ's declaration syntax ignores the pointer asterisks when carrying a type over to multiple declarations. Given this, what is the type of ptr_b? int *, right? then you can declare multiple int-pointer variables by simply giving the int-pointer type (int *) followed by a comma-separated list of names to use for the variables ( ptr_a, ptr_b).and a single statement can declare multiple variables of the same type by simply providing a comma-separated list ( ptr_a, ptr_b),.If the type of a variable containing a pointer to int is int *,. ![]() The obvious way to declare two pointer variables in a single statement is: int * ptr_a, ptr_b The use of pointers to pointers is called multiple indirection. An int **'s type is int * (it points to a pointer to int). Thus it is an 'int pointer' (a pointer to int). You could put a different pointer in the foo_ptr box, and the box would still be foo_ptr. The point of that is that the pointer is not the variable! The pointer to foo is the contents of foo_ptr. (Thus, 'pointer variable' really means 'variable of a pointer type'.) There are, however, variables with different types. In fact, grammatically speaking, there is no such thing as a 'pointer variable': all variables are the same. This is true of all variables, regardless of type. When you access the address, you actually access the contents of the box it points to. foo is a box that is sizeof ( int ) bytes in size. & foo is the address of foo (which is why & is called the 'address-of operator'). Its location in memory is called its address. We have initialised it to point to foo.Īs I said, foo occupies some memory. int * foo_ptr = & foo įoo_ptr is declared as a pointer to int. On a PowerPC, it occupies four bytes of memory (because an int is four bytes wide). ![]()
0 Comments
Leave a Reply. |