6 Replies - 719 Views - Last Post: 21 July 2009 - 11:12 PM Rate Topic: -----

#1 brettfavre9190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-July 09

passing array through pointers to functions?

Posted 21 July 2009 - 10:07 PM

How can I pass my array into other functions? I need to pass char info*[100] into all three functions. I keep getting errors when I try. Something like "cannot convert something to something". Here is my code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <iomanip>
#include <string.h>
#include <ctype.h>
using namespace std;
int inserting(){
}
int sorting(){
}
int startover(){
}
int main(){
char *info[100];
int choose;
int (*ptr[])()={inserting,sorting,startover}… an array of pointers to functions
do{
do{
cout<<"\nPlease select a menu option:\n";
cout<<"\n0)Insert SSN, First Name, Last Name";
cout<<"\n1)Sort and Display Names";
cout<<"\n2)Start Over";
cout<<("\n3)Quit\n");
cin>>choose;
if (choose<0||choose>3)
{
cout<<"\nInvalid selection try again!\n";//Error message for invalid input
}

}while(choose<0||choose>3);//Validatio… loop 
if (choose==0)//If statement calling functions using array of pointers
(ptr[0])();

else
if (choose==1)
(ptr[1])();
else
if (choose==2)
(ptr[2])();
else;

}while(choose!=3);

system("pause");
}//main func


This post has been edited by brettfavre9190: 21 July 2009 - 10:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: passing array through pointers to functions?

#2 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: passing array through pointers to functions?

Posted 21 July 2009 - 10:18 PM

Please edit your post and put your code between
Your code here
blocks.

It would also be helpful if you provided your functions
int (*ptr[])()={inserting,sorting,startover}… an array of pointers to functions
Was This Post Helpful? 0
  • +
  • -

#3 brettfavre9190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-July 09

Re: passing array through pointers to functions?

Posted 21 July 2009 - 10:28 PM

View Postskaoth, on 21 Jul, 2009 - 09:18 PM, said:

Please edit your post and put your code between
Your code here
blocks.

It would also be helpful if you provided your functions
int (*ptr[])()={inserting,sorting,startover}… an array of pointers to functions


At the moment I dont have anything in my functions but I need *info[100] in all 3 functions, when I do the following I get error messages.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <iomanip>
#include <string.h>
#include <ctype.h>
using namespace std;
int inserting(char *info[100]){
}
int sorting(char *info[100]){
}
int startover(char *info[100]){
}
int main(){
char *info[100];
int choose;
int (*ptr[])(char)={inserting,sorting,startover}… an array of pointers to functions
do{
do{
cout<<"\nPlease select a menu option:\n";
cout<<"\n0)Insert SSN, First Name, Last Name";
cout<<"\n1)Sort and Display Names";
cout<<"\n2)Start Over";
cout<<("\n3)Quit\n");
cin>>choose;
if (choose<0||choose>3)
{
cout<<"\nInvalid selection try again!\n";//Error message for invalid input
}

}while(choose<0||choose>3);//Validatio… loop 
if (choose==0)//If statement calling functions using array of pointers
(ptr[0])(info);

else
if (choose==1)
(ptr[1])(info);
else
if (choose==2)
(ptr[2])(info);
else;

}while(choose!=3);

system("pause");
}//main func


Was This Post Helpful? 0
  • +
  • -

#4 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: passing array through pointers to functions?

Posted 21 July 2009 - 10:45 PM

Are you looking for something like this?

int inserting(char **info)
{
	cout << "calling inserting\n";
}

int sorting(char **info)
{
	cout << "calling sorting\n";
}

int startover(char **info)
{
	cout << "calling startover\n";
}

int _tmain(int argc, _TCHAR* argv[])
{
	char *info[100];
	int choose;
	int (*insertFunc)(char **) = inserting;
	int (*sortFunc)(char **) = sorting;
	int (*startOverFunc)(char**) = startover(;)

		do{
			do{
				cout<<"\nPlease select a menu option:\n";
				cout<<"\n0)Insert SSN, First Name, Last Name";
				cout<<"\n1)Sort and Display Names";
				cout<<"\n2)Start Over";
				cout<<("\n3)Quit\n");
				cin>>choose;
				if (choose<0||choose>3)
				{
					cout<<"\nInvalid selection try again!\n";//Error message for invalid input
				}

			}while(choose<0||choose>3);//Validatio… loop
			if (choose==0)//If statement calling functions using array of pointers
				insertFunc(info);

			else
				if (choose==1)
					sortFunc(info);
				else
					if (choose==2)
						startOverFunc(info);
					else;

		}while(choose!=3);

	system("pause");


}


Was This Post Helpful? 0
  • +
  • -

#5 brettfavre9190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-July 09

Re: passing array through pointers to functions?

Posted 21 July 2009 - 10:48 PM

View Postskaoth, on 21 Jul, 2009 - 09:45 PM, said:

Are you looking for something like this?

int inserting(char **info)
{
	cout << "calling inserting\n";
}

int sorting(char **info)
{
	cout << "calling sorting\n";
}

int startover(char **info)
{
	cout << "calling startover\n";
}

int _tmain(int argc, _TCHAR* argv[])
{
	char *info[100];
	int choose;
	int (*insertFunc)(char **) = inserting;
	int (*sortFunc)(char **) = sorting;
	int (*startOverFunc)(char**) = startover(;)

		do{
			do{
				cout<<"\nPlease select a menu option:\n";
				cout<<"\n0)Insert SSN, First Name, Last Name";
				cout<<"\n1)Sort and Display Names";
				cout<<"\n2)Start Over";
				cout<<("\n3)Quit\n");
				cin>>choose;
				if (choose<0||choose>3)
				{
					cout<<"\nInvalid selection try again!\n";//Error message for invalid input
				}

			}while(choose<0||choose>3);//Validatio… loop
			if (choose==0)//If statement calling functions using array of pointers
				insertFunc(info);

			else
				if (choose==1)
					sortFunc(info);
				else
					if (choose==2)
						startOverFunc(info);
					else;

		}while(choose!=3);

	system("pause");


}


That works but I have to use an array of pointers to functions...
Was This Post Helpful? 0
  • +
  • -

#6 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: passing array through pointers to functions?

Posted 21 July 2009 - 10:57 PM

Here is one way to define an array of these

int inserting(char **info)
{
	cout << "calling inserting\n";
	return 0;
}

int sorting(char **info)
{
	cout << "calling sorting\n";
	return 0;
}

int startover(char **info)
{
	cout << "calling startover\n";

	return 0;
}

typedef int (*FUNC)(char **);
int _tmain(int argc, _TCHAR* argv[])
{
	char *info[100] = {"Hello" };

	
	FUNC funcAry[3] = {inserting, sorting, startover};
	funcAry[0](info);
	funcAry[1](info);
	funcAry[2](info);

	system("pause");


}


Was This Post Helpful? 1
  • +
  • -

#7 brettfavre9190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-July 09

Re: passing array through pointers to functions?

Posted 21 July 2009 - 11:12 PM

View Postskaoth, on 21 Jul, 2009 - 09:57 PM, said:

Here is one way to define an array of these

int inserting(char **info)
{
	cout << "calling inserting\n";
	return 0;
}

int sorting(char **info)
{
	cout << "calling sorting\n";
	return 0;
}

int startover(char **info)
{
	cout << "calling startover\n";

	return 0;
}

typedef int (*FUNC)(char **);
int _tmain(int argc, _TCHAR* argv[])
{
	char *info[100] = {"Hello" };

	
	FUNC funcAry[3] = {inserting, sorting, startover};
	funcAry[0](info);
	funcAry[1](info);
	funcAry[2](info);

	system("pause");


}


Thanks for your help, I really appreciate it!!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1