Good Afternoon,

I'm a little difficulty with this little program. The error is argument of type int (*) is not incompatible with parameter of type double. I'm getting this error on

``````swapFour(&a, &b);
``````

Here is the whole program code

``````double a =102.39, b=201.59;
double c=30.99, d=48.99;
double e=55.21, f=95.35;
//call swapFour with pointer type paramters
cout<<"before calling swapFour(...), a is " << a << " and a is " <<a <<endl;
swapFour(&a, &b);                   //watch, we pass &x and &y to this function!!!!!!
cout<<"after calling swapOne(...), x is " << a << " and y is " <<b <<endl;

//call swapFive with reference type paramters
cout<<"before calling swapFive(...), c is " << c << " and d is " <<d <<endl;
swapFive(c, d);                 //watch, we pass c and d to this function!!!!!!
cout<<"after calling swapFive(...), c is " << c << " and d is " <<d <<endl;

//call swapSix with value type paramters
cout<<"before calling swapSix(...), e is " << e << " and f is " <<f <<endl;
swapSix(e, f);
cout<<"after calling swapSix(...), e is " << e << " and f is " <<f <<endl;
``````

Functions

``````//swapFour takes two pointer type parameters
void swapFour(double * a, double * b)
{
//watch carefully how we do the swaping. Compare this with swapTwo
double tp;
tp = *a;
*a = *b;
*b = tp;
}

//swapFive takes two reference type parameters
void swapFive(double & a, double & b)
{
//watch carefully how we do the swaping. Compare this with swapOne
double tp;
tp = a;
a = b;
b = tp;
}
//swapSix takes two pointer type parameters
void swapSix(double a, double b)
{
//watch carefully how we do the swaping
double tp;
tp = a;
a = b;
b = tp;
}
``````

I would really appreciated if anyone could help me fix this error.

Thank you

## All 7 Replies

Hi poloblue,

Your function calls look fine to me, in as much as I don't see an obvious problem with the parameters you're passing, unless there's some way in which the code you posted doesn't represent what you're trying to compile (with the code wrapped in a main() function and suitable prototypes provided). Is there more than you've posted here?

Bob,

yes, if you want I can post the whole program, so you can see it

Bob, here is the whole program

CPP file

``````#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <vector>                    //include vector class template

//define a const array size
const int SIZE = 35;                //class size
const int COLUMN = 11;              //column size

using namespace std;

int main( )
{

/************************************************************************
Part A: pointer declarations, operators:
* : dereference operator
. : member selection operator
->: dereference member selection operator
************************************************************************/
//simple pointers
int cat, dog;                       //two int vars
int *intPtr;                        //an int pointer
cat = 10;
dog = 20;
intPtr = &cat;                      //intPtr points to cat, i.e., intPtr has the address of cat
cout << "cat is      " <<cat <<",  "
<< "dog is    " <<dog <<",  "
<< "intPtr is " <<intPtr<<",  "
<<"*intPtr is " <<*intPtr<<endl;

dog = *intPtr;                      //get what is pointed by intPtr to dog
*intPtr = 55;                       //change what is pointed by intPtr to 55

intPtr = &cat;                      //intPtr points to cat, i.e., intPtr has the address of cat
cout << "cat is      " <<cat <<",  "
<< "dog is    " <<dog <<",  "
<< "intPtr is " <<intPtr<<",  "
<<"*intPtr is " <<*intPtr<<endl;

//structure/class pointers
studentType jane, joe;              //two students
studentType *ptr;                   //student pointer

//using member selection operator . to access members of a structure/class
jane.firstName="Alice";             //initilization
jane.lastName="Smith";

//using member selection operator . to access members of a structure/class
joe.firstName="Alex";               //initilization
joe.lastName="Black";

ptr = &jane;                        //ptr points to jane

//use dereference member selection operator . to access members of a structure/class
cout << ptr->firstName <<"  "
<< ptr->lastName  <<" got "

ptr = &joe;                         //ptr points to joe

//use dereference operator *, member selection operator ., and dereference member selection operator
//to access members of a structure/class
cout << (*ptr).firstName <<"  "
<< (*ptr).lastName  <<" got "

/************************************************************************
Part B: Pointer arithemtic, arrays vs. pointers
************************************************************************/
//var declaration
int a={10, 20, 30, 40, 50};      //an int array of size 5
studentType fun;                 //a studentType array of size 3
int *myPtr;                         //an int pointer
studentType *yourPtr;               //a studentType point
int i;                              //loop var

//array name is a pointer pointing to the first element of the array
//use array name pointer to access array elements
cout<<"use array name ............ "<<endl;
for (i=0; i<5; i++)
{
cout<< *(a+i) <<" ";            //a+i is &a[i], *(a+i) is a[i]
}
cout<<endl;

myPtr = a;
cout<<"*myPtr = *a = a = " << *myPtr<<endl;
myPtr = &a;
cout<<"*myPtr = *(a+1) = a = " << *myPtr<<endl;
cout<<"What is *&a? "         <<*&a<<endl;

yourPtr = fun;                      //youPtr points to the first element of fun array
for (i=0; i<3; i++)
{
cout<<"Enter a first name => ";
cin>> (yourPtr+i)->firstName;
cout<<"Enter a last name => ";    //changed by sirisha
cin>> (*(yourPtr+i)).lastName;
}
//cout students
yourPtr = fun;
for (i=0; i<3; i++)
{
cout << (yourPtr+i)->firstName <<"  "
<< (*(yourPtr+i)).lastName <<" got "
}

/************************************************************************
Part C: Pointer parameter vs reference parameters vs value parameter
************************************************************************/
// var declaration
int x =10, y=20;
int u=30, v=40;
int w=50, z=60;
//call swapOne with pointer type paramters
cout<<"before calling swapOne(...), x is " << x << " and y is " <<y <<endl;
swapOne(&x, &y);                    //watch, we pass &x and &y to this function!!!!!!
cout<<"after calling swapOne(...), x is " << x << " and y is " <<y <<endl;

//call swapTwo with reference type paramters
cout<<"before calling swapTwo(...), u is " << u << " and v is " <<v <<endl;
swapTwo(u, v);                  //watch, we pass u and v to this function!!!!!!
cout<<"after calling swapTwo(...), u is " << u << " and v is " <<v <<endl;

//call swapThree with value type paramters
cout<<"before calling swapThree(...), w is " << w << " and z is " <<z <<endl;
swapThree(w, z);
cout<<"after calling swapThree(...), w is " << w << " and z is " <<z <<endl;

/*******************************************************************************
Part D: This part is for you to complete.
a. Define a new struct type
bookType with members of title, author, isbn, and price.

You shall add the definition of the bookType into the header fil

b. Repeat Part A with variables of double type and bookTypes.
b. Repeat Part B with char type arrays and variables
c. Repeat Part C with variables of bookType
*******************************************************************************/

/************************************************************************
Repeat Part A: pointer declarations, operators:
* : dereference operator
. : member selection operator
->: dereference member selection operator
************************************************************************/
//simple pointers
double tom, jerry;                      //two double vars
double *doublePtr;                      //an double pointer
tom = 10;
dog = 20;
doublePtr = &tom;                       //doublePtr points to tom, i.e., doublePtr has the address of tom
cout << "tom is      " <<tom <<",  "
<< "jerry is    " <<jerry <<",  "
<< "doublePtr is " <<doublePtr<<",  "
<<"*doublePtr is " <<*doublePtr<<endl;

jerry = *doublePtr;                     //get what is pointed by doublePtr to jerry
*doublePtr = 55;                        //change what is pointed by doublePtr to 55

doublePtr = &tom;                       //doublePtr points to tom, i.e., doublePtr has the address of tom
cout << "tom is      " <<tom <<",  "
<< "jerry is    " <<jerry <<",  "
<< "doublePtr is " <<doublePtr<<",  "
<<"*doublePtr is " <<*doublePtr<<endl;

//structure/class pointers
bookType twilight, computers;               //two books
bookType *bptr;                 //book pointer

//using member selection operator . to access members of a structure/class
twilight.title="Computer Illuminated";              //initilization
twilight.author="Smith";
twilight.isbn= 1589635;
twilight.price= 100.95;

//using member selection operator . to access members of a structure/class
computers.title="AI";               //initilization
computers.author="Johnson";
computers.isbn= 9876542;
computers.price= 98.63;

bptr = &twilight;                       //ptr points to twilight

//use dereference member selection operator . to access members of a structure/class
cout << bptr->title <<"  "
<< bptr->author  <<"  "
<< bptr->isbn  <<" is "
<< bptr->price << endl;

bptr = &computers;                          //ptr points to computers

//use dereference operator *, member selection operator ., and dereference member selection operator
//to access members of a structure/class
cout << (*bptr).title <<"  "
<< (*bptr).isbn  <<" is "
<< bptr->price << endl;

/************************************************************************
Repeat Part B: Pointer arithemtic, arrays vs. pointers
************************************************************************/
//var declaration
char r[]={'A', 'B', 'C', 'D', 'E'};     //an char array of size 5
bookType funct;                  //a studentType array of size 3
char *bmyPtr;                           //an char pointer
bookType *yrPtr;                //a bookType point
int i;                              //loop var

//array name is a pointer pointing to the first element of the array
//use array name pointer to access array elements
cout<<"use array name ............ "<<endl;
for (i=0; bmyPtr[i]; i++)
{
cout<< *(r+i) <<" ";            //r+i is &r[i], *(r+i) is r[i]
}
cout<<endl;

bmyPtr = r;
cout<<"*bmyPtr = *r = r = " << *bmyPtr<<endl;
bmyPtr = &r;
cout<<"*bmyPtr = *(r+1) = r = " << *bmyPtr<<endl;
cout<<"What is *&r? "         <<*&r<<endl;

yrPtr = funct;                      //yrPtr points to the first element of funct array
for (i=0; i<3; i++)
{
cout<<"Enter the title of a book => ";
cin>> (yrPtr+i)->title;
cout<<"Enter the author of the book => "; //changed by sirisha
cin>> (*(yrPtr+i)).author;
cout<<"Enter the isbn of the book => ";
cin>> yrPtr[i].isbn;

}
//cout books
yrPtr = funct;
for (i=0; i<3; i++)
{
cout << (yrPtr+i)->title <<"  "
<< (*(yrPtr+i)).author <<" got "
<<  yrPtr[i].isbn <<endl;
}

/************************************************************************
Repeat Part C: Pointer parameter vs reference parameters vs value parameter
************************************************************************/
// var declaration
double a =102.39, b=201.59;
double c=30.99, d=48.99;
double e=55.21, f=95.35;
//call swapFour with pointer type paramters
cout<<"before calling swapFour(...), a is " << a << " and a is " <<a <<endl;
swapFour(&a, &b);                   //watch, we pass &x and &y to this function!!!!!!
cout<<"after calling swapOne(...), x is " << a << " and y is " <<b <<endl;

//call swapFive with reference type paramters
cout<<"before calling swapFive(...), c is " << c << " and d is " <<d <<endl;
swapFive(c, d);                 //watch, we pass c and d to this function!!!!!!
cout<<"after calling swapFive(...), c is " << c << " and d is " <<d <<endl;

//call swapSix with value type paramters
cout<<"before calling swapSix(...), e is " << e << " and f is " <<f <<endl;
swapSix(e, f);
cout<<"after calling swapSix(...), e is " << e << " and f is " <<f <<endl;
//well done and exit
return 0;
}
``````

``````#include <iostream>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

//define studentType structure
struct studentType
{
string firstName,                   //firstnames for students
lastName;                    //last names for students
};

//define bookType structure
struct bookType
{
string title,                       //titles for books
author;                      //authors for books
int isbn;                           //isbns for books
double price;                       //prices for books
};

//swapOne takes two pointer type parameters
void swapOne(int * x, int * y)
{
//watch carefully how we do the swaping. Compare this with swapTwo
int temp;
temp = *x;
*x = *y;
*y = temp;
}

//swapTwo takes two reference type parameters
void swapTwo(int & x, int & y)
{
//watch carefully how we do the swaping. Compare this with swapOne
int temp;
temp = x;
x = y;
y = temp;
}
//swapThree takes two pointer type parameters
void swapThree(int x, int y)
{
//watch carefully how we do the swaping
int temp;
temp = x;
x = y;
y = temp;
}

//swapFour takes two pointer type parameters
void swapFour(double * a, double * b)
{
//watch carefully how we do the swaping. Compare this with swapTwo
double tp;
tp = *a;
*a = *b;
*b = tp;
}

//swapFive takes two reference type parameters
void swapFive(double & a, double & b)
{
//watch carefully how we do the swaping. Compare this with swapOne
double tp;
tp = a;
a = b;
b = tp;
}
//swapSix takes two pointer type parameters
void swapSix(double a, double b)
{
//watch carefully how we do the swaping
double tp;
tp = a;
a = b;
b = tp;
}

#endif
``````

I'm getting this error while I compile the program

char' differs in levels of indirection from 'int 

Here:

``````83. int a={10, 20, 30, 40, 50}; //an int array of size 5

268. double a =102.39, b=201.59;
``````

Looks like you've used the same name twice for two different variables, both named 'a'.

Hope that helps.

Bob,

Thank you very much for your help, the program runs

Now you know why you should use variable names that make sense. Just choosing random letters obviously have drawbacks.

Be a part of the DaniWeb community

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