why cannot pass parameter like this? - c++

I have a function:
int getCaseNum(float isovalue,float *F,int **point,int *dims,float *F_value)
{
int digtial_point[8];
int case_num=0;
int i = 0;
for(i=0;i<8;i++)
{
F_value[i] = F[GetPointIndex(point[i],dims)];
if(F_value[i]>isovalue)
{
digtial_point[i] = 1;
case_num = case_num + powf(2,i);
}
else
{
digtial_point[i] = 0;
}
}
return case_num;
}
Then I want to call this function in another function like this:
int pointID[8][3];
int case_num = getCaseNum(isovalue,F,pointID,dims,F_value);
However, when I compile my code, it says:
/Users/liyuanliu/Documents/lecture/sicvis/final_6b/mc510/proj6B.cxx:862:24: error:
no matching function for call to 'getCaseNum'
int case_num = getCaseNum(isovalue,F,pointID,dims,F_value);
^~~~~~~~~~
/Users/liyuanliu/Documents/lecture/sicvis/final_6b/mc510/proj6B.cxx:816:5: note:
candidate function not viable: no known conversion from 'int [8][3]' to
'int **' for 3rd argument
int getCaseNum(float isovalue,float *F,int **point,int *dims,float *F_value)
^
Why this happens ? Cannot I pass parameter like this?

// Create a dynamic 2D int array
int **pointID=new int*[8];
for(int i=0;i<8;i++) {
pointID[i]=new int[3];
}
int case_num=getCaseNum(isovalue,F,pointID,dims,F_value);

you need to get a pointer to first entry in 2D array.
Since array is contiguous, you can use below logic to iterate
int getCaseNum(float isovalue,float *F,int* point,int *dims,float *F_value)
{
for (i = 0; i < 24; i++)
{
cout << *(point+i*sizeof(int));
}
}
caller:
int case_num = getCaseNum(isovalue,F,&pointID[0][0],&dims,F);

Related

error - "no matching function for call to 'show2D'" while trying to call the function; why this error?

In the code that I have attached here, I have written the function called "show2D" and then I want to run this function inline 21 but it is showing an error there saying "no matching function for call to 'show2D'"
#include <iostream>
using namespace std;
void show2D(int variable[][20]){
int answer[4000];
int n=0;
for (int i=0; i<20;i++){
for(int j=0;j<20;j++){
if (variable[i][j]%2==1){
answer[n]=variable[i][j];
n++;
}
}
}
}
int main() {
int trailarr[2][2];
trailarr[0][0] = 0;
trailarr[0][1] = 1;
trailarr[1][0] = 2;
trailarr[1][1] = 3;
show2D(trailarr);
return 0;
}
Because trailarr is a [2][2] array and show2D expects an array of size variable[][20]. So the linker does not find any function called show2D that can accept a [2][2] array.

Confusion about pointer to an array as a function parameter

In my textbook about c++ I have the following code example:
using std::cout;
using std::endl;
int main() {
int aArr[4] = { 3,4,2,3 };
int bArr[3] = { 2,3,1 };
cout << "Append: " << endl;
printArray(aArr, 4); cout << " + "; printArray(bArr, 3);
int* cArr = append(&aArr, bArr);
cout << " = "; printArray(cArr, 7); cout << endl;
return 0;
}
Does the "&" symbol in front of "aArr" in the call to append in main mean that the address of aArr is passed, or that a reference to aArr is passed.
The question then asks for me to implement a function append which takes two arrays: the first array (in the first argument) of size 4 by array pointer and the second array (in the second argument) of size 3 by reference and returns a pointer to an array of size 7. I have declared that function as (in the appropriate header file)
int* append( int foo[4], int (&secondArray) [3] );
Has the author perhaps misplaced the order of the "&" symbol in the append method (that it should be in front of "bArr")?
The compiler can help you out in cases like this.
Lets assume that this is the function prototype for your append function:
int* append( int foo[4], int (&secondArray) [3]);
I can test this out with this simple bit of code:
int* append( int foo[4], int (&secondArray) [3])
{
return 0;
}
int main() {
int aArr[4] = { 3,4,2,3 };
int bArr[3] = { 2,3,1 };
int* cArr = append(&aArr, bArr);
return 0;
}
But the compiler doesn't like this, failing with this error:
test.cpp(9): error C2664: 'int *append(int [],int (&)[3])':
cannot convert argument 1 from 'int (*)[4]' to 'int []'
As you can see it doesn't like the &aArr argument 1 at line 9 as it does not match the argument 1 defined by the function at line 1. From the error message it is even nice enough to give a reason why it thinks they don't line up.
Now using the hint from the compiler it is clear the function should in fact look like this:
int *append(int (*foo)[4], int secondArray[3])
{
return 0;
}
int main() {
int aArr[4] = { 3,4,2,3 };
int bArr[3] = { 2,3,1 };
int* cArr = append(&aArr, bArr);
return 0;
}
With that change the compiler is happy to accept the code as correct.
Now comparing the two you can see the difference is in the first case the first argument was passed as an array of 4 integers, whereas in the second case it is passed as the address of an array of four integers.
Just from the english you can tell these are two very different things.
EDIT: Here is an extension of that example that shows how to access the data inside the function.
#include <stdio.h>
int *append(int (*foo)[4], int secondArray[3] )
{
int *foo1 = *foo;
for (int i = 0; i < 4; ++i)
{
printf("foo: %d\n", foo1[i]);
}
for (int j = 0; j < 3; ++j)
{
printf("secondArray: %d\n", secondArray[j]);
}
return 0;
}
int main() {
int aArr[4] = { 3,4,2,3 };
int bArr[3] = { 12,13,11 };
int* cArr = append(&aArr, bArr);
return 0;
}
Compiling an running this code produces this output:
foo: 3
foo: 4
foo: 2
foo: 3
secondArray: 12
secondArray: 13
secondArray: 11

passing an array of structs to a function and changing it through the fucntion

so this is my code but it wont compile for some reason.
Error 3 error C2036: 'pjs *' : unknown size
Error 4 error C2100: illegal indirection
Error 5 error C2037: left of 'size' specifies undefined struct/union 'pjs'
void initArray(struct pjs* array)
{
(*array[1]).size = 1;
}
struct pjs{
char* name;
int size;
int price;
};
int main(int argc , char** argv)
{
struct pjs array[10];
initArray(array);
system("PAUSE");
return (0);
}
Following may help:
struct pjs{
char* name;
int size;
int price;
};
// safer to use one of the following declaration
// void initArray(pjs *array, std::size_t size) // simpler
// void initArray(pjs (&array)[10]) // more restrictive but unintuitive syntax
void initArray(pjs* array)
{
array[1].size = 1;
}
int main()
{
pjs array[10];
initArray(array);
}
Definition of pjs should be given before to use it (or requiring its size).
array[1] is a pjs so *array[1] is illegal (as pjs no have operator*)
That simply should be
array[1].size = 1;
Correct your 1st statement of initArray to :
array[1].size = 1;
and cut paste the struct declaration to before the function.
If you want to initialize the entire array, you need to pass the array and a size to initArray.
int main(int argc , char** argv)
{
struct pjs array[10];
initArray(array, sizeof(array)/sizeof(array[0]));
system("PAUSE");
return (0);
}
and then, initialize each object the array as:
void initArray(struct pjs* array, size_t size)
{
for (size_t i = 0; i < size; ++i )
{
array[i].size = 1;
array[i].price = 0; // Or whatever makes sense
array[i].name = malloc(1); // Or whatever makes sense.
}
}

C++ Error -- "cannot convert 'char (*)[3]' to 'char*' for argument '1' to 'char rLeft(char*, int, int)' rLeft(cI,4,3);"

i am trying to create a program that will act as a rubik's cube. I created multi-dimensional arrays to act as a row for the rubik's cube (4*3).
I'm trying to create a function that will rotate one of the arrays the the left switching [3][0] to [2][0] and [2][0] to [1][0]...etc, and I'm getting this error and need help fixing it.
#include <iostream>
using namespace std;
char rLeft(char* const rArray,int rows,int colms);
//~~~~~~~~~~~~~~~~~~~~~~~~Left,~~~~~~~~Center,~~~~~~~~Right,~~~~~~~~Back~~~~~~~//
char rI [4][3] = {{'r','r','r'},{'b','b','b'},{'o','o','o'},{'g','g','g'}};
char rII [4][3] = {{'r','r','r'},{'b','b','b'},{'o','o','o'},{'g','g','g'}};
char rIII [4][3] = {{'r','r','r'},{'b','b','b'},{'o','o','o'},{'g','g','g'}};
char cI [4][3] = {{'w','w','w'},{'r','r','r'},{'y','y','y'},{'o','o','o'}};
char cII [4][3] = {{'w','w','w'},{'r','r','r'},{'y','y','y'},{'o','o','o'}};
char cIII [4][3] = {{'w','w','w'},{'r','r','r'},{'y','y','y'},{'o','o','o'}};
int main()
{
cout << "Hello World!" <<endl;
rLeft(rI,4,3);
return 0;
}
char rLeft(int rArray[4][3], int rows, int colms);
{
rows = 0;
colms = 0;
for (i = 0, i < rows, i++)
{
for (ii = 0, ii < colms, ii++)
{
if (rows != 3)
{
rArray[rows][colms] = rArray[rows+1][colms]
return rArray[row][colms];
}
else
{
rArray[rows][colms] = rArray[0][colms]
return rArray[row][colms];
}
}
}
}
Well, clearly your first declaration of the function differs from the second. Make then identical:
char rLeft(int rArray[4][3], int rows, int colms);
You also need to remove the semicolon from the function definition:
char rLeft(int rArray[4][3], int rows, int colms)
// ^^^^^ (no semicolon)
{
// ...

Confusing clone passing down argument

So basically I am solving the famous "Philosopher dining" problem, 5 philosopher is generating out using clone. The point is I want each philosopher to hold a id (from 0 to 4). I plan to do it using clone passing down argument. Here is the code (I omit some sub function)
void philoshopher(void* arg)
{
int i = &arg;
while (TRUE)
{
printf("Philosopher %d is thinking", i);
take_forks(i);
printf("Philosopher %d is eating", i);
sleep(2);
put_forks(i);
}
}
int main(int argc, char **argv)
{
int i;
int a[N] = {0,1,2,3,4};
void* arg;
/*
struct clone_args args[N];
void* arg = (void*)args;
*/
if (sem_init(&mutex, 1, 1) < 0)
{
perror(NULL);
return 1;
}
for (i=0;i<N;i++)
{ if (sem_init(&p[i], 1, 1) < 0)
{
perror(NULL);
return 1;
}
}
int (*philosopher[N])() ;
void * stack;
for (i=0; i<N; i++)
{
if ((stack = malloc(STACKSIZE)) == NULL)
{
printf("Memorry allocation error");
return 1;
}
int c = clone(philosopher, stack+STACKSIZE-1, CLONE_VM|SIGCHLD, &a[i]);
if (c<0)
{
perror(NULL);
return 1;
}
}
//Wait for all children to terminate
for (i=0; i<4; i++)
{
wait(NULL);
}
return 0;
}
After compile out, I get this error:
passing argument 1 of ‘clone’ from incompatible pointer type [enabled by default]
expected ‘int (*)(void *)’ but argument is of type ‘int (**)()’
I also try to cast this to a void pointer but still same result:
void* arg;
....
arg = (void*)(a[i]);
int c = clone(...., arg);
Anyone know how to fix this. Thanks for your help.
You aren't declaring your function pointer correctly. It should look like this:
int (*philosopher[N])(void*);
Basically when you're declaring function pointers you have to specify the argument type, because pointers to functions that accept different types are (thankfully!) incompatible with each other.
I think you also need to remove the & before a[i] in the function call. That's giving you a pointer to a function pointer, and it just expects a plain function pointer apparently.