Pointer Question

I'm doing a very simple pointer program just pointing to addresses and printing them, but I can't seem to grasp what I'm doing. I read through the page on pointers from this site as well as some other notes and I don't quite understand what I'm being asked. Maybe some of you could help clarify.

Basically I'm given a list of variables/arrays/pointers I need to declare and determine the address of. I have to create a pointee for each pointer, and print the address of each pointer and pointee.

Here is the list I am supposed to use:
1
2
3
4
5
6
7
8
char p;         //Primitive character type
char *ptr;      //Pointer to dynamically create a new char
char a[3];      //Array of 3 characters
char *aptr;     //Pointer to dynamically create an array, new char[3]
char a2[3][3];  //2-D array of 9 characters
char *a2ptr[3]; //Array of 3 pointers to dynamically create 3 new char[3]
char **a2dptr;  //Double pointer to dynamically create a [3][3] array
string str;     //String object 


So does this mean for '*ptr' I would create the pointee 'ptr'? What does it mean to "dynamically create" something? I don't know if I don't understand pointers well enough, or if all of the extra lingo is throwing me off. Thank you for any help.
I am a beginner too, but pointers is one of the subjects I have been reading about over the last week. From what I gather when you do int *ptr you are creating a variable in the memory which is ready to hold an address. When you do ptr = &num you put the address of num into the pointer variable ptr. Dynamic creation is something I am struggling with. I know how to dynamically create something, but using it after confuses me. My instruction with dynamic creation is referring to an array. I suppose you could dynamically create a single variable, but that might not really be useful. The reason a dynamically created array would be useful is in cases when you do not know how many variables you want to create. That is how it is explained to me anyway. In my work I have used the form ptr = new int[variable] where the variable is the number of int's the program user creates.

Another thing I have found interesting about pointers is something like int* or int** seems to indicate how to read the contents of the section of memory the variable has reserved. So one * means read the information stored there as an address, but read the address it points to as an int or whatever your prefix is, while two * means read the information stored as an address, then the next section of memory as an address as well, but the third time read it as an int, and so on.
Last edited on
This is my code as of right now. I think the ptr pointer/pointee make sense, but the aptr one isn't making sense. How do you point to dynamically create an array?

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
  #include <iostream>
  #include <string>
 
  using namespace std;
  int main(int argc, char *argv[]) {
      char p;
      char *ptr;
      char a[3];
      char *aptr;
      char a2[3][3];
      char *a2ptr[3];
      char **a2dptr;
      string str;
 
      char ptr1; //Creates a pointee for *ptr
      ptr = &ptr1; //Points ptr to ptr1
 
      aptr = new char[3];
 
 
 
      cout << hex;
      cout << "Address of ptr1 " << (unsigned long) &ptr1 << endl;
      cout << "Address ptr points to " << (unsigned long) ptr << endl;
 
      cout << "Address of aptr[3] " << (unsigned long) &aptr[3] << endl;
      cout << "Address aptr points to " << (unsigned long) aptr << endl;
 
 
      return 0;
  }
How do you point to dynamically create an array?

You don't. You have to dynamically create an array and then make your pointer point to it.

The dynamic allocation of memory is performed by operator new.
When you call new char[N], you ask your system to reserve enough space to store N chars. If it succeeds, it returns the location (ie memory address) of the reserved space.

If you want to use the allocated memory, you need to store its address so you know where it is located. That's what aptr = new char[3]; does.

By the way, there is a mistake in your output : aptr[3] is not supposed to exist.
Indices go from 0 to 2.
Another thing I have found interesting about pointers is something like int* or int** seems to indicate how to read the contents of the section of memory the variable has reserved. So one * means read the information stored there as an address, but read the address it points to as an int or whatever your prefix is, while two * means read the information stored as an address, then the next section of memory as an address as well, but the third time read it as an int, and so on.


I don't really get what you were meant to say but: are you sure? As far as I know it's sometimes useful using a pointer that points to another pointer. I've used that when I wanted to define -dinamically- a matrix: in that case you can define a pointer that points to an array of pointers (each pointing to an array of int, for instance). Defining a double-pointer needs a sintax similar to the one you were describing (with two stars).

For instance:

1
2
3
4
5
6
7
int** matrix;
*matrix = new int*[10]; // Defining a "row" of ten pointers

for(i = 0; i < 10; ++i)
{
 matrix[i] = new int[10]; // Making each pointer of that row pointing to a "column" of integers.
}


Note: of course, rows and columns are just for the sake of having an image to get how multiple-pointers (that's not a formal terminology) work, but nothing should go wrong if you want to extend this construction from a 2-dimensional matrix to an nth-dimensional one.
What does it mean to "dynamically create" something?

For example, let's say you need to create an array but you don't the size until run time due to it being dependent on some form of input. Well the compiler needs to know at compile the size of the array. The way around this is to use dynamic allocation. When this is done, memory is given to the program from the heap which is different than static memory which lies on the stack. These are terms you should get familiar with.

Pointers in and of themselves are very simple. All a pointer is is a data type which holds an address of some place in memory, ie a variable. So instead of dragging the whole variable around you can just store it's address and access indirectly that way.

Think of pointers as a phone number of a friend. When you want to get in touch with your friend quickly, do you get in your car and drive all the way to his house? Or do you just call him? His phone number would be the pointer in this situation, and calling him and retrieving the information would be dereferencing the pointer. In this analogy, driving to his house and talking to him would similar to manipulating variables directly, by value.

Now I made this analogy up on the spot so don't hate if it's not perfect.

Here's how they work in practice.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int* ptr; //This is a variable called ptr that is of type int*. This means it points to an int.

int* ptr2 = new int[runTimeVar]; //The new keyword is how you dynamically create something. This allows variable length arrays.

char* ptr3; //ptr3 points to a char.


int* ptr;
double val;
ptr = val; //Invalid. Can't assign non pointer types to a pointer. Only pointer to pointer.

ptr = &val; //Also invalid. We already declared ptr as pointing to an int, not a double.

int num;
ptr = &num; //This is valid. This now makes ptr point at num. ptr now stores num's address.

int* ptr2;
ptr = ptr2; //This is valid. This makes ptr point to the same ptr2 is pointing at. Their values are now the same, 
//ie they hold the same memory address. This does not mean they are sitting in the same memory address though. 
Last edited on
Topic archived. No new replies allowed.