C++ help

hi Sup Forums,
I have a final exam tomorrow for my Into C++ class and I'm going over the practice exam my professor gave out. One of the questions asks what the output of the code in the image is, but when I try to test it out, it says "a value of type "double *" cannot be assigned to an entity of type "double"".

Did my teacher mess this up? Is it a trick question? Can I fix something in the code so that I can see the intended result?

Please help. Thank you.

Other urls found in this thread:

ideone.com/plq62i
twitter.com/NSFWRedditVideo

first post on Sup Forums I'll try and post the code.
int main () {
double *p,*q;
double i=2.3, k =4.3;
p=new double[2];
*p=i; q=p;
cout

"p[1] = &i" is the culprit (incompatible types). You should see a result (not necessarily that which is intended) by replacing it with "p[1] = (double)&i" (casting the double* to a double)

I attempted to do that and it still says it's an invalid type conversion.

also tried to do static_cast(&i);

Piggy backing for my own question;
I have a major assignment due soon in which we have to parse an entire (oxford) dictionary file into an array where every word and its definition are a separate object in order to qucik search the word. Any idea on where to start?

Your teacher should rethink his approach.
Ugly pointer manipulation and naked new/delete is not something you show on the first lesson.
The whole community is trying to move away from that...

Just buffer it senpai, you dont want segfaults

It's so confusing. This is her first semester teaching here. she's from some middle eastern country. I don't understand pointers at all no matter how hard I try and these kinds of quiz questions just mess me up even more.

Did they specifically say "an array"?
Because when you hear "dictionary" or "key/value pair" you should think "map" or any other associative container.

With an array you'd need to store std::pair and sort/search with a custom compare function which only looks at the first element of each pair.
It's doable but completely retarded compared to using std::map.

>p=new double[2];
>p[1] = &i;
wew lad
you're trying to store a reference (unsigned 32/64bit int) inside a double
the reference seems to get converted to a double* which explains the error

that'll be 1000rupees thank you pajeet

I don't even see a delete there, lol.

I guess it's fine to start using naked new early, even though you don't want to use it as a general rule. It's easier to understand than make_unique and make_shared

>p[1] = &i
This bit is what's causing the error I think. Try gettong rid of the &. Just a stab in the dark, I haven't thought about it much.

The unary & operator takes a pointer of, not a reference to

I think she is expecting a legitimate output on the answer though. She never gives "error"/no output answers on her quizes.

To understand pointers you need to understand the basic memory model of a computer.
Basically, a very wide array of bytes starting from index 0 to over 9000.
A pointer is simply an address (offset) in memory.
Operator& (unary) gives you the address of a thing (usually a variable).
Operator* (unary) is the reverse operation, gives the value of the stuff stored at a given address.

I understand that much. It's just when I have so many things pointing to so many other things I get lost really quickly.

It's probably just an oversight. It's easy to make these kinds of mistakes when you're not really thinking about what you're doing.

Bjarne himself recommends to start directly with make_unique and either unique_ptr or simply the "auto" keyword

that's what I'm thinking. Do you have any idea what the intended result was supposed to be?

Well shit, I'm BTFO.

change p[1] = &i to p[1] = i and see if it compiles. If it does, run it.

This is when you get your paper and pencil and draw that shit out

lemme just do your homework OP

ideone.com/plq62i

That's why modern C++ practices suggest you don't juggle with more than a couple of pointers at a time.
It just makes the code harder to read for no particular benefit...

yeah that's what I did and what I got... But I feel like she's trying to indicate something by the &, even if the compiler doesn't technically allow it to happen. Like that she's trying to say that p[1] will change to 5.2 when i changes or something like that.

For that to work, p would have to be of type double*[]
i.e. a pointer to an array, which holds pointers to doubles anywhere.

It's most likely an oversight. That said the code quality here is pretty poor.

I tried it with 3 different compilers they all throw the same error and clang even recommends leaving off the &.

The only thing I can think of is she fucked up and didn't check after making the & typo or she used some dinosaur compiler like devc++. Just complain to the teacher on the class discussion board or in person if you get it wrong.
(output on )

It's most likely just a logic error on the part of the programmer. If you get a question like this on the exam, that similarly also doesn't compile, don't try to guess their intentions and spout off some random answer. Just explain clearly what you think is wrong with the code, how you would fix it based on what you assume it's supposed to do, and then explain the output of the fixed version.

They said an array or other std container so a a vector I think is more likely there are said to be more than 106000 objects

I love when Sup Forums helps students learn. Makes me feel less like shit
t. TA for a professor at my uni

p[1] = &i;

Makes zero sense. For a few reasons:
>already used and proved p is a double array
>tries putting the same value in again, but this time, as a pointer
>has the value k that's unused, I can only assume it's used later

He was either supposed to do one of 2 things by the looks:
p = &i;

Or

p[1] = i;

I'm guessing the latter. But his intentions are unclear.

The actual solution is to walk up, bitch slap him, and tell him you don't get paid enough to debug his shitty code.

>SHUT THE FUCK UP!

It's a bug alright - in the code. How long have you been a
teacher? And you *still* haven't learnt the first rule of coding?

If a change results in the compiler throwing an error, it's a bug in the
code. We never EVER blame the compiler. How hard can this be to
understand?

To make matters worse, your exam is clearly total and utter
CRAP even if its code did compile. A value of type "double *" cannot be assigned to an entity of type "double". It never has been possible, and never will be.

Fix your f*cking "exam", because it is obviously broken.
And fix your approach to programming.

user.

This is why they should teach you fuckers C BEFORE C++.
There's much less to get your head around and it just works. C++ throws you in the deep end and overwhelms the new user with it's use of objects in the standard library and dumb syntax to go with them.

You will see in time that pointers are nice and easy to deal with. Bad coders, on the other hand, are the reason you will hate them.

why on earth would you think that I'm the instructor? did you even read the post?

yeah I gotta say I'm pretty overwhelmed.

std::map *is* a standard container. Use it.

This, and brace for your teacher answer.
If he insist on this being correct or even remotely a good introduction you should change course and/or university.

>We never EVER blame the compiler.
I've had g++ segmentation fault on me providing absolutely no information about mistakes in the code. Checkmate atheist.

Are you fucking retarded m8, maybe try actually reading OP's post instead of making shit up to vent your frustration

It's entirely possible to teach C++ step by step with no introduction to C whatsoever.
It's just that half the so called "C++ teachers" are just seeing it as "C with classes", a practice that every serious C++ programmer has been considering retarded for decades

Agreed.

There is way too much legacy shit in C++ to be taught to absolute beginners.

"& means address of, but here in the function prototype it means pass this value by reference, yes you can use pointers to do the same thing, just remember both"

this makes 0 sense to someone new to C++ and C

>they don't read the linux mailing list

It's obviously version of a Linus Torvalds rant.
It is supposed to be written by the OP to the teacher.

Can anyone help with this line of code; It errors out with long begin, end, and lineNumber. Not sure quite where I've gone wrong.

vector theDictionary;

void Dictionary::loadDictionary()
{
string filename = "dictionary.txt";
long begin, end, lineNumber;
string record;
cout

bumping

> // copy the read in line of text to the Word record
> currentRecord.getWord() = record;

Not sure what you meant but this looks fishy.
Does getWord() return a reference?

so the getWord() function returns the string contained in it called word but it I haven't referenced it

Which means it returns a temporary copy of whatever the underlying object is in Word, replace the content by what you read and then discards it.
You should have a set() to mirror your get() method. There might be better alternatives from a design point of view but for now it'll suffice.

Can you guys c++ experts help me with my thread:
I'm sure it's super simple but I've never used that "initializer_list", thank you!

Yeah see i have a setter in my header file and another source but I don't know where it fits into this file.

bump again due to being seriously lost

>One of the questions asks what the output of the code in the image is

Obviously, the output is a compiler error.

Don't forget about & also meaning references. It's a clusterfuck.

Is setWord() defined? Is there a function body you can look at or is defining it part of the exercise?

or maybe he forgot a reinterpret_cast(&i) :^)

>Ugly pointer manipulation and naked new/delete is not something you show on the first lesson.
>The whole community is trying to move away from that...

Pointers are one of the first things you need to teach with both C and C++. They are one of the primitive building blocks for constructing other types. Even if std::unique_ptr is better most of the time, if you want to improve understanding, it makes sense to teach the primitive from which the abstraction is constructed. It will also serve to demonstrate WHY we use the abstraction over the primitive.

setWord is defined by me as part of the exercise but any help you can give would be ace (if you can't tell I'm very new to c++):
in the class it is defined as
void setWord(string wordIn);
and later is written as
void Word::setWord(string wordIn) {
word = wordIn;
}

fucking email your prof and ask for clarification then.