Thread: question about pointers

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    question about pointers

    Does dereferencing mean working directly with the variable a pointer is pointing to, or is there something much more to the definition of dereferencing?
    Last edited by volk; 05-31-2003 at 09:33 AM.

  2. #2
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by Salem
    > Does dereferencing mean working directly with the variable a pointer is pointing to
    Yeah, that's what it means
    Though you should note that pointers can point at const things as well.
    You can't change the value of a const, so what's the point of having a pointer point to it?

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    By your logic, there would be no reason to have constant variables:

    const int PI = 3.14;

    After all why have a variable if you can't change it? It's more efficient to pass pointers to functions than having to copy variables, especially when they represent large objects. If you have something in your program that is critical that it remain constant, you would delcare it as constant and the compiler would not let a pointer to it change it.
    Last edited by 7stud; 05-31-2003 at 07:40 PM.

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    So passing a variable to a function by reference is a form of dereferencing. So there is no point of having a pointer point to a constant unsless you're using functions, right?
    Last edited by volk; 05-31-2003 at 08:47 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "So passing a variable to a function by reference is a form of dereferencing."

    Not at all. Dereferencing a pointer is a very basic operation and it has nothing to do with whether you pass the pointer to a function or not:

    int a = 10;
    int* pa = &a;

    cout<<pa; //output is the address in memory of a
    cout<<*pa: //output is the data stored at the address in memory

    "So there is no point of having a pointer pointer to a constant unsless you're using functions, right?"

    Once again, that's equivalent to saying there's no reason to use a pointer--of any kind--unless you're going to use functions. Generally, it's probably true, but I'm sure their are applications for using pointers either to non-constants or constants without using functions.
    Last edited by 7stud; 05-31-2003 at 09:04 PM.

  6. #6
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    >> but I'm sure their are applications for using pointers either to non-constants or constants without using functions.

    Um, ok...I guess I can trust you.

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Perhaps a C style string.. that is const.

    Code:
    #include <stdio.h>
    #include <alloc.h>
    int main(){
    
       const char * str = "What?";
       /* do a lot of atributions and such with str, but 
        knowing that str stays unchanged*/
       printf("So we have : \"%s\"",*str);
    
    }
    Nothing more to tell about me...
    Happy day =)

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here's an example of using pointers to constants without needing to use functions. It involves strings:

    char* pstring = "Hello World";

    That statement takes the string literal "Hello World" and stores it as a constant in memory. Then it assigns the address of the first char in the string to the pointer pstring. So, you can't change the value of what pstring points to. If you try to do this:

    *pstring = 'x';

    You won't get a compiler error, but your program will crash. Normally, your compiler won't allow you to assign a const--the string literal--to a non constant variable--the pointer, and you'll get an error while compiling. In this case, string literals only become constants recently so the compiler allows it for legacy code reasons.

    This is the way you might use these pointers to constants:
    Code:
    #include <iostream>
    using namespace std;
    
    int main () 
    {
    	char* pstring1="Today you are going try something new.";
    	char* pstring2="Tomorrow, you will experience a reversal.";
    	
    	int answer=0;
    	cout<<"Enter 1 or 2 to get your fortune.\n";
    	cin>>answer;
    	
    	switch(answer)
    	{
    	case 1:
    		cout<<pstring1<<endl;
    		break;
    	case 2:
    		cout<<pstring2<<endl;
    		break;
    	default:
    		cout<<"No follow directions, no fortune.";
    	}
    				
    	return 0;
    }
    Last edited by 7stud; 05-31-2003 at 09:35 PM.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by volk
    So passing a variable to a function by reference is a form of dereferencing. So there is no point of having a pointer point to a constant unsless you're using functions, right?
    Not necessarily. For example, you might want to create a default object.

    One example: in a binary search tree, each leaf (the end of each path) is a special object (usually called a NIL or sentinel node). Rather than make every leaf of a large search tree be a separate object (which all are identical, and store no data at all), you make all the leaves point at the same object. Since any tree uses pointers to connect parent to child, you can easily do this, and you then, if you had a million branches in the tree, you only have ONE sentinel object in memory, you just make many, many pointers reference it.

    Anytime you want to provide one or more default objects, it's very handy to make them constants, and point to them as needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM