Can someone check if I got these correct please?? I'm pretty sure I missed almost half of these so I really need help on these. If someone can explain how to get an answer, that would be great.

a: 5
b: address of location of x (0x....)
c: address of value x (0x...)
d: address of value x (0x...)
e: 5
f: 3
g: 3
h: 6
i: 9
j: vals
k: address of vals + 1
l: address of ptrVals + 2
m: 0
n: 55
o: 55
p: 0

int x;
int *ptrX;

x = 5;

ptrX = &x;


cout << "a:" << x << endl;
cout << "b:" << &x << endl;   
cout << "c:" << ptrX << endl;
cout << "d:" << &ptrX << endl;
cout << "e:" << *ptrX << endl;

int vals[3];

*vals = 3;

vals[1] = *vals + 3;

*(vals+2) = 9;

cout << "f:" << vals << endl;
cout << "g:" << vals[0] << endl;
cout << "h:" << vals[1] << endl;
cout << "i:" << vals[2] << endl;


int *ptrVals;

ptrVals = vals;

cout << "j:" << ptrVals[0] << endl;

cout << "k:" << *(vals+1) << endl;

cout << "l:" << *(ptrVals+2) << endl;

int *ptr = NULL;

cout << "m:" << ptr << endl;

const int intRate = 55;   
const int *ptrRate;
ptrRate = &intRate;

cout << "n:" << intRate << endl;

*ptrRate = 66;

cout << "o:" << intRate << endl;
int * const ptrY = &x;

ptrY = &vals[0];
cout << "p:" << *ptrY << endl;
const int * const ptrZ = &intRate;

ptrZ = &vals[0];

*ptrZ = 7;

Dani AI

Generated

A quick, focused correctness checklist and debugging plan for the snippet in the first post.

Summary of what should happen (not including the illegal assignments later): a, b, c, d and e are correct as written — a and e show the integer value 5, and b/c/d print pointer addresses (address format is implementation-defined). The array section sets vals[0]=3, vals[1]=6, vals[2]=9 (via *vals = 3, vals[1] = *vals + 3, *(vals+2) = 9). So the value outputs are: g: 3, h: 6, i: 9. f: prints the array’s address (not the element 3). With ptrVals = vals, j: prints 3 (same as vals[0]), k: prints 6 (same as *(vals+1)), and l: prints 9 (same as *(ptrVals+2)). m: prints the null pointer value (usually 0 or (nil)), and n: prints 55.

Why the later lines fail to compile (brief):

  • *ptrRate = 66; is illegal because ptrRate is const int * (pointer-to-const); the pointee is read-only.
  • ptrY = &vals[0]; is illegal because ptrY was declared int * const (the pointer itself is const; it cannot be reassigned).
  • ptrZ = &vals[0]; and *ptrZ = 7; are illegal because ptrZ is const int * const (neither the pointee nor the pointer may be changed). These cause compile-time errors, so code will not produce outputs past the first error unless those lines are removed or fixed.

Practical debugging approach (what , and were driving at): compile the code in an online C++ fiddle, read the first compiler error, fix or comment out that line, recompile, and repeat. To allow the later prints either remove the invalid assignments or change the declarations (for example, drop const where modification is intended, or remove the const on the pointer if reassignment is needed). Also avoid dereferencing a null pointer (printing the pointer itself is fine; *ptr when ptr is NULL will crash).

Recommended Answers

All 14 Replies

Did you run that code? What output did you get?

It gets errors, I just did it by hand...

Your code is so incomplete that no one can try it. Example, line 56 and on do nothing. Why have those lines?

Tip? Go put it into some C++ online fiddle and see what happens. Then you can share the fiddle here.

Well, I didn't do write that code first of all, it's just supposed to be an example my professor put. Line 56 doesn't matter then. Forget about the stuff that doesn't work because that doesn't matter...Can you please just check the other stuff without compiling it??

Yup, other lines give me errors in my head too. Same as JC above noted. I think you should re-read the assignment and go ahed with a working app. Again, use some C++ fiddle. If you never used a fiddle, just say so.

It's just a review example for an exam, not an assignment. I've never used C++ fiddle either.

I don't think it's supposed to be as complicated as we are making it though..

OK, you've never used a C++ fiddle. As a student you can now learn a few of the most powerful tools I've used over the years.

  1. Google. Google C++ fiddle and then
  2. Go to the fiddle and put in the code and try it out to see if it's right.

It's not complicated at all. You only need to do your search and work.

which one is it? when I click c++ it shows me to compileonline or gcc.gobalt??

then it just takes me to tutorialspoint...

I've never heard of or used a fiddle before either. Just googled "C++ fiddle", picked the top result, then kept clicking my options for C++ and within 15 seconds, I got to a screen where I could copy and paste your code and compile. Showed the errors, commented the bad lines out, recompiled, then executed. Less than a minute total.

commented: Let me show you the power of the dark fiddle. (Vader) +10

Oh, I went on it but it just shows me the errors..How does it take out bad lines or whatever?

Oh, I went on it but it just shows me the errors..How does it take out bad lines or whatever?

OK, so you DID find it, you were able to copy and paste the code in, got it to compile, and saw the errors.

As far as "taking out bad lines or whatever", how would you do it if you had your computer/compiler/text editor/IDE right in front of you? You'd do one of three things.

  1. Fix the bad lines.
  2. Comment out the bad lines.
  3. Erase the bad lines.

Then you'd compile again. When you got no errors after compiling, you'd run it. Same here.

I agree with this post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.