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
array of function pointer [message #36149] Sat, 05 May 2012 13:08 Go to next message
varu is currently offline  varu
Messages: 21
Registered: March 2012
Location: Bangalore,India
Promising Member
hi

i am doing something similar as show below in c . can u please help me to do the same in u++
#include <stdio.h>

int sum(int a, int b);

int subtract(int a, int b);

  

int main(void)

{
  p[0] = sum; // address of sum() 

  p[1] = subtract; // address of subtract() 

  int result;

  int i = 2, j = 2, op;

  

  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;

}

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] = &sum; // 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] = &sum; // 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;
}

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


Current Time: Sat Apr 27 23:17:15 CEST 2024

Total time taken to generate the page: 0.02467 seconds