Having trouble with pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • v0xx
    New Member
    • Oct 2009
    • 4

    Having trouble with pointers

    I am rather new to programming and to C++, and one concept I can't seem to wrap my head around is pointers and how to use them syntactically. I can comprehend the "int a=1; int *b=&a; int c=*b" line of reasoning perfectly fine, although when you add in pointer arithmetic, arrays and passing pointers between functions, I quickly get lost.

    I decided to try to get some practice and make a simple toupper() function here, but I'm running into compiling errors no matter what I do. The code and the current error (during linking) can be viewed here: http://codepad.org/XJCI9Kid

    Any help you guys could offer me would be greatly appreciated. Thanks.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You already know how to use pointers. It is same as writing an address of a house on a piece of paper so you know where to go. The piece of paper is the pointer. The address you wrote on the paper is the contents of the pointer. And the house is the thing pointed at.

    To do this in C++ you use this syntax:

    Code:
    struct House
    {
        int Value;
    };
    
    House theHouse;           //the actual house
    House* ptr;                   //the piece of paper. This is the pointer variable.
    ptr = &theHouse;          //write the address of a House on the paper
    
    cout << theHouse.Value;  //access the Value using the object
    cout << ptr->Value;          //access the Value using the pointer
    cout << (*ptr).Value;        //access the Value dereferencing the pointer back to an object.
    Pointers are used to make the code useable with any House object. They are, however, a source of confusion and error so C++ tends to use references instead.

    Pointers come from C and were designed to look like this:

    Code:
    int A;               //A is an int
    int A,B            // A is an int and B is an int
    int A,B,*C       //A is an int, B is an int and *C is an int
    C is the pointer variable. To use it as the thing it points at you code *C whereas to work woith the address of the thing it points at you use C.

    In your code you just need to know if you are working with thing itself or just its address.

    Comment

    • v0xx
      New Member
      • Oct 2009
      • 4

      #3
      Hi weaknessforcats ,

      I appreciate your reply. I do fully understand how pointers work in theory. For example, since I want to take a string and modify it in a different function - since I know I cannot pass an array to a function directly - I am aware that I must use pointers to work in that kind of fashion. I was hoping you could take a look at my code directly and maybe point out where I'm making the mistake - because I can't see it.

      I did improve my code a bit (I think) so that it compiles now, though when printing the string after calling toUpper(), it is totally blank. Now I'm not really sure if this is an issue with my misuse of pointers, or if I am incorrectly using my bit mask, or if there's another underlying issue here.

      The new code can be viewed here: http://codepad.org/N5kMK6B0

      I'd appreciate it if you could look it over. Thanks.
      Edit: I got it working now. It was simply a case of doing
      Code:
      case 1:
      instead of
      Code:
      case '1':
      ...
      (also, I was using the bitmask incorrectly as well but it became significantly easier to fix after the function actually started being called)
      Last edited by v0xx; Oct 6 '09, 05:52 PM. Reason: i hate my life

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You only want to add 0x20 to str[i] if str[i] is between 65 and 90 to convert to lower case. Otherwise, just leave the character alone. It is already in lower case or is a digit or punctuation or some such.

        Your function is called goUpper and probably needs to be called goLower.

        Also i is a char. That limits your string lengths to 127. I would use an int.

        Comment

        • v0xx
          New Member
          • Oct 2009
          • 4

          #5
          I got all the functions working a minute ago:


          Thanks for the suggestion, i'll use int in the future.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by v0xx
            I got all the functions working a minute ago:
            http://codepad.org/jVcG4BPQ
            Those functions do not work! They still have the problems enumerated by weaknessforcats two replies ago.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Originally posted by v0xx
              I got all the functions working a minute ago:
              http://codepad.org/jVcG4BPQ
              By the way, your program uses function gets to acquire a string from the user. This function is deprecated because it provides no way for your program to protect itself from a user who types in a string longer than will fit in the str array. You should get out of the habit of using gets.

              Comment

              • v0xx
                New Member
                • Oct 2009
                • 4

                #8
                Thanks for the responses. Edited it one more time.



                I could have made a version of isalpha() too, but it is 12:30am here and I have been looking between some different tutorials, the internet and my compiler for over 14 hours straight now and I think it's time to go home...

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Originally posted by v0xx
                  What do you mean? The only issue that technically still remains is that punctuation will be butchered in goUpper() and switch(). If I really cared about that aspect I'd just call isalpha().
                  Nonprintable characters, punctuation and numeric digits are butchered by goUpper, goLower, and swap. Most worrisome, goUpper and swap will convert a space character into a null -- thereby truncating the string.

                  You're right; if you don't care about this aspect then there isn't a problem.

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Originally posted by donbock
                    Nonprintable characters, punctuation and numeric digits are butchered by goUpper, goLower, and swap. Most worrisome, goUpper and swap will convert a space character into a null -- thereby truncating the string.

                    You're right; if you don't care about this aspect then there isn't a problem.
                    Give the guy a break - he's tired! :P

                    Comment

                    Working...