About pointer

Hi all,

I'm very new to C++ programming, I read the tutorial about pointer, and there are some questions pop up in my mind that I would like your help. These are:
1/ Let say I declare a pointer type:
double * pointerOfX;
double X = 1000.0;
double = &X;
cout << pointerOfX;
What is the output of pointerOfX? I know the result is a memory address, but what is the data type of a memory address? hex? or decimal?
2/ how about if I point to a custom object:
Student * pointerOfAStudent;
Student aStudent = new Student("First", "Last");
pointerOfAStudent = &aStudent;
cout << pointerOfAStudent;
What will be the result of this output? will it be a memory address?

Thanks,

Sam
closed account (zb0S216C)
linhungsam wrote:
what is the data type of a memory address? (sic)

Hexadecimal.

linhungsam wrote:
will it be a memory address? (sic)

Yes. Until you dereference a pointer, you'll always get the address of that pointer.

Wazzak
Last edited on
Thanks Framework, I know how to use pointer now, but can you tell me more about the purpose of using pointer?

In which specific scenario should we use pointer?

Sam.
I just wanted to add that when you're working with custom objects the "->" operator dereferences the pointer.
@OP: I would also like to contribute to this thread, could you select a context for the types of programs you want to write? Is it video games, Data bases, web applications, system adminitration, networking, something completley different?

The problem with answering your question directly is that pointers are one of the most powerful aspects of C++, they are what allow us to use DMA (Direct Memory Access) so there is no single answer and anyone who thinks they have one is not using their imagination.
closed account (zb0S216C)
linhungsam wrote:
can you tell me more about the purpose of using pointer? (sic)

In C++, pointers are more commoly used to hold allocated resources. The whole idea of a pointer is to point to another address.

linhungsam wrote:
In which specific scenario should we use pointer? (sic)

When you want to reserve n bytes of memory during run-time. For example, suppose you had an array, and you wanted the user to determine the length of the array. This is where pointers come into play. Since pointers can allocate memory, you can dynamically create the array with the specified amount of elements. In this case, the users input. If you didn't use pointers, you wouldn't be able to dynamically create the array, thus, the array would be a fixed length that would be defined at compile time.

Wazzak
Last edited on
:) Thanks for the quick replies.

@Computergeek01: I'm writing a Windows Mobile project. I m a c# programmer, but I realize that C# is not powerful enough in term of accessing certain part of devices such as sound card, video card, DMA directly like you mentioned... Even though there are managed code for c# which wrap native c++ into .net class, but some of them are not totally free, and instead of waiting or paying for someone to post a wrapper class solution, I decide to learn C++ myself. In other word I want to directly and totally control the devices that I own. At the moment, I need to learn C++, because I want to integrate a portion of c++ code to c# code, however I will post another thread regarding this topic, hope you can help too.

@Framework: so in other words, pointers store the data of data? because memory address also is some sort of data. Will it be faster if we directly process a pointer instead of process a variable? for ex:
int i = 0;
for (i; i < 1000000000; i++)
{

// do something
}
vs
int * i;
*i = 0;
for (i; *i < 1000000000; (*)i++)
{

// do something
}
closed account (zb0S216C)
linhungsam wrote:
1
2
3
int * i;
*i = 0;
for (i; *i < 1000000000; (*)i++)
(sic)

I think you've misunderstood pointers. The first of your two code segments is the correct one.

Pointers need to point to an address of another variable or to the address of the first block (byte) of a region of memory. Pointers cannot point to numerical literals, except for zero (considered NULL). NULL is C++'s way of saying that the pointer isn't pointing to an actual address.

Here's an example that shows the incorrect use of a pointer:

1
2
3
4
5
6
7
8
9
10
int main( ) 
{
    int *Pointer( nullptr );

    // Place a numerical literal into the address pointed to
    // by Pointer.
    *Pointer = 100;

    return 0;
}

Here, since Pointer isn't pointing to a valid address (nullptr is C++0x's new NULL), the write operation results in an access violation. Instead, pointers need something to point to in order to use them as intended. For example, consider this:

1
2
3
4
5
6
7
8
9
10
11
12
int main( ) 
{
    int Data( 0 );

    int *Pointer( &Data );

    // Place a numerical literal into the address pointed to
    // by Pointer.
    *Pointer = 100;

    return 0;
}

Here, Pointer is now pointing to the address of the first of 4 bytes of Data. When we dereference Pointer, the data within the address pointed to by Pointer is read/written to. As I said before, pointers can also point to the first byte of an allocated block. For example:

1
2
3
4
5
6
7
8
int main( ) 
{
    int *Pointer( new( std::nothrow ) int[3] );

    delete [] Pointer;

    return 0;
}

In this code segment, a block of 12 bytes is allocated during the construction of Pointer. Now, Pointer is pointing the the first byte of the first integer within the array.

Wazzak
Last edited on
According to me , the pointer or reference is used where large structure or class object is passed/ copied to . Instead of passing / copying one by one varialble to the function , we can use pointer or refernce .
What I like most about them is doing cool stuff like self extending data circles.

Consider a string-input, you don't know how long it'll be...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>

struct circleElement
{
	circleElement* prev;
	circleElement* next;
	char value;
	};

int main( int argA, char ** argV ){
	circleElement* dummy = new circleElement;
	dummy->next = dummy->prev = dummy;
	dummy->value = ' ';
	fprintf( stdout , "Please enter something:");
	while( dummy->prev->value != '\n' ){
		circleElement *temp = new circleElement;
		temp->prev = dummy->prev; //insert a new element at last circle position
		temp->next = dummy;
		temp->prev->next = temp;
		dummy->prev = temp;
		
		temp->value = getc( stdin ); //read a char from stdin
		};

	fprintf( stdout, "\n\nreverted input:"); // just exchange "prev" with "next" in following section to see what happens...
	while( dummy->prev != dummy ){
		circleElement *temp = dummy->prev;
		fprintf( stdout, "%s", &temp->value );
		dummy->prev = temp->prev;
		delete temp; //be sure to deallocate the memory somewhere...
		};
	
	fprintf( stdout, "\nDONE\n" );
	return 0;
};


actually I would place the whole circle thingy inside a class to safely deallocate memory within the destructor

ZED
Topic archived. No new replies allowed.