Pointers and structs confusing me

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 652 Views - Last Post: 04 February 2018 - 06:03 PM Rate Topic: -----

#16 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 504
  • Joined: 09-September 13

Re: Pointers and structs confusing me

Posted 04 February 2018 - 01:49 PM

i get can't return void function or something like that when i don't cast the link. but it passed all the tests so I'm guessing it works :P

/Users/Ben255/Downloads/Chap4/ListbastedCStack/student1cstackl.cpp:48: error: cannot initialize a variable of type 'struct Link *' with an rvalue of type 'void *'
   struct Link * newLink =  malloc(sizeof(struct Link*));
                 ^          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6016
  • View blog
  • Posts: 20,691
  • Joined: 05-May 12

Re: Pointers and structs confusing me

Posted 04 February 2018 - 01:51 PM

That is because you are compiling your C code as C++. Note the use of your .CPP file extension which tells the compiler that it is dealing with a C++ file rather than a C file which would have a .C file extension.
Was This Post Helpful? 0
  • +
  • -

#18 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 504
  • Joined: 09-September 13

Re: Pointers and structs confusing me

Posted 04 February 2018 - 01:52 PM

View Postsepp2k, on 04 February 2018 - 09:41 PM, said:

struct Link * newLink = (struct Link *)  malloc(sizeof(struct Link*));


This is wrong. sizeof(struct Link*) is the size of a pointer (so 4 on 32-bit platforms and 8 on 64-bit platforms) and that's not enough memory to store a Link struct in it.


hmm.. weird that it passed all the tests. i changed it to just a struct Link now and it also worked. think that will be a better approach. thanks
Was This Post Helpful? 0
  • +
  • -

#19 jimblumberg  Icon User is online

  • member icon

Reputation: 5398
  • View blog
  • Posts: 16,829
  • Joined: 25-December 09

Re: Pointers and structs confusing me

Posted 04 February 2018 - 01:52 PM

Quote

i get can't return void function or something like that when i don't cast the link.

Then you need to stop using a C++ compiler to compile C code!

Quote

it passed all the tests so I'm guessing it works


I'd say you just got lucky. Isn't undefined behavior wonderful?

Jim
Was This Post Helpful? 1
  • +
  • -

#20 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 504
  • Joined: 09-September 13

Re: Pointers and structs confusing me

Posted 04 February 2018 - 01:55 PM

View PostSkydiver, on 04 February 2018 - 09:51 PM, said:

That is because you are compiling your C code as C++. Note the use of your .CPP file extension which tells the compiler that it is dealing with a C++ file rather than a C file which would have a .C file extension.


yah we have to write the program in both C and C++. I'm working with the C++ part atm. Dunno this is how he set it up and he wanted us to use the QT editor to make it easier for him to grade the stuff.
But this is a course in algorithms but there is a lot of C and C++ how to in the course also since we just got started with it.
Was This Post Helpful? 0
  • +
  • -

#21 jimblumberg  Icon User is online

  • member icon

Reputation: 5398
  • View blog
  • Posts: 16,829
  • Joined: 25-December 09

Re: Pointers and structs confusing me

Posted 04 February 2018 - 02:08 PM

Quote

yah we have to write the program in both C and C++. I'm working with the C++ part atm.

Then you shouldn't be using malloc, you should be either using safe pointers or new/delete.

Jim
Was This Post Helpful? 0
  • +
  • -

#22 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 504
  • Joined: 09-September 13

Re: Pointers and structs confusing me

Posted 04 February 2018 - 02:10 PM

Yes I'm using that on the C++ part of the code.

This post has been edited by Skydiver: 04 February 2018 - 06:07 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#23 jimblumberg  Icon User is online

  • member icon

Reputation: 5398
  • View blog
  • Posts: 16,829
  • Joined: 25-December 09

Re: Pointers and structs confusing me

Posted 04 February 2018 - 02:13 PM

Quote

Yes I'm using that on the C++ part of the code.

Where? I don't see any calls to new or delete anywhere in the C++ code you supplied. If you're compiling the code with a C++ compiler it is a C++ program not a C program. C and C++ are different languages even though they contain many of the same constructs.

Also if you're writing a C++ program you really should be using C++ containers instead of rolling your own.

Edit: Also using new delete will save you many problems because new allocates memory for the objects not memory for bytes.

Jim

This post has been edited by jimblumberg: 04 February 2018 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#24 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 504
  • Joined: 09-September 13

Re: Pointers and structs confusing me

Posted 04 February 2018 - 02:26 PM

my instruction were only open X file in qt editor and complete the function skeletons that have been supplied for us. So i had no choice to compile in a C editor. it all just has to be writing in the C language that is the only specification. so i have no control over anything that you are commenting on :P

void StackL::push(float value){
    Link * newLink = new Link();
    newLink->_pNext = this->_pTop;
    newLink->_data = value;
    this->_size ++;
    this->_pTop = newLink;
}


this is my C++ push code
Was This Post Helpful? 0
  • +
  • -

#25 jimblumberg  Icon User is online

  • member icon

Reputation: 5398
  • View blog
  • Posts: 16,829
  • Joined: 25-December 09

Re: Pointers and structs confusing me

Posted 04 February 2018 - 02:45 PM

Well then good luck. Mixing C malloc and C++ new is a sure way for your program to go bonkers good luck finding out what your doing wrong.

Jim
Was This Post Helpful? 0
  • +
  • -

#26 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 504
  • Joined: 09-September 13

Re: Pointers and structs confusing me

Posted 04 February 2018 - 02:51 PM

thanks.
But I'm not mixing them both in one program. i got two different programs that are doing the same thing. one should be written in C and the other in C++. But i got them both to work now so I'm happy to turn it in 1 hour from the deadline :D
Was This Post Helpful? 0
  • +
  • -

#27 jimblumberg  Icon User is online

  • member icon

Reputation: 5398
  • View blog
  • Posts: 16,829
  • Joined: 25-December 09

Re: Pointers and structs confusing me

Posted 04 February 2018 - 03:02 PM

Quote

one should be written in C and the other in C++.

But you're not writing C code, all you have is C++ code. To get a C program you need to compile it with a C compiler.

And to top it off your writing piss poor C code, the required cast for malloc is but one indication, and piss poor C++ code, using new/delete instead of safer alternatives, so what you have the worst of both languages, good luck with that.

Jim
Was This Post Helpful? 0
  • +
  • -

#28 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 504
  • Joined: 09-September 13

Re: Pointers and structs confusing me

Posted 04 February 2018 - 03:04 PM

i expect nothing else after 1 month of C and 1 week of C++

This post has been edited by Skydiver: 04 February 2018 - 05:57 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#29 jimblumberg  Icon User is online

  • member icon

Reputation: 5398
  • View blog
  • Posts: 16,829
  • Joined: 25-December 09

Re: Pointers and structs confusing me

Posted 04 February 2018 - 03:11 PM

I'm not blaming you for the piss poor code, it is your instructor making things easier for himself instead of properly teaching the subject. He needs to pick either C or C++ but trying to do both at the same time is a horrible way to go. It doesn't help to teach or reinforce good practices in either language and leads to you learning the worst practices of both languages. I hope you're (or your parents) aren't paying an arm and a leg for this class.


Jim

This post has been edited by Skydiver: 04 February 2018 - 05:59 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#30 ben255  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 504
  • Joined: 09-September 13

Re: Pointers and structs confusing me

Posted 04 February 2018 - 03:16 PM

school is free here in sweden so its np. But dunno rly after this course we are going to start introduction to c++. this course used to be a 5th term course but they changed the layout and now we have no c++ background so he wanted us to get acquainted to pointers and structs and objects in like 1 weeks time.
SO yah i agree the layout is a little wierd but there is 10 more weeks for this course and i think it will be more algorithms and datastructures in the future.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3