   C Plus Plus pointers
  Hello all,So, I have been away from C Plus Plus for a bit while doing other college classes. I have recently decided to go back to an old project that I never quite figured out.Aside from the problem specifics, I was just curious about two different things1. int *width = 0;   int *height = 0;What exactly would those lines do? Do they create a variable on the heap or stack (or both)? Depending on which, what values are where?2. If you are accepting input for width and height and expect values of 2-50 inclusive, how can you type check that in C Plus Plus?Thanks!!   
  1. The pointers themselves are on the stack. They don't point to anything since they are set to 0 (NULL). But they can point to both objects on the stack and on the heap.2.if (x >= 2 && x <= 50);  1. they create a pointer on the stack and initialize it to point to the memory address 0.  There is no int variable created, just a pointer to an int.If you wanted to actually create the int you could do one of the following:int anInt = 5;int *width = &anInt; //the pointer width now points to the integer anInt.int *width = new int;  //the pointer width now points to the memory address   //returned by new of an int on the heap//beacuse you used new you have to use delete to ensure you don't//have a memory leak.  do this when you are done using widthdelete width;  //this releases the memory on the heap which was storing//the int to which width pointed-me   ;Quote:Original post by shawnreWhat exactly would those lines do? Do they create a variable on the heap or stack (or both)? Depending on which, what values are where?They create a variable that's not a int, but a pointer-to-int, and the pointer is on the stack. The pointer can point to variables on the heap or on the stack, but currently isn't pointing to anything. (You pointed it to 0, which is the same as NULL, meaning, nothing)Quote:If you are accepting input for width and height and expect values of 2-50 inclusive, how can you type check that in C Plus Plus?if(width < 2) width = 2;if(width > 50) width = 50;If you find yourself doing that frequently, you could just make it into a function. Something like:void Bind(int &value, int min, int max){    if(value < min)   width = min;    if(value > max)   width = max;};  Great, thanks for the answers. It does lead me to another question with regards to pointers however, well actually a couple questions.In regards to this line:int *width = 0;If it creates a pointer, and effectively it is NULL or pointing to address 0 (which has me a bit confused), but would it be better to just say:int *width;I have always thought it best to initialize any variables or even pointers upon declaration...is that a good practice or no?The second question would be, as far as pointers go, I have seen several instances of placing the "*" when declaring things. What are the differences with regards to the location of the "*" when creating a pointer, and does "white space" matter?   ;  I think I should clarify my second question with an example:(*width)*(width)If that clarifies it at all....maybe the parens are the difference in this case?   ;  You should always initialise pointers. There is a huge difference between a NULL pointer and an uninitialised pointer.Whitespace is unimportant in pointer declarations.// some people like this way because multiple declarations slightly cleaner.int *ptr = 0;// some people like this way because it makes the type look like "int*"int* ptr = 0;// a rare form, a compromise.int * ptr = 0;// I've never seen a non-beginner do thisint*ptr = 0;;  Rip-off, thanks for the clarification. I guess my question, the clarification I mean, was more geared towards the deref of the variable. Is it then the ()'s that make the difference there?For example:int i = 5;int *pI = &iWhat differences does say:cout << (*pI) << endl;cout << *(pI) << endl;make?I guess I can just try that in my compiler....blah, lol ;-)-Edit- Apparently makes no difference....   ;Quote:cout << (*pI) << endl;This is (imo) more clear, and will cause fewer ambiguities with surrounding code.   ;Quote:Original post by shawnreI think I should clarify my second question with an example:(*width)*(width)If that clarifies it at all....maybe the parens are the difference in this case?It's a matter of order of operations. I'll give an example:int x = 5;int * x_ptr = &x; // x_ptr is a stack variable that contains the address of the integer x(*x_ptr)++; // x now contains the integer 6*(x_ptr++); // this is bad code. We increment the pointer to point to an unknown address and then dereference it. Your program could crash, or, worse, appear to work correctly*x_ptr++; // not easy to tell what this does unless you remember the order of operations. Does it increment the pointer and then dereference it or does it dereference it and then increment the integer?