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;
}

Recommended Answers

All 2 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.