C++ Assignment Operator With Pointers Dogs

References might be implemented by storing the address.Usually Java references will be implemented as pointers, but that's not required by the specification. They may be using an additional layer of indirection to enable easier garbage collection. But in the end it will (almost always) boil down to (C-style) pointers being involved in the implementation of (Java-style) references.

You can't do pointer arithmetic with references. The most important difference between a pointer in C and a reference in Java is that you can't actually get to (and manipulate) the underlying value of a reference in Java. In other words: you can't do pointer arithmetic.

In C you can add something to a pointer (i.e. the address) or substract something to point to things that are "nearby" or point to places that are at any place.

In Java, a reference points to one thing and that thing only. You can make a variable hold a different reference, but you can't just ask it to point to "the thing after the original thing".

References are strongly typed. Another difference is that the type of a reference is much more strictly controlled in Java than the type of a pointer is in C. In C you can have an and cast it to a and just re-interpret the memory at that location. That re-interpretation doesn't work in Java: you can only interpret the object at the other end of the reference as something that it already is (i.e. you can cast a reference to reference only if the object pointed to is actually a ).

Those differences make C pointers more powerful, but also more dangerous. Both of those possibilities (pointer arithmetic and re-interpreting the values being pointed to) add flexibility to C and are the source of some of the power of the language. But they are also big sources of problems, because if used incorrectly they can easily break assumptions that your code is built around. And it's pretty easy to use them incorrectly.

The latest version of this topic can be found at Pointers (C++).

Pointers are declared using the following sequence.

where any valid pointer declarator may be used for . The syntax for a simple pointer declarator is as follows:

  1. The declaration specifiers:
  • An optional storage class specifier. For more information, see Specifiers.

  • An optional or keyword applying to the type of the object to be pointed to.

  • The type specifier: the name of a type representing the type of the object to be pointed to.

  1. The declarator:
  • An optional Microsoft specific modifier. For more information, see Microsoft-Specific Modifiers.

  • The operator.

  • An optional or keyword applying to the pointer itself.

  • The identifier.

  • An optional initializer.

The declarator for a pointer to function looks like this:

  • For an array of pointers, the syntax looks like this:
  • However, pointer declarators can be more complex. For more information,see Declarators.

  • Multiple declarators and their initializers may appear together in a single declaration in a comma separated list following the declaration specifier.

A simple example of a pointer declaration is:

The preceding declaration specifies that points to an object of type .

A more complex example is

The preceding declaration specifies that is a constant pointer to an object of type with static storage duration.

The next example shows how multiple pointers are declared and initialized:

In the preceding example, pointers p and q both point to objects of type and are initialized to the addresses of i and j respectively. The storage class specifier applies to both pointers.

Another example illustrates the use of pointers in data structures; in this case, a linked list.

C++ Abstract Declarators
Addition of Pointer Types
Indirection Operator: *
Address-of Operator: &

[storage-class-specifiers] [cv-qualifiers] type-specifiers [ms-modifier] declarator ;
* [cv-qualifiers] identifier [= expression]
(* [cv-qualifiers] identifier )( argument-list ) [cv-qualifers] [exception specification] [= expression];
* identifier [ [ constant-expression ] ]
static unsigned int * const ptr;
static int *p = &i, *q = &j;
// pointer.cpp // compile with: /EHsc #include <iostream> int main() { int i = 1, j = 2; // local variables on the stack int *p; // a pointer may be assigned to "point to" the value of // another variable using the & (address of) operator p = & j; // since j was on the stack, this address will be somewhere // on the stack. Pointers are printed in hex format using // %p and conventionally marked with 0x. printf_s("0x%p\n", p); // The * (indirection operator) can be read as "the value // pointed to by". // Since p is pointing to j, this should print "2" printf_s("0x%p %d\n", p, *p); // changing j will change the result of the indirection // operator on p. j = 7; printf_s("0x%p %d\n", p, *p ); // The value of j can also be changed through the pointer // by making an assignment to the dereferenced pointer *p = 10; printf_s("j is %d\n", j); // j is now 10 // allocate memory on the heap for an integer, // initialize to 5 p = new int(5); // print the pointer and the object pointed to // the address will be somewhere on the heap printf_s("0x%p %d\n", p, *p); // free the memory pointed to by p delete p; // At this point, dereferencing p with *p would trigger // a runtime access violation. // Pointer arithmetic may be done with an array declared // on the stack or allocated on the heap with new. // The increment operator takes into account the size // of the objects pointed to. p = new int[5]; for (i = 0; i < 5; i++, p++) { *p = i * 10; printf_s("0x%p %d\n", p, *p); } // A common expression seen is dereferencing in combination // with increment or decrement operators, as shown here. // The indirection operator * takes precedence over the // increment operator ++. // These are particularly useful in manipulating char arrays. char s1[4] = "cat"; char s2[4] = "dog"; char* p1 = s1; char* p2 = s2; // the following is a string copy operation while (*p1++ = *p2++); // s2 was copied into s1, so now they are both equal to "dog" printf_s("%s %s", s1, s2); }
0x0012FEC8 0x0012FEC8 2 0x0012FEC8 7 j is 10 0x00320850 5 0x00320850 0 0x00320854 10 0x00320858 20 0x0032085C 30 0x00320860 40 dog dog
// pointer_linkedlist.cpp // compile with: /EHsc #include <iostream> using namespace std; struct NewNode { NewNode() : node(0){} int i; NewNode * node; }; void WalkList(NewNode * ptr) { if (ptr != 0) { int i = 1; while (ptr->node != 0 ) { cout << "node " << i++ << " = " << ptr->i << endl; ptr = ptr->node; } cout << "node " << i++ << " = " << ptr->i << endl; } } void AddNode(NewNode ** ptr) { NewNode * walker = 0; NewNode * MyNewNode = new NewNode; cout << "enter a number: " << endl; cin >> MyNewNode->i; if (*ptr == 0) *ptr = MyNewNode; else { walker = *ptr; while (walker->node != 0) walker = walker->node; walker->node = MyNewNode; } } int main() { char ans = ' '; NewNode * ptr = 0; do { cout << "a (add node) d (display list) q (quit)" << endl; cin >> ans; switch (ans) { case 'a': AddNode(&ptr); break; case 'd': WalkList(ptr); break; } } while (ans != 'q'); }
a 45 d a 789 d qa (add node) d (display list) q (quit) enter a number: a (add node) d (display list) q (quit) node 1 = 45 a (add node) d (display list) q (quit) enter a number: a (add node) d (display list) q (quit) node 1 = 45 node 2 = 789 a (add node) d (display list) q (quit)

One thought on “C++ Assignment Operator With Pointers Dogs

Leave a Reply

Your email address will not be published. Required fields are marked *