![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
strings, pointers, arrays
alright, i've been bitching about how c makes you understand this concept. this MAY be helpful to newbies. two things i learned in college..
FIRST: you do NOT have to explicitly create a pointer variable. you can pass a function the address of a value and it will treat it like a pointer (our teacher says a pointer IS an address, instead of a variable that points to an address)...i did not know that (or think about it like that). i always thought you had to... {
int i = 1;
iPtr = &i;
function(iPtr);
}instead of {
int i = 1;
function(&i);
}SECOND: the other and BIGGEST thing is that for debugging just see if the left and right-hand operands match. sounds simple but we all f*** it up. if it's a pointer on the left than it has to be a pointer on the right. if the function takes a pointer to an array of pointers to structures then...etc. nothing new syntactically but the difference in the way you think helps A LOT.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#2 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
If your teacher is correct in saying a pointer IS an address, not a variable which holds an adress; then how can you explane the following:
#include <iostream>
int main(int argc, char *argv[])
{
int* pInt;
std::cout << "\nAddress of : " << &pInt;
std::cout << "\nPoints to : " << pInt;
std::cout << "\nSize of : " << sizeof(pInt) << " Bytes" << std::endl;
int Int;
pInt = ∬
std::cout << "\nAddress of : " << &pInt;
std::cout << "\nPoints to : " << pInt;
std::cout << "\nSize of : " << sizeof(pInt) << " Bytes" << std::endl;
std::cin.sync();
std::cin.get();
return 0;
}If you run that code you will see that pInt has a size, and a unique constant address separate from the address it points to. But the address it points to is not constant - it can be changed. So it seems (to me) that a pointer IS NOT an adress, and infact (in the case of an int*) is a 4Byte variable that can hold an address. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
A pointer is not an address, and it is not a variable that points to an address. A pointer is a variable and the value it contains is an address.
|
|
|
|
|
|
#4 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Quote:
EDIT: In my code above I used the words "Points to:" instead of "Value of:" so people didn't think I meant "Value pointed by". |
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
I wasn't disputing what you said Cache. I was just stating what a pointer is and isn't.
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
In some tutorials, you will read that a pointer doesn't have a value, it has a pointer. I have been taken to task for calling the contents (NULL included) a value. That's bullshit of course. It's sort of frowned on, but the value, the address, can often be interpreted as an index into a memory-sized array beginning at zero.
Blood's example of passing a pointer variable in one instance and "an address" in the other has implications: level of indirection from the viewpoint of the code inside the function. Quite often the compiler, which is built to recognize these things, will do different things under the skin for identical constructs on the surface. This leads, occassionally, to confusion in the mind of the coder.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 | ||
|
Newbie
Join Date: Feb 2006
Posts: 20
Rep Power: 0
![]() |
Quote:
I don't often hear the notion of pointers being explained as objects of a completely seperate data type, most often, tutorials explain pointers almost as if they are a subset of built-in types (perhaps because C++ treats them as compound types, although I believe some weakly typed languages accept "pure" pointers). This extra clarification might reduce the inevitable conflict of ideas that always arises when a beginner attempts to write a program using pointers. Quote:
|
||
|
|
|
|
|
#8 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Well, I actually misquoted. Here's a direct quote from the site linked to by the person (anonymous person, I might add) who took me to task:
Quote:
I think maybe those of us who were introduced to addressing modes in assembler before we were introduced to higher-level languages like C might not have as much difficulty with the concept as others.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#9 | |||
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
Quote:
I've sometimes been taken to task for using the term "pure" or "raw" pointer to mean the C pointer, which is a variable that contains a value that is an address. The reason I do that is that C++ also allows creation of a "smart" pointer, which is a particular type of object that acts as if it is a raw pointer, but also provides some additional capabilities. The std::auto_ptr template type in the STL is an example. It is literally a type of object that manages the lifetime of a ANOTHER dynamically created object who's address is contained in the pointer and, in practice. The standard containers in the STL (std::vector, etc) are other examples: the only difference is that the containers manage the lifetime of a set of dynamically created objects but, eventually when one gets down to implementation details, the lifetime of those dynamically created objects are managed using some type of "raw" pointer. In other words, the implementation details C++ notion of a "smart" pointer often work with C's notion of a "raw" pointer. Weakly typed languages support a notion of pointers which is somewhere between the concepts of a raw pointer and a smart pointer. Quote:
Saying a pointer contains a pointer is sort of like saying "a dog contains a dog". It doesn't always make sense. The circumstances in which such a statement makes sense are VERY limited (eg a pregnant bitch could be said to contain puppies) and those circumstances are not relevant for all dogs. |
|||
|
|
|
|
|
#10 |
|
Newbie
Join Date: Feb 2006
Posts: 20
Rep Power: 0
![]() |
I believe there is currently some discussion among members of the ISO C++ Standards Committee about precisely how the value of a null pointer should be written. In a recent usenet post, Bjarne Stroustrup commented that he and others are seriously considering a new built-in-constant of nullptr. Not that this will change anything in real terms, but it will at least cut the confusion.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|