Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » array of function pointer
Re: array of function pointer [message #36164 is a reply to message #36149] Tue, 08 May 2012 11:05 Go to previous message
navi is currently offline  navi
Messages: 107
Registered: February 2012
Location: Sydney, Australia
Experienced Member
U++ is C/C++; U++ is not a new language its a new cross platform GUI Framework using the C++. See the U++ Overview page to learn what is U++

In your code you did not declare any 'p'. also 'p's should be assigned the '&' of the functions. attached binary is complied using MinGW compiler with U++

	int (*p[2])(int,int);

	p[0] = ∑ // address of sum() 
	p[1] = &subtract; // address of subtract()



#include "stdio.h"

int sum(int a, int b);
int subtract(int a, int b);  

int main(int argc, const char *argv[]){
	int (*p[2])(int,int);
	int result;
	int i = 2, j = 3, op;

	p[0] = ∑ // address of sum() 
	p[1] = &subtract; // address of subtract()

	printf("0: Add, 1: Subtract\n");	
	do {
		printf("Enter number of operation: ");		
		scanf("%d", &op);
	} while(op<0 || op>2);	
	result = (*p[op]) (i, j);	
	printf("%d", result);
	return 0;
}

int sum(int a, int b){
  return a + b;
}

int subtract(int a, int b){
  return a - b;
}

 
Read Message
Read Message
Previous Topic: Form with OpenGL Display object
Next Topic: fills the DropList from VectorMap
Goto Forum:
  


Current Time: Sun Aug 24 11:40:29 CEST 2025

Total time taken to generate the page: 0.04418 seconds