[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

I am trying to make c++ pass an array into a function by referencing

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 20
Thread images: 2

I am trying to make c++ pass an array into a function by referencing the first element of the arrays address.
Screenshot is where I am currently at. Highlighted in red is the current errors.

NOTE: the code is from a book (i think).
>>
>>59582748
First year CS student? I literally have to do the same thing in Pascal (not sure how)
>>
>>59582770
I am not doing this for school.
I am doing this for learning + maybe it will help with future school assignments.
>>
Think that should just be
const float *g
dude.
No
[&0]
there.
>>
http://pastebin.com/2WbZXqUS

heres a pastebin
>>
>>59582788
But the way arrays work with the compiler is that the compiler is just taking the address of the first element of g.
So why can't I write g[&0]?>
>>
>>59582748
Last error: 18. That's what's up. You're using an rvalue.
>>
>>59582799
it has to do with rvalues and shit

The g pointer is already pointing towards the first element. Just use that
>>
>>59582799
&g[0] is the address of the first element, I think.

You can't specify the size of an array in a function signature. Numbers in arrays in function signatures make no sense.
>>
>>59582807
But I want to pass the address of the first element of g. I dont know what an r or L value is , but I'm going to ask is there any way to pass the address as an lvalue?
>>
>>59582828
I don't want to use that.
This isn't for a program I have to write, work, school, or anything else.
I just want to pass the g pointer that is pointing towards the first element without writing const float* g.
>>
>>59582836
>is there any way to pass the address as an lvalue?
This dude gave you the right answer. It's an lvalue. Lvalues are values that are left of the = sign.
>>59582788
An * can be used as an lvalue can't they? So here's a pointer to a pointer called g.
>>
>>59582799
this is trying to specify you want to access the element &0 in the array g[]. Unless &0 somehow coerces to an int, yer hooped.
>>
The meaning of
int * g


*g is an int (dereferencing g gives int)
g is an int* (g points to an int)
>>
File: cstudent.png (3KB, 594x316px) Image search: [Google]
cstudent.png
3KB, 594x316px
>>59582850
well it works if you do
>>
>>59582858
I meant i want to pass g[0]'s address without writing g.
Since all thats happening is that const float * g is grabbing the address of g[0] then i should be able to wrote g[&0]
>>
>>59582799
>g[&0]

Because 0 is a constant and doesn't have an address. Just write g[] or *g
>>
>>59583013
g[&0] is not anything. Arrays need an index that is a positive integer.
'&0' is semantically "the address of zero" it doesn't make sense.

If you really wanted the address of the first element in g, you'd use &g[0] but that doesn't work in a function header.
>>
>>59583013
I don't know. You're asking questions about design decisions in C++. I have no idea why you can't just say 'g[&0]'. I also have no idea why lvalues are required when clearly 'g[&0]' means 'g'. I would just accept the facts and move on. All the nitty-gritty details may come later.
>>
>>59583051
>>59583043
Yes. It would be &g[0].
Thread posts: 20
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.