Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 14th, 2006, 12:03 AM   #1
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Mar 14th, 2006, 2:16 AM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
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 = &Int;
	
	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.
Cache is offline   Reply With Quote
Old Mar 14th, 2006, 2:58 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Mar 14th, 2006, 3:01 AM   #4
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by grumpy
A pointer is a variable and the value it contains is an address.
Thats exactly what I meant by "a 4Byte variable that can hold an address".

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".
Cache is offline   Reply With Quote
Old Mar 14th, 2006, 6:12 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
I wasn't disputing what you said Cache. I was just stating what a pointer is and isn't.
grumpy is offline   Reply With Quote
Old Mar 14th, 2006, 6:56 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 14th, 2006, 9:05 AM   #7
Bench
Newbie
 
Join Date: Feb 2006
Posts: 20
Rep Power: 0 Bench is on a distinguished road
Quote:
Originally Posted by DaWei
In some tutorials, you will read that a pointer doesn't have a value, it has a pointer.
And I think this sort of confusion among tutors is the main source of confusion among learners. I'm sure it would help if the word "pointer" itself was not applied to the value aswell as the object.
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:
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.
I agree with you on this point. calling it a value is certainly far better than just saying "it contains a pointer".
Bench is offline   Reply With Quote
Old Mar 14th, 2006, 9:58 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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:
Whoever it was who wrote "the value 0" which is an integer value. (void *)0
is a null pointer constant but it doesn't have "the value 0", it has a
pointer value.
So, they're not saying it doesn't have a value, but the value is a 'pointer value.' It seems to me that if one has 'pointer arithmetic', one has an arithmetic value at least somewhere along the way.

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
DaWei is offline   Reply With Quote
Old Mar 14th, 2006, 10:11 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by Bench
And I think this sort of confusion among tutors is the main source of confusion among learners. I'm sure it would help if the word "pointer" itself was not applied to the value aswell as the object.
People who do that are showing they don't really understand what a pointer is. And, as Dawei said, the unfortunate thing is that people who do that are often tutors or authors of some introductory texts for C or C++.

Quote:
Originally Posted by Bench
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).
The problem is that C++ has TWO slightly different concepts for pointers. One of those is inherited from C, and the other is not.

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:
Originally Posted by Bench
This extra clarification might reduce the inevitable conflict of ideas that always arises when a beginner attempts to write a program using pointers.

I agree with you on this point. calling it a value is certainly far better than just saying "it contains a pointer".
Indeed.

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.
grumpy is offline   Reply With Quote
Old Mar 14th, 2006, 10:38 AM   #10
Bench
Newbie
 
Join Date: Feb 2006
Posts: 20
Rep Power: 0 Bench is on a distinguished road
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.
Bench is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC