Dereferencing Problems
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Dereferencing Problems

  1. #1
    Join Date
    Mar 2009
    Posts
    6

    Dereferencing Problems

    So, I'm getting a bit lost in the whole dereferencing idea. I understand the idea behind it, but I just can't get it to work.

    Code:
    // Author:      Josh Johnson
    // Date:        30 March 2009
    
    // Program switches the letters of a character array between two input bounds
    
    #include <iostream>      // commands program to use input and output stream
    #include <string>
    
    using namespace std;     // tells compiler to use standard names for things
    
    void reverse(char *inputStream, int lowerBound, int upperBound);
    void reverseAll(char *inputStream);
    
    int main() {
    
        int high, low, sLength;
        string s;
    
        cout << "\014" << flush;
    
        cout << "This program will switch the letters of an input between two ";
        cout << "inputted bounds.\nPlease input a string of characters:\n";
    
        getline (cin, s, '\n');
    
        sLength=(s.length()+1);
    
        // Convert String to Character Array
        char *name = new char[s.length()+1]();
        strcpy(name, s.c_str());
    
        cout << "\nNow select what range of characters you would like to switch.\n";
        cout << "Lower Bound: ";
        cin >> low;
        cout << "Upper Bound: ";
        cin >> high;
    
        reverse(name, low, high);
    
    return 0;
    }
    
    void reverse(char *inputString, int lowerBound, int upperBound){
    
        char temp;
        int low=lowerBound;
        int high=upperBound;
        int arraySize;
    
        arraySize=sizeof(inputString);
    
        char str[arraySize];
    
        for (int i=0; i<=arraySize; i++) {
            str[i]=inputString[i];
        }
    
        if (lowerBound<upperBound) {
            temp=str[lowerBound];
            str[lowerBound]=str[upperBound];
            str[upperBound]=temp;
    
            lowerBound++;
            upperBound--;
    
            reverse(str, low, high);
        }
        else {
            for (int j=0; j<=arraySize; j++) {
                cout << str[j];
            }
        }
    }
    
    void reverseAll(char *inputStream) {
    // To finish later
    }

    I'm getting the following errors:
    Code:
    hw13-1.cpp: In function `int main()':
    hw13-1.cpp:38: error: invalid type argument of `unary *'
    hw13-1.cpp: In function `void reverse(char**, int, int)':
    hw13-1.cpp:50: error: non-lvalue in assignment
    hw13-1.cpp:54: error: invalid conversion from `char*' to `char'
    hw13-1.cpp:55: error: non-lvalue in assignment
    hw13-1.cpp:56: error: non-lvalue in assignment
    hw13-1.cpp:61: error: invalid conversion from `char' to `char**'
    hw13-1.cpp:61: error:   initializing argument 1 of `void reverse(char**, int, int)'
    (for reference, line 61 is the last reverse function call)
    I guess I'm just mainly lost about when and where to (and to not) put the * and &

    Any help with this?
    Last edited by JoshJ; March 30th, 2009 at 01:55 AM.

  2. #2
    Join Date
    Mar 2001
    Posts
    2,366

    Re: Dereferencing Problems

    Code:
    reverse(inputString, low, high);
    Strings are a special case. In this case it is enough to put the var name because it is a string.
    Alternatives are "&" the address operator:
    Code:
    reverse((char*)&inputString[0], low, high);
    Here we pass in the address of the first char.
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Mar 2009
    Posts
    6

    Re: Dereferencing Problems

    I changed line 61 to what you suggested, but am still getting these errors:

    Code:
    hw13-1.cpp: In function `int main()':
    hw13-1.cpp:38: error: invalid type argument of `unary *'
    hw13-1.cpp: In function `void reverse(char**, int, int)':
    hw13-1.cpp:50: error: non-lvalue in assignment
    hw13-1.cpp:54: error: invalid conversion from `char*' to `char'
    hw13-1.cpp:55: error: non-lvalue in assignment
    hw13-1.cpp:56: error: non-lvalue in assignment
    hw13-1.cpp:61: error: cannot convert `char*' to `char**' for argument `1' to `void reverse(char**, int, int)'

  4. #4
    Join Date
    Mar 2001
    Posts
    2,366

    Re: Dereferencing Problems

    I am sorry but please post your entire .cpp file, otherwise it will take alot longer to help you.

    Thanks,
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    Join Date
    Mar 2001
    Posts
    2,366

    Re: Dereferencing Problems

    this needs to be changed to this:
    Code:
    void reverse(char *inputString, int lowerBound, int upperBound);//in the prototype
    //...and below
    void reverse(char *inputString, int lowerBound, int upperBound)//the definition
    {
    ;

    and
    Code:
    &str[i]=(*inputString[i]);
    to
    Code:
    str[i]=(inputString[i]);
    and
    Code:
    reverse(*inputString[50], low, high);
    to
    Code:
    reverse(inputString, low, high);
    Last edited by ahoodin; March 30th, 2009 at 12:53 AM.
    ahoodin
    To keep the plot moving, that's why.

  6. #6
    Join Date
    Mar 2009
    Posts
    6

    Re: Dereferencing Problems

    Ok, I edited the original post to show the entire program.

    And I tried those edits that you suggested, and I get this:
    Code:
    hw13-1.cpp: In function `int main()':
    hw13-1.cpp:38: error: invalid type argument of `unary *'
    hw13-1.cpp: In function `void reverse(char*, int, int)':
    hw13-1.cpp:50: error: non-lvalue in assignment
    hw13-1.cpp:54: error: invalid conversion from `char*' to `char'
    hw13-1.cpp:55: error: non-lvalue in assignment
    hw13-1.cpp:56: error: non-lvalue in assignment

  7. #7
    Join Date
    Mar 2001
    Posts
    2,366

    Re: Dereferencing Problems

    strings are different than other types. They have their own set of rules.
    ahoodin
    To keep the plot moving, that's why.

  8. #8
    Join Date
    Mar 2001
    Posts
    2,366

    Re: Dereferencing Problems

    Slight edit:
    Code:
    &str[i]=(*inputString[i]);
    to
    Code:
    str[i]=(inputString[i]);
    Also as I said to begin with you did not post your complete code starting with line 0, so it was harder.

    Although I see it now.
    ahoodin
    To keep the plot moving, that's why.

  9. #9
    Join Date
    Mar 2001
    Posts
    2,366

    Re: Dereferencing Problems

    You needed to change :
    Code:
    reverse(*name[50], low, high);
    to:

    Code:
    reverse(name, low, high);
    and
    Code:
            
    &str[i]=(inputString[i]);
    to
    Code:
    str[i]=(inputString[i]);
    and
    Code:
            temp=&str[lowerBound];
            &str[lowerBound]=&str[upperBound];
            &str[upperBound]=temp;
    to
    Code:
            temp=str[lowerBound];
            str[lowerBound]=str[upperBound];
            str[upperBound]=temp;
    ahoodin
    To keep the plot moving, that's why.

  10. #10
    Join Date
    Mar 2001
    Posts
    2,366

    Re: Dereferencing Problems

    fixed:
    Code:
    // Author:      Josh Johnson
    // Date:        30 March 2009
    
    // Program switches the letters of a character array between two input bounds
    // The second function will reverse the entire string
    
    #include <iostream>
    #include <string>
    
    using namespace std;     // tells compiler to use standard names for things
    
    void reverse(char *inputStream, int lowerBound, int upperBound);
    void reverseAll(char *inputStream);
    
    int main() {
    
        int high, low, sLength;
        string s;
    
        cout << "This program will switch the letters of an input between two ";
        cout << "inputted bounds.\nPlease input a string of characters or a word.";
        cout << "The string must be less than 50 characters: ";
    
        getline (cin, s, '\n');
    
        sLength=(s.length()+1);
    
        // Convert String to Character Array
        char *name = new char[s.length()+1]();
        strcpy(name, s.c_str());
    
        cout << "\nNow select what range of characters you would like to switch.\n";
        cout << "Lower Bound: ";
        cin >> low;
        cout << "Upper Bound: ";
        cin >> high;
    
        reverse(name, low, high);
    
    return 0;
    }
    
    void reverse(char *inputString, int lowerBound, int upperBound){
    
        char temp;
        int low=lowerBound, high=upperBound;
        char str[50];
    
        for (int i=0; i<51; i++) {
            str[i]=(inputString[i]);
        }
    
        if (lowerBound<upperBound) {
            temp=str[lowerBound];
            str[lowerBound]=str[upperBound];
            str[upperBound]=temp;
    
            lowerBound++;
            upperBound--;
    
            reverse(inputString, low, high);
        }
    
    }
    
    void reverseAll(char *inputStream) {
    
    // To finish later
    
    }
    ahoodin
    To keep the plot moving, that's why.

  11. #11
    Join Date
    Mar 2001
    Posts
    2,366

    Re: Dereferencing Problems

    You should check the logic though. I particularly fixed your syntax, not your logic.
    ahoodin
    To keep the plot moving, that's why.

  12. #12
    Join Date
    Mar 2009
    Posts
    6

    Re: Dereferencing Problems

    Thank you SO much. The logic I can take care of without a problem, it's the syntax that was killing me.

    It works great!

    I really, really appreciate it!

  13. #13
    Join Date
    Mar 2009
    Posts
    6

    Re: Dereferencing Problems

    Maybe I spoke too quickly...

    It compiles fine and all, but when I go to run it, it tells me that there is "Segmentation Fault" after I've inserted the string and integers.

    I've never encountered this before. I'll still do some testing to see if I can change anything. But if anyone has a quick fix, that'd be awesome.

    (I've edited the original code to the updated version)
    Last edited by JoshJ; March 30th, 2009 at 01:56 AM.

  14. #14
    Join Date
    Mar 2009
    Posts
    6

    Re: Dereferencing Problems

    Fixed!

    Not sure why, but it did not like these lines of code:
    Code:
        int low=lowerBound;
        int high=upperBound;
    So I just used lowerBound and upperBound for the reverse function call, rather than low and high.

  15. #15
    Join Date
    Feb 2009
    Posts
    326

    Re: Dereferencing Problems

    I am just a newbie to C++.

    correct me if I am wrong....

    why is the function reverse being called inside the function reverse, is this intentional ?

    Any reason why it is made recursive ?

    reverse contains the variables high and low which are initialized to the value of the arguments supplied to it.

    Assuming the value of low and high are 10 and 20 respectively to begin with:

    Iteration low high
    ------------ ------- ------
    1 10 20

    During the 1st iteration, the function reverse is again called, at this point the variables low and high are still alive. Now when the function is again invoked, since it is call by value, it causes it value of low to be copied to the parameter lowerBound and high to be copied to upperBound. Just after doing so the function declares the variables high and low again.

    I think this is was causes the segmentation fault.

    I just feel it would be worth questioning if the recursive call was deliberate and was a measured approach.

    It would be a little easier if the logic could be straightened and then looked at the rectifying the runtime/syntactical errors, instead of going the other way around.

    My feeling is that this program doesn't require a recursive call, correct me if I am wrong.
    Last edited by Muthuveerappan; March 30th, 2009 at 01:59 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center