Reputation: 659
I have to questions about pointers; one theoretical and one practical.
Why, when declaring a pointer in C, must I preface the *var with a type. If a pointer is simply a variable that contains a memory address why does the compiler/language need any more information than that it is a pointer. What is the difference between an int * and a char *. Does this imply that an int * is pointing a some location that contains 2-4 bytes of memory and a char * contains only 1? I have never read anything about the underlying reasoning for why the type matters if ultimately it is a variable pointing to some hexadecimal number as evidence by %p printing them.
Secondly, given this code
int t = 10;
int *i = &t;
int *j = i;
--
*i == 10;
*j == 10;
Why is it that *j is equal to 10 instead of **j being 10? If j is a pointer to i which is a pointer to t which is 10 don't i need to double dereference the variable j? When writing this code in xcode it forces me to use *j.
So these are a few examples of confusion I have had with pointers.
Upvotes: 0
Views: 64
Reputation: 12047
C is a statically typed language. There are other languages, like JavaScript for example, which are dynamically typed, where you can assign objects of different types to the same variable.
Both approaches have their advantages and disadvantages, the most important advantage of statically typed languages is that many errors can be caught at compilation.
It's a design decision.
Upvotes: 0
Reputation: 5773
The datatype is required in order to know how many bytes to read when the pointer is dereferenced.
int *i = &t;
Here, the value stored in i
is the address of t
.
int *j = i;
So now, the value stored in j
is the value stored in i
which is the address of t
. If you wanted to do a double dereference, you'd need to store the address of i
.
int **j = &i
Upvotes: 3
Reputation: 26599
why does the compiler/language need any more information than that it is a pointer
To use the pointer only, the compiler doesn't. The type void*
means "a pointer to anything".
However, to use the value that is being pointed to, the compiler needs to know the type of what is being pointed to, so that it knows what can be done with it. Dereferencing a void*
will cause a compiler error, unless you first cast it to a typed pointer.
Secondly, given this code
j
is not a pointer to i
. The assignment int *j = i;
sets j
to the same value as i
, which is the address to t
(so j
would now point to t
).
To make j
a pointer to i
, you would need to declare it as int **j = &i;
Upvotes: 2
Reputation: 106092
why does the compiler/language need any more information than that it is a pointer.
That's because different data types are of different size and size of data types are needed to allocate memory.
What is the difference between an int * and a char *.
int *
is a pointer to int
and char *
is a pointer to a char
.
Why is it that *j is equal to 10 instead of **j being 10? If j is a pointer to i which is a pointer to t which is 10 don't i need to double dereference the variable j?
int *j = i;
tells the compiler that declare j
as pointer to int
and point this pointer to the memory location where pointer i
points to. Therefore j
is a pointer to variable t
.
Upvotes: 0