Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ is crazy difficult.

  1. Oct 25, 2012 #1
    I'm into my second year now at uni and after learning Java for the past year, the course is now introducing us to C++. We're still doing java but now C++ aswell.

    I don't know if I'm the only one but C++ is sooooo much harder than java, today after just our second lesson we're being introduced to header files and coding our own. it took my 2 hours just to figure out how to right a program that read from a file, command line arguments and then added up the number of arguments and printed out the sum of those arguments.

    The thing is our lecturer uploads the solutions to the problems 1 week after the problem was given to us, so I can always wait until the solution is provided and then try to peice together what is going on, but I just don't understand it.

    Everything seems to be moving at such a fast past and I'm just slipping behind, I'm 1 week behind so far and tomorrow is the next exercise and I still can't complete last weeks one :/
     
  2. jcsd
  3. Oct 25, 2012 #2

    phinds

    User Avatar
    Gold Member
    2016 Award

    Is there a question in here somewhere or did you just need to get that off your chest? :smile:
     
  4. Oct 25, 2012 #3
    Mainly just to get it off my chest lol. Also for someone to tell me that they were in my position and it's just a hurdle that you need to overcome and once you start doing more C++ you'll get used it it?

    I dunno really =(
     
  5. Oct 25, 2012 #4

    rcgldr

    User Avatar
    Homework Helper

    Seems the main issue here is pointers, and in this case, a pointer to an array of pointers to the arguments, often declared as a pointer to a pointer to a character ( ... , char **argv ) or sometimes as an array of pointers to characters ( ... , char *argv[] ). The end result is basically the same, since C / C++ treats arrays and pointers similarly.
     
    Last edited: Oct 25, 2012
  6. Oct 25, 2012 #5
    Pointers can be non-intuitive to the Java programmer.

    But headers already after week 2? That's a little too accelerated.
     
  7. Oct 25, 2012 #6
    This has always mystified me since Java has pointers too: Java references are, in fact, just pointers.

    In my experience there are a couple of things in particular that seem to blow Java programmers minds when they come to programming in C++:

    • the lack of a garbage collector and the resultant necessity to engage in manual memory management for anything allocated on the heap;
    • the ability to store actual objects - not just pointers or references to objects - as members in other objects.

    The first isn't actually a problem since the advent of smart pointers and RAII mean that nobody beyond library writers should really need to worry about memory management. The second is fixed simply with practice.
     
  8. Oct 25, 2012 #7

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    You can't write any C++ (or even C) programs without using the predefined headers, and you can't write any non-trivial code that is sensibly stuctured without writing your own headers.

    If you already know Java, this probably isn't a first programming course that spends two weeks explaining what "i = i + 1" means. I would have thought you would be meeting headers within in the first two hours, not the first two weeks.
     
  9. Oct 25, 2012 #8

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Of course, that doesn't stop some people from teaching/learning C++ from a 25-year old perspective on using the language.

    Because of this, "I'm learning C++" can mean a very wide variety of things. :frown:
     
  10. Oct 25, 2012 #9

    rcgldr

    User Avatar
    Homework Helper

    I think the syntax is part of the issue, since it doesn't consistently flow left to right:

    char achar[]; // array of characters
    char * pchar; // pointer to character
    char ** ppchar; // pointer to pointer to character
    char * papchar[]; // pointer to array of pointers to characters

    Then there's the issue that pointers can be used similar to arrays:

    ... *(pchar + 1) is the same as ... pchar[1]

    The syntax for pointer to function isn't obvious either:

    int (*pfun)(void); // ptr to function wtih no parameters and returning integer
     
  11. Oct 25, 2012 #10

    pwsnafu

    User Avatar
    Science Advisor

    Ah, the old days of programming when people said "I program in C++ because Java is too slow". I was just talking to a programmer who was saying "I program in Java because Ruby is too slow" :rofl:
     
  12. Oct 25, 2012 #11
    Yesterday I got lost in my own C++ code. I totally forgot WTF I was doing. So I hear ya... My two semesters of Java was a walk in the park compared to my first semester of C++!

    But that means you're learning, too. :p
     
  13. Oct 25, 2012 #12

    jhae2.718

    User Avatar
    Gold Member

    Things like
    Code (Text):

    [color=#B00040]char[/color] [color=#666666]*[/color]([color=#666666]*[/color]c[[color=#666666]10[/color]])([color=#B00040]int[/color] [color=#666666]**[/color]p);

     
    get interesting...
     
  14. Oct 25, 2012 #13

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    That's because it doesn't flow left to right. For the most part, it flows right to left. In general it swings from center to right to left to right to left to ... You just have to get into the swing of things.

    Code (Text):

    char const * foo;
    // foo is a pointer to a const char.
    // Simply read from right to left.

    char * const bar;
    // bar is a const pointer to a char.
    // Once again, just read right to left.

    int *(*baz(double red_herring))[];
    // baz is a pointer to a function that takes a double and
    // returns a pointer to an array of pointers to integers.
    // Now we really do need to get into the swing of things.
    // How to read this:
    //   1. Find the identifier: It's baz. That "red_herring" is a red herring.
    //      baz is ...
    //   2. Read to the right: ... a function that takes a double.
    //   3. Stop at the unbalanced close paren and read to the left:
    //       ... a pointer to a function that takes a double ...
    //   4. Stop at the open paren, swing back to reading to the right:
    //      ... that returns an array ...
    //   5. Stop at the semicolon, swing back to reading to the left:
    //       ... of pointers to integers.
    // Easy-peasy! :)
     
     
    Last edited: Oct 25, 2012
  15. Oct 25, 2012 #14

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    c is an array of 10 function pointers each of which takes a pointer to a pointer to an int as an argument and returns a pointer to a char.
     
  16. Oct 25, 2012 #15

    rcgldr

    User Avatar
    Homework Helper

    Except this is also allowed:

    Code (Text):

    const char * foo;
    // foo is a pointer to a const char.
     
    There's also two ways of accessing members via pointers to structures or classes:

    (*pobject).member or pobject->member

    Then C++ added another alternative with function parameters accessed by reference

    using pointer:

    Code (Text):

        fun(&object);
    // ...
    void fun (OBJECT * pobject)
    {
    ...  pobject->member  ...
    }
     
    using reference.

    Code (Text):

        fun(object);
    // ...
    void fun (OBJECT &object) // parameter is actually pointer to object
                              // allowing object to be modified by fun
    {
    ...  object.member  ...
    }
     
    With reference method, you can't tell from the call if the function can modify object (pass by reference instead of pass by value), you have to look at the function or it's prototype, which could be located in a separate header file.

    The classic mistake if(x = 1)... when you meant if(x == 1) ...

    One of the cases where whitespace matters:

    a+*ptr2b (a plus (*ptr2b))
    a-*ptr2b (a minus (*ptr2b))
    a**ptr2b (a times (*ptr2b))
    a/*ptr2b (/* comments out all remaining code until */ if there is any)
     
    Last edited: Oct 25, 2012
  17. Oct 25, 2012 #16
    Here's a strange pointer-related "feature" of C++. Suppose we define a five-element array of ints, a, to store the integers from 0 to 4. Everyone knows that the following prints '4' to stdout:

    Code (Text):

    std::cout << a[4] << std::endl;
     
    If you want to mess with someone's head, the same result can be achieved using the following code:

    Code (Text):

    std::cout << 4[a] << std::endl;
     
    You're well on your way to understanding pointers and arrays if you can wrap your head around why this works.
     
    Last edited: Oct 25, 2012
  18. Oct 25, 2012 #17

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    That's a misfeature, not a feature, and it's an inherited misfeature from C. It's due to the extremely weak support in C for arrays. Unfortunately, this is one of the misfeatures that makes C a bit poor for scientific computing.

    All in my opinion, of course.
     
  19. Oct 25, 2012 #18

    Mark44

    Staff: Mentor

    Simple - here's the definition of that array.
    Code (Text):
     int 4[a] = {0, 1, 2, 3, 4};
     
    :wink:
     
  20. Oct 25, 2012 #19

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    No. It's worse than that, Mark. 4[a] and a[4] as an lvalue or rvalue refer to exactly the same thing. It's because C treats the operator [] as syntactic sugar for pointer addition and deferencing. a[4] is just a sugar coated version of *(a+4), and 4[a] is just a sugar coated version of *(4+a).
     
  21. Oct 25, 2012 #20

    jhae2.718

    User Avatar
    Gold Member

    If you have a copy of Expert C Programming: Deep C Secrets, the commutativity of the subscript operator is discussed some on p. 243.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ is crazy difficult.
  1. VC++ has gone crazy! (Replies: 6)

  2. C or C++? (Replies: 8)

Loading...