Thread: Pointer and If Statement

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    9

    Pointer and If Statement

    Hello,

    I am trying to use an if statement which has a pointer in it to determine which function to use.

    The original code would be -

    Code:
    pqueue *pq = new_bounded_pqueue(5,le_artist);
    I want to use an if statement so if a user inputs a certain variable it will declare a different line of code. Here is what I have done so far -

    Code:
    printf("Please enter if you want to order by ascending or descending, A = Assending, D = Descending:   \n");    scanf("%c", &order);
        
        pqueue *pq;
    
    
        if (order == 'A' || order == 'a')    {
        pqueue *pq = new_bounded_pqueue(5,le_artist);
        }
        else if (order == 'D' || order == 'd')
        {
        pqueue *pq = new_bounded_pqueue(5,le_artistdec);
        }

    Once compiled the program runs but as soon as I input data I return a segmentation fault.
    Last edited by Nathan Kent; 12-22-2014 at 05:05 PM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,673
    It's a matter of variable scope. You're creating new variables don't exist outside of their individual blocks. Instead just assign to the pq variable.

    Code:
    pq = new_bounded_queue(4, le_artist);
    etc.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    9
    Quote Originally Posted by rags_to_riches View Post
    It's a matter of variable scope. You're creating new variables don't exist outside of their individual blocks. Instead just assign to the pq variable.

    Code:
    pq = new_bounded_queue(4, le_artist);
    etc.

    I was silly and realised that after, if you check my original post i've updated it and now I have a segmentation fault.

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,776
    Within the body of the if statement, you declared a variable named pq. You want to assign to the variable named pq that was declared before the if statement.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    2

    Duplicate declarations of variable

    I believe that you are making duplicate declarations of your variable pointer in the begining and then again when assigning it to the new_bounded_pqueue() function.

    I believe you should remove the declaration and you just use the name of the pointer variable and you'll be just fine.

  6. #6
    Registered User
    Join Date
    Dec 2014
    Posts
    9
    Quote Originally Posted by C-Lover View Post
    I believe that you are making duplicate declarations of your variable pointer in the begining and then again when assigning it to the new_bounded_pqueue() function.

    I believe you should remove the declaration and you just use the name of the pointer variable and you'll be just fine.
    I figured that would be the reason last night it's just the syntax I can't get correct, could you possibly push me in the right direction so I can learn from it?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,776
    Err... this is actually the same problem that rags_to_riches pointed out in post #2. Your edit did not fix the problem. Rather:
    Code:
    printf("Please enter if you want to order by ascending or descending, A = Assending, D = Descending:   \n");
    scanf("%c", &order);
    
    pqueue *pq;
    if (order == 'A' || order == 'a')
    {
        pq = new_bounded_pqueue(5, le_artist);
    }
    else if (order == 'D' || order == 'd')
    {
        pq = new_bounded_pqueue(5, le_artistdec);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2014
    Posts
    9
    Quote Originally Posted by laserlight View Post
    Err... this is actually the same problem that rags_to_riches pointed out in post #2. Your edit did not fix the problem. Rather:
    Code:
    printf("Please enter if you want to order by ascending or descending, A = Assending, D = Descending:   \n");
    scanf("%c", &order);
    
    pqueue *pq;
    if (order == 'A' || order == 'a')
    {
        pq = new_bounded_pqueue(5, le_artist);
    }
    else if (order == 'D' || order == 'd')
    {
        pq = new_bounded_pqueue(5, le_artistdec);
    }
    Thank you! I think the issue that was confusing me was the way I laid out my code. I'm used to seeing declaring pointers like this
    Code:
    pqueue * pq;
    With the space, it's still early days I guess

    Appreciate the help!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,776
    You're welcome

    You could read Stroustrup's answer to the FAQ: Is ``int* p;'' right or is ``int *p;'' right?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  4. Pointer syntax in if statement
    By fkheng in forum C Programming
    Replies: 5
    Last Post: 06-10-2003, 04:01 AM
  5. Replies: 2
    Last Post: 08-05-2002, 04:52 PM