The textbook i'm reading explains that pointers are variables which hold the starting address of another variable, and that they are defined with a type of data to point to. Why can you assign a pointer to be the address of a variable then? or rather not be an address if omitting the "&" should it not always hold the address if that's how pointers are defined?
-
Why do you use the C++ tag for a C question? – Jul 02 '17 at 15:44
-
I thought the two used very similar syntax, and it was suggested. :/ – Pixel Jul 02 '17 at 15:49
-
You need to read a [good book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Passer By Jul 02 '17 at 15:55
-
Thanks, for the link, if i continue with C i will, the textbook im using only uses it for examples of programming rather than for learning the language which is probably why its lacking in that quality. – Pixel Jul 02 '17 at 16:08
-
If you have questions about a piece of code, you need to provide the code. I don't know what book you're reading and I haven't seen the `ptr = var` code you're talking about, so I can't comment on why that is. – torstenvl Jul 02 '17 at 18:03
2 Answers
ptr
is the actual pointer, while *ptr
is whatever it is pointing at, so *ptr=&var
does not really make any sense, unless it's a pointer to a pointer. It's either ptr=&var
or *ptr=var
If you really want to assign a variable to a pointer, it is possible with casting. This compiles, but I cannot see any good reason to do something like this at all:
#include <stdio.h>
main()
{
int var=4;
int *ptr;
ptr = (int *)var;
printf("%d\n", *ptr);
}
The behavior is undefined. When I ran it, it segfaulted.

- 30,332
- 17
- 55
- 95
-
Thank you, if so why can pointer variables be assigned values of variables then? where you can have `ptr = variable1` instead of `ptr =&variable1` does this make the pointer behave as any other named variable? – Pixel Jul 02 '17 at 15:58
-
@Pixel: Because C puts the responsibility to code sane stuff on the programmer. Still any decent compiler would issue a warning (in most cases). – alk Jul 02 '17 at 16:00
-
Thanks a lot, the textbook im using has used both which really threw me off. – Pixel Jul 02 '17 at 16:01
-
@Pixel: There are situation where you want to store an address of something as a specific integer variable. But this is mentioned in the later chapters of any complete C-book, if ever. – alk Jul 02 '17 at 16:05
-
C was designed a long time ago, and some of the design choices were made in circumstances that are no longer current. The address of operator was needed to pass the address of an object rather than its value to a function at a time where function prototypes were optional and the ambiguity couldn't have been resolved from context. The same syntax was used for assignment, for consistency.
Note however that your proposed syntax simplification one could no longer distinguish these cases:
void *p;
void *q = &p; // make q point to the pointer p
void *q = p; // set q to the value of p
There are other potential syntax simplifications:
The
.
and->
operators for object and pointer dereferencing could be merged into a single operator.The
*
syntax for indirect function calls:(*fun)()
is reduncdant asfun()
is exactly equivalent... (note that you can write(****fun)()
too)

- 131,814
- 10
- 121
- 189
-
so the purpose of the `void *ptr = variable` syntax is something that would only be used for passing adresses between pointers? – Pixel Jul 02 '17 at 16:03
-
`void *ptr = variable;` is equivalent to `void *ptr;` followed by `ptr = variable;`. It is used to initialize the value of void pointer `ptr` with the value of `variable`. – chqrlie Jul 02 '17 at 16:05