1

I am currently using Visual Studios for class and needless to say I am having difficulty understanding pointers. I have created a program but does not seem to work right for me. I keep getting an assertion error after I click the function type that I want. I get a Debug assertion failed pop up error. Expression: result_pointer != nullptr. It says line 1558

int function1(int a,int b);
int function2(int a, int b);
int function3(int a, int b);

int(*p[3])(int x, int y);

int main()
{

    int num1, num2;
    int choice = 0;

    p[0] = function1;
    p[1] = function2;
    p[2] = function3;

    printf("Please enter two numbers: ");
    scanf_s("%d", &num1);
    scanf_s("%d", &num2);
    printf("Which would you like to try (1 for Math, 2 for Subtraction, 3 for Multiplication): \n");
    scanf_s("%d", choice);
    int (*i) = &choice;

    while (*i >= 0 && *i < 3) {
        (p[*i])(num1,num2);
    }

puts("Program execution compiled");
}
int function1(int a, int b) 
{
    int total;
    total = a + b;
    return total;

}
int function2(int a, int b) 
{
    int total;
    total = a - b;
    return total;
}
int function3(int a, int b) 
{
    int total;
    total = a * b;
    return total;
}
7
  • 2
    To make it easier for answerers, or others with similar problems, please edit to add a specific problem statement — "it doesn't work" can be assumed, but how does it not work? What error message or incorrect behavior is characteristic? Have you tried stepping through in VS's debugger? What happens to the various variables? Commented Oct 10, 2015 at 1:52
  • 1
    Is the compiler giving you any warnings when you compile? Commented Oct 10, 2015 at 2:00
  • no when I build it goes through with no errors here is what comes up1>------ Rebuild All started: Project: pointer-function, Configuration: Debug Win32 ------ 1> stdafx.cpp 1> pointer-function.cpp 1>c:\users\cesteves\documents\c programming\pointer-function\pointer-function\pointer-function.cpp(26): warning C4477: 'scanf_s' : format string '%d' requires an argument of type 'int *', but variadic argument 1 has type 'int' 1> pointer-function.vcxproj -> C:\Users\cesteves\Documents\C Programming\pointer-function\Debug\pointer-function.exe = Rebuild All: 1 succeeded, 0 failed, 0 skipped = Commented Oct 10, 2015 at 2:04
  • 1
    Yes, that warning is telling you that on line 26, which is scanf_s("%d",choice), you need to pass a pointer, but instead you passed an int. In other words line 26 should be scanf_s("%d", &choice) Commented Oct 10, 2015 at 2:08
  • 1
    The code doesn't try to printf the outcome. Also, the while loop will never end. The while should be if. Commented Oct 10, 2015 at 2:38

1 Answer 1

2

This is why your code fails:

scanf_s("%d", choice);

You forgot to use the address of operator &.

scanf_s("%d", &choice);

Of course there are a number of other quirks with your code.

For example,

  1. Why do you use int (*i) = &choice;? Wouldn't it make more sense just to take the choice and subtract 1? No need to use pointers int i = choice - 1.
  2. Your loop is going to execute the body of the loop until i is less than zero or greater than 2, which is never if the input entered is correct. Consider choosing another loop condition, or swapping the loop with an if statement.
  3. You don't ever display the return value of the function. Try something like this printf("%d\n", (p[i])(num1,num2) (assuming you've fixed the i is a pointer thing)
Sign up to request clarification or add additional context in comments.

3 Comments

I figured it out. The reason I used pointers is because the chapter was for the purpose of including pointers and I felt like I was not using them enough.
@Chris You used the address of operator & quite a few times, so you used a lot of pointers. You could probably skip the one I mentioned though.
thank you all for all of the great advice and suggestions!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.