0

In the following program I'm getting the warning -> unused variable ‘fn’. I'm following along with a book so I don't know why it gave me that portion of code if it's unusable. Also, I don't understand this line at all.
-> void(*fn)(int& a, int* b) = add;

#include <iostream>

void add(int& a, int* b) { std::cout << "Total: " << (a + *b) << std::endl; };

int main()
{
    int num = 100, sum = 500;
    int& rNum = num;
    int* ptr = &num;

    void(*fn)(int& a, int* b) = add;

    std::cout << "Reference: " << rNum << std::endl;
    std::cout << "Pointer: " << *ptr << std::endl;

    ptr = &sum;

    std::cout << "Pointer now: " << *ptr << std::endl;
    add(rNum, ptr);

    return 0;
}
3
Contributors
2
Replies
24
Views
2 Years
Discussion Span
Last Post by sepp2k
1

fn is a pointe to a function. It is set on line 11 but never actually used for anything. That is what your compiler is warning you about -- you could just delete line 11 with no loss.

0

I don't know why it gave me that portion of code if it's unusable

fn is unused, not unusable. You could replace line 19 with fn(rNum, ptr) and thus actually use fn.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.