passing pointer array to function

problems with functions accepting ptr array params

Page 1 of 1

5 Replies - 1299 Views - Last Post: 26 February 2010 - 04:55 AM Rate Topic: -----

#1 mizzi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-May 09

passing pointer array to function

Posted 24 February 2010 - 10:44 PM

Hi Folks,
I've been working on a program that uses several pointer arrays to collect data (int, char, float). I'm having problems finding a way to go about this in my text. Below is a snippet of code showing funct prototype, main and function body. Can someone please look this over and let me know what is wrong?

Thanks in advance, Mark


#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

//Function Prototypes

//Inputs Vin number
int * InputVin(int *[]);

//Inputs Make of car
char * InputMake(char *[]);

//Inputs Model of car
char * InputModel(char *[] );

//Inputs Year
int * InputYear(int *[]);

//Inputs amount of Fee
float * InputFee(float *[]);

int main () //begin main
{
	//Main variables declaration
	int rows = 5;
	int columns = 8;
	
//create pointer array of int (Vin)
	int * vin = new int [5];

//create pointer array of int (Year)
	int * year = new int [4];

//create pointer array of float (Fee)
	float * fee = new float [1];

//create 2 D pointer array of char (Make)
	char ** cars = new char * [rows];
		for (int i = 0;i < rows; i ++)
			cars [i] = new char [columns];

//create 2 D pointer array of char (Model)
	char ** model = new char * [rows];
		for (int i = 0;i < rows; i ++)
			model [i] = new char [columns];
//fill arrays
        for (int i = 0;i < rows;i ++){
		* vin[i] = InputVin(vin[i],size);
		* cars[i] =InputMake(make_main,size);
		* model[i] = InputModel(model_main,size);
		* year[i] = InputYear(year_main,size);
		* fee [i]= InputFee(fee_main,size);
        }
return 0;
}//end main

//Function Bodies

//Inputs Vin number
int * InputVin(int * vin[]){
	cout << " Enter car Vin#: ";
	cin >>  * vin;
	return  vin;
}

//Inputs Make of car
char * InputMake(char * make[],int size){
	cout << " Enter car Make: ";
	cin.get(); 
	cin.getline(cars[i],columns,'\n');  
	return make;
}

//Inputs Model of car
char * InputModel(char * model[], int size){
	cout << " Enter car Model: ";  
	cin.getline(model,'\n');  
	return model;
} 

//Inputs Year
int * InputYear(int * year[], int size){
	cout << " Enter car Year: ";
	cin >>  * year;
	return year;
}

//Inputs amount of Fee
float * InputFee(float * fee[], int size){
	cout << " Enter car Fee: ";
	cin >>  * fee;
	return fee;

}




Is This A Good Question/Topic? 0
  • +

Replies To: passing pointer array to function

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: passing pointer array to function

Posted 24 February 2010 - 10:56 PM

You can access dynamically allocated arrays the same way you would a stack based array. Allocate the arrays and then fill them like this:
for (int i = 0;i < rows;i ++){
    vin[i] = InputVin();
    cars[i] =InputMake();
    model[i] = InputModel();
    year[i] = InputYear();
    fee [i]= InputFee();
}


Create your functions like this:
int InputVin(){
    int vin;
    cout << " Enter car Vin#: ";
    cin >> vin;
    return  vin;
}

This post has been edited by eker676: 24 February 2010 - 10:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: passing pointer array to function

Posted 24 February 2010 - 10:58 PM

make sure to delete the allocated memory also when your finished with it :)
Was This Post Helpful? 0
  • +
  • -

#4 mizzi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-May 09

Re: passing pointer array to function

Posted 25 February 2010 - 11:51 AM

Hi,
I tried your suggestion and it is not working.
I'm getting the following compilation errors:

C2440: '=' : cannot convert from 'int *' to 'int' -
vin[i] = InputVin(); 
//in main
C2440: 'return' : cannot convert from 'int' to 'int *'
return   vin; 
//in function

Can we try this again?
Thanks, M
Was This Post Helpful? 0
  • +
  • -

#5 gsekhon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 18-February 10

Re: passing pointer array to function

Posted 25 February 2010 - 10:29 PM

if you have an array of pointers to integers as:

int* blah[50];



then blah is a pointer to a pointer, which means you can do something like:

int** blah2 = blah;



So instead of doing int *[], just write int**.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: passing pointer array to function

Posted 26 February 2010 - 04:55 AM

Right, time for some "terms" ground rules. These things are called:
// arrays
int a[5]; // array of type int, allocated statically
int *a = new int[5]; // array of type int, allocated dynamically

// these could be considered "arrays of pointers"
int *p[5]; // array of type int*, allocated statically
int *p = new int[5]; // array of type int*, allocated dynamically



The term "pointer array" is confusing in your post, because it seems you're symply looking for a dynamic array. I'm also not clear on why you're using char arrays and not strings; you're adding confusion.

Then again, you dim you year to 5, you fee to 1, an then loop 5 times? There is no fee[3]. What are you thinking?

I'd offer you some code, but what you have is inconsistent enought that I can't tell what you're thinking of.

Good luck.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1