Pointers and links

I understand the general meaning of pointers and references (or at least I think I know), I also understand that when I use new , I dynamically allocate memory. My question is this, if I used cout <& p the “virtual memory location” will be displayed on page. Is there a way to manipulate the “virtual memory location”? The following code shows an array of ints, if I wanted to show the value of p [1], and I knew the "virtual memory location" p, could I hypothetically make & p + 1 and get the value p [1] by doing cout <* p , since * p is now a pointer to the second element in the array.

int *p; p = new int[3]; p[0] = 13; p[1] = 54; p[2] = 42; 
+6
c ++ c pointers reference
source share
5 answers

Of course, you can manipulate the pointer to access the various elements in the array, but you will need to manipulate the contents of the pointer (that is, the address of what p points to), and not the address of the pointer itself.

 int *p = new int[3]; p[0] = 13; p[1] = 54; p[2] = 42; cout << *p << ' ' << *(p+1) << ' ' << *(p+2); 

Each addition (or subtraction) means the next (previous) element in the array. If p points to a 4 byte variable (for example, int on typical 32-bit PCs) at an address, for example 12345, p + 1 will point to 12349, not 12346. Note that you want to change the value of what p contains, before dereferencing him, gain access to what he points to.

+8
source share

Not really. &p is the address of the pointer p . &p+1 will refer to an address that is one int* further. What you want to do is

 p=p+1; /* or ++p or p++ */ 

Now that you do

 cout << *p; 

You will get 54. The difference is that p contains the address of the beginning of the array int, and &p - the address of p. To move one element forward, you need to point further to the int array, and not further along your stack where p is located.

If you only have &p , you will need to do the following:

 int **q = &p; /* q now points to p */ *q = *q+1; cout << *p; 

This will also print 54, if I'm not mistaken.

+3
source share

Some time has passed (many years), since I worked with pointers, but I know that if p points to the beginning of the array (ie p [0]), and you increased it (ie p ++) then p will now be pointing to p [1].

I think you need to remove the link to p in order to get the value. You are looking for a pointer by placing * in front of it.

So * p = 33 with a change in p [0] to 33.

I assume that to get the second element you should use * (p + 1), so the syntax you need will be:

 cout << *(p+1) 

or

 cout << *(++p) 
+2
source share

I like to do this:

 &p[1] 

It looks neat to me.

+2
source share

Think of the “types of pointers” in C and C ++ as creating a very long logical string of cells superimposed on bytes in the memory space of the CPU, starting from byte 0. The width of each cell, in bytes, depends on the type of pointer. Each type of pointer sets a row with a different cell width. The "int *" pointer sets a string of 4-byte cells, since the width of the int storage is 4 bytes. A "double *" sets an 8-byte string for each cell; a "struct foo *" pointer sets a row with each cell the width of one "struct foo" , whatever that is. The “address” of any “thing” is the byte offset, starting at 0, of the cell in the row containing the “thing”.

Pointer arithmetic is based on cells in a row, not bytes. " *(p+10) " is a reference to the 10th cell after "p", where the cell size is determined by the type p. If the type "p" is "int", the address "p + 10" is 40 bytes per p; if p is a pointer to a structure of 1000 bytes long, "p + 10" is 10,000 bytes per p. (Note that the compiler chooses the optimal size for the structure, which may be larger than you think, this is due to “filling” and “alignment.” The considered structure of 1000 bytes can take 1024 bytes per cell, for example, so that “p + 10 "will actually be 10,240 bytes per p.)

+1
source share

All Articles