Question

In: Computer Science

How is a const pointer similar and different from a reference variable? And please give a...

How is a const pointer similar and different from a reference variable? And please give a good example.

Please help! Thank you!

Solutions

Expert Solution

SIMILARITIES

  • Reference variable and const pointers are very similar, both are used to have one variable provide access to another.
  • They providing lots of the same capabilities, sometime it is unclear what is different between these different mechanisms.
  • Constant pointer as the name implies can not be bound again. Same is the case with the reference.variable.
  • both const pointer and reference variable holds the address.
  • The performances are exactly the same, as references are implemented internally as pointers

DIFFERENCES

  • Const pointers can be NULL
  • A reference does not contain it's own address whereas a pointer does contain.
    The address of a reference is the actual object's address.
  • A pointer has its own address and it holds as its value the address of the value it points to.
  • A pointer can be initialized in this way:
     int x = 12;        
      int *p = &x;    
             OR 
         int *p;
       p = &x;
  • A reference can be initialized in this way:
    int x=12;
    int &p=x;
  • A pointer can be re-assigned:
  • ex-
    int a = 5;
    int b = 6;
    int *p;
    p = &a;
    p = &b;
    *p = 10;
    assert(a == 5);
    assert(b == 10);
  • A reference cannot reassigned, and must be assigned at initialization:
  • ex-
    int a = 5;
    int b = 6;
    int &r = a;

Related Solutions

Independent and dependent variable How are these similar and different?
Independent and dependent variable How are these similar and different?
C++ please. Define a problem with user input, user output, Pointer, with const and sizeof operator.
C++ please. Define a problem with user input, user output, Pointer, with const and sizeof operator.
how is adolescence similar to and different from middlechildhood
how is adolescence similar to and different from middlechildhood
How is the AICPA Code different from the IFAC Code of Ethics? How are they similar?...
How is the AICPA Code different from the IFAC Code of Ethics? How are they similar? Select one section of the AICPA code and describe the guidance provided in that section. How could an accountant use the guidance to more effectively manage his conduct as a business professional? Try to support your explanation with a relevant example.
how are aseptic techniques similar and different in the lab and healthcare field? give three differences...
how are aseptic techniques similar and different in the lab and healthcare field? give three differences and three similarities.
How is a clinical trial for a medical device similar to or different from a clinical...
How is a clinical trial for a medical device similar to or different from a clinical trial for a drug?
How are absorption costing and variable costing the​ same? How are they​ different? Give one example...
How are absorption costing and variable costing the​ same? How are they​ different? Give one example on how management uses either absorption costing or variable costing to make a decision. Your answer should be in complete sentences with proper grammar and spelling. no handwriting
How are abortion and eugenics similar? How are they different?
How are abortion and eugenics similar? How are they different?
How iOS and Android similar and How are they different?
How iOS and Android similar and How are they different?
What is “public health” and how is it similar to and different from “population health”? What...
What is “public health” and how is it similar to and different from “population health”? What are some of the approaches available to promote and protect health?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT