Something I still don't understand about pointers

This is a discussion on Something I still don't understand about pointers within the C++ Programming forums, part of the General Programming Boards category; Alright... the * attached to the pointer means the contents of whatever it is pointing to right? and & means ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    Something I still don't understand about pointers

    Alright... the * attached to the pointer means the contents of whatever it is pointing to right? and & means the address of... so why does this code work...

    Code:
    #include <iostream>
    double result(int * pointer, int counter);
    main()
    {
    	int * pointer,
    		counter;
    	std::cout<<"How many items will you be entering?\n";
    	std::cin>>counter;
    	pointer=new int[counter];
    	for(int x=0; x<counter; x++)
    	{
    		std::cout<<"Enter value "<<x+1<<": ";
    		std::cin>>pointer[x];
    		std::cout<<std::endl;
    	}
    	std::cout<<"The average of the numbers entered is: "<<result(pointer, counter)<<std::endl;
    	delete [] pointer;
    	return 0;
    }
    double result(int * pointer, int counter)
    {
    	double total=0;
    	for(int x=0; x<counter; x++)
    	{
    		total+=pointer[x];
    	}
    	return total/counter;
    }
    if the * means the contents of, why couldn't this work, for example:

    Code:
    total+=pointer[x]*;
    now i know that doesn't work. and i understand it doesn't work...but when do you use the * and when do you leave it blank, as far as pointers go? Any global truths?

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    yes a pointer points to an address..but since you use the 'new' operator..which allocated memory off the heap..it is pointing to the new allocated memory you just supply for it..isnt dynamic memory allocation beauitful?

    this allocates an array of ints..4 bytes (visual c++) time counter amounts.
    Code:
    pointer=new int[counter];
    well when you use
    Code:
    pointer[x]
    it is the same as
    Code:
    *(pointer + x)
    why wouldn't this work?
    Code:
    total+=pointer[x]*;
    because thats like saying
    Code:
    total += *(pointer + x)*;
    pointers and arrays are very similar in c++
    nextus, the samurai warrior

  3. #3
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    main() needs a return type

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    51
    Interesting. Never knew that before. So whenever you have a pointer to an array, its like picking a memory allocation to start from, and counting from that certain memory units based on what the type is and which element. Wow!

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    An array is a pointer, more or less.. a pointer to the first element. For example,

    int i = array[0];

    is the same as

    int i = *array;
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: ~

    Originally posted by rmullen3
    An array is a pointer, more or less.. a pointer to the first element. For example,

    int i = array[0];

    is the same as

    int i = *array;
    Don't confuse him. An array is still extremely different from a pointer. C++ just provides syntax which easily allows you to access a pointer to the first element. That doesn't in any way make it a pointer. A lot of early programmers that I know actually think that an array is a pointer because of statements similar to that.

    An array is a group of data of the same type in a row in memory. It, itself, is NOT a "pointer to the first element of that memory."

    C++ just makes it so that when you use the array name by itself, it, in some cases, "returns" a pointer to the first element of the array.

    The best metaphor I can give is a function returning a value:

    Take this function definition

    char* Func();

    What is Func? Is it a pointer to a character or is it a function? Here I have absolutely no doubt that you will tell me it's a function.

    Of course, I can always use it's return value in an expression

    *(Func() + 1) = 'a';

    Is Func all of a sudden a pointer? No. It's still just a function.

    A similar thing happens with an array

    char Array[5];

    Sure, you can do

    *(Array + 1) = 'a';

    Here, it's implicit, but it's in many ways like function example, Array isn't a pointer. We just are provided with a way of working with a pointer to the array's first element in a quick and easy fashion (otherwise you'd have to do &Array[0] which just takes up more space to write).

    If an array was a pointer, then the array itself would always takeup sizeof(char*) size, and would point to some other place in memory. Obviously this is not the case. When you make an array on the stack, you are getting exactly what the definition says it is -- a group of consecutive elements of the given type put right on the stack. There is asolutely no pointer to the first element stored with that array. They are completely different concepts.
    Last edited by Polymorphic OOP; 03-01-2003 at 04:35 AM.

  7. #7
    Registered User The Junglist's Avatar
    Join Date
    Nov 2002
    Posts
    42

    Re: Re: ~

    As we have seen, when we declare an array, a contiguous block of memory is allocated for the cells of the array and a pointer cell (of the appropriate type) is also allocated and initialized to point to the first cell of the array. This pointer cell is given the name of the array. When memory is allocated for the array cells, the starting address is fixed, i.e. it cannot be changed during program execution. Therefore, the value of the pointer cell should not be changed. To ensure that this pointer is not changed, in C, array names may not be used as variables on the left of an assignment statement, i.e. they may not be used as an Lvalue. Instead, if necessary, separate pointer variables of the appropriate type may be declared and used as Lvalues.
    That comes from here.

    [edit]
    Forgot to add this.

    An array name is a pointer to the first element of the array. The indexing used is exactly the same as for non-constant-pointers.
    [/edit]
    Last edited by The Junglist; 03-01-2003 at 05:06 AM.

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    I'm certainly glad I don't go to the University of Hawaii then -- because that is a completely false statement!

  9. #9
    Registered User The Junglist's Avatar
    Join Date
    Nov 2002
    Posts
    42
    Originally posted by Polymorphic OOP
    I'm certainly glad I don't go to the University of Hawaii then -- because that is a completely false statement!
    Which one? From the quote or my statement?

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Both. A pointer is NOT allocated with the array. The person who wrote that for the University of Hawaii is completely wrong! I guess this is another reason why beginners have so many problems -- the professors don't even know what they're teaching!!!

  11. #11
    Registered User The Junglist's Avatar
    Join Date
    Nov 2002
    Posts
    42
    Could you please post some references, because I would also like to know what the real deal is with arrays.

  12. #12
    Registered User The Junglist's Avatar
    Join Date
    Nov 2002
    Posts
    42
    This cleared things up for me :

    from The C Programming Language by Kernighan & Ritchie

    There is one difference between an array name and a pointer that must be kept in mind. A pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal.
    When an array name is passed to a function, what is passed is the location of the initial element. Within the called function, this argument is a local variable, and so an array name parameter is a pointer, that is, a variable containing an address.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 11:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21