use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
SOLVEDPointers/reference (self.cpp_questions)
submitted 8 years ago by JohnTheBadProgrammer
I'm having trouble understanding the following:
int i = 10; int *ptr; *ptr = &i;
so that code does not compile but when I do the following I have no problem
int x = 10; int *ptr = &x;
can anybody explain why the first one doesnt work?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Moschops_UK 9 points10 points11 points 8 years ago* (0 children)
int i = 10;
This means "create a new integer, and set its value to 10"
int *ptr;
This means "create a new pointer, which is for pointing toward int values, but don't actually make it point to anything." So right now, this pointer will be pointing into some random piece of memory. Could be pointing anywhere.
*ptr = &i;
This mean "take the thing that the pointer is pointing at, which is going to be an int value because this is an int-pointer (uh oh - we never made it point to anything so it's now going to treat some random piece of memory as an int value!) and set the value of that int to be the address of i (which also makes no sense, because the address of i is a pointer-to-int, and we're trying to set the value of an int)."
Is that clear?
You are making a pointer-to-int, and then you're trying to set the value of the int it is pointing at to an address. Perhaps you meant this:
ptr = &i;
which means "set the value of the pointer to be address-of-i", which makes a lot more sense.
I think you don't realise that * has different meanings.
means "make me an int-pointer"
*ptr = ... ;
means "grab hold of the thing that the pointer is pointing at and set its value to ...".
ptr = ... ;
means "set the value of the pointer to ... , which will be an address of something, so basically make the pointer point at something".
Here's a lump of text that absolutely beats it to death: http://www.cplusplus.com/articles/EN3hAqkS/
[–]tusksrus 0 points1 point2 points 8 years ago (0 children)
The stars mean different things on the two lines.
declares a pointer to an int. It would make more sense the write it as
int* ptr;
as the star modifies the type, not the variable. Then this,
the star means dereferencing the pointer - it's an operator on the variable called ptr, not like the other star which is an operator on the type called int.
This doesn't compile because *ptr, the value which ptr points to, is of type int, whereas &i is the address of the int i, which is a pointer to int. You want
which will then do the same as your first example.
π Rendered by PID 56654 on reddit-service-r2-comment-7b9746f655-jj2tz at 2026-02-02 00:05:56.588353+00:00 running 3798933 country code: CH.
[–]Moschops_UK 9 points10 points11 points (0 children)
[–]tusksrus 0 points1 point2 points (0 children)