[ 3 / a / adv / an / asp / cgl / ck / co / diy / fa / g / gd / int / jp / k / lit / m / mlp / mu / n / o / out / p / po / sci / sp / tg / toy / trv / tv / v / vg / vp / vr / w / wsg / x]

/sci/ - Science & Math

<< back to board
[Delete this thread]

c++ 06/27/14(Fri)03:17 UTC+1 No.6613513 Report

Yes, I know c++ is despised programers language, but I can't comprehend even that simple one
Can anybody help me figure out what the hell is wrong with this part of code

struct complex* carray;
double x;
int i;
carray=malloc (sizeof (complex));
Equal sign is underlined and says
a value of type void* cannot be assigned to an entity of type complex*

Anonymous 06/27/14(Fri)03:19 UTC+1 No.6613516 Report

Programming goes on /g/.
c++ 06/27/14(Fri)03:20 UTC+1 No.6613518 Report

Oh, didn't know that
Anonymous 06/27/14(Fri)03:23 UTC+1 No.6613523 Report

You have to define the structure of your "complex" struct.

Here is how you do it: http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29
Anonymous 06/27/14(Fri)03:26 UTC+1 No.6613528 Report

I suggest you read the 4chan FAQ before posting again
Anonymous 06/27/14(Fri)03:58 UTC+1 No.6613570 Report

>I know c++ is despised programers language

No it's not. Stay the fuck out of /g/ and its retarded CS major population.

> carray=malloc (sizeof (complex));

Why the fuck are you using malloc in C++ when you don't have too?

Anyway you need to cast it with static_cast<complex*>( ) before equating it as malloc/calloc/realloc all return void*. Also you forgot to multiply a size of that array so you only have 1 element worth of space allocated. (

Safety tip: sizeof(type)*array_length can overflow q size_t so check for it or use calloc when users/others have control over the length parameter

Protip: "new" fixes this and is much clearer as you just write
carray=new complex[1];
carray=new complex;

and just use "delete [] carray;" or "delete carray;" respectfully when you're done with it.
Anonymous 06/27/14(Fri)04:01 UTC+1 No.6613573 Report

pls combine /g/ and /sci/ together. They're both nerds who like logic and math.
Anonymous 06/27/14(Fri)04:03 UTC+1 No.6613576 Report

>They're both nerds who like logic and math

But >>>/g/nu/retards can't into math
Anonymous 06/27/14(Fri)04:21 UTC+1 No.6613598 Report

Timecube; proven by "Unseen Side" 06/27/14(Fri)04:24 UTC+1 No.6613605 Report

seems totally unbiased
Anonymous 06/27/14(Fri)04:28 UTC+1 No.6613609 Report

No fuck /g/ they can go die in hole
Anonymous 06/27/14(Fri)04:30 UTC+1 No.6613611 Report

I'd still appreciate code tags though
Anonymous 06/27/14(Fri)04:38 UTC+1 No.6613636 Report
File: 3.jpg-(90x125)
Anonymous 06/27/14(Fri)04:44 UTC+1 No.6613656 Report

I really think C++ is a terrible language. I know I'm not alone.

The only reason people use it at this point is because it has years of momentum behind it.
Anonymous 06/27/14(Fri)04:45 UTC+1 No.6613659 Report

Anonymous 06/27/14(Fri)04:56 UTC+1 No.6613681 Report

>I really think C++ is a terrible language. I know I'm not alone.

Because you're either a retard or one of those autists that believe there should only be one way of doing something (theirs) and everyone else must be prevented from doing it any other way that's killing modern software.
Anonymous 06/27/14(Fri)05:01 UTC+1 No.6613690 Report

well, nobody seems to have answered OPs question.

You need to cast the malloc:

carray=(complex *) malloc(sizeof(complex));

This is more like C than C++. Good luck OP.
Anonymous 06/27/14(Fri)05:03 UTC+1 No.6613694 Report

OP is a fucking tard

Anonymous 06/27/14(Fri)05:10 UTC+1 No.6613701 Report

Then he will need luck even more.
All the content on this website comes from 4chan.org. 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. 4chanArchive is not affiliated with 4chan.