Click here to Skip to main content
16,016,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I wanted to know how I can pass two structs to the same function. I'm getting an error
error: incompatible type for argument 1 of fun
if I use two structs as the same type.
Here is the code

What I have tried:

typedef struct First            		
   {
    //members
    ....
   }first;

void fun( first f[size] ){
// some computation on str1 and I want to applay same computation on str2
 ....
 for(i=0;i<3;i++){
    f[i].member +=5;
 }
} 
void cal(first str1[4],first str2[4] ){
// some calculation on str1
for(i=0;iNsomesize;i++){
str2[i].member=str1[i].member;
}
fun(str2[4]);

}

 void main(){
first str1[4]; 
first str2[4];
fun(str1); // I call just str1 because firstly I want to applay fun computation on 
              //str1
}
Posted
Updated 10-May-18 10:43am
v4

You need pointers. Let's try a simple example
C
#include <stdio.h>

typedef struct
{
  int x, y;
} Point;

void doubleIt( Point * p )
{
  p->x = p->x * 2;
  p->y = p->y * 2;
}

void printIt( const Point * p)
{
  printf("{%d, %d}\n", p->x, p->y);
}

int main()
{
  Point p1, p2;
  Point pa[4];
  int n;

  p1.x = 1;
  p1.y = 1;

  p2.x = 0;
  p2.y = 2;

  // accessing p1
  printIt( &p1);
  doubleIt(&p1);
  printIt( &p1);

  // accessing p2
  printIt( &p2);
  doubleIt(&p2);
  printIt( &p1);

  // accessing the pa array
  for (n = 0; n<4; ++n)
  {
    pa[n].x = n;
    pa[n].y = -n;
    printIt(&pa[n]);
    doubleIt(&pa[n]);
    printIt(&pa[n]);
  }
}
 
Share this answer
 
Comments
Lilyanaa 10-May-18 16:23pm    
Ok, thanks, something like this.. but can I do without a pointer?
CPallini 10-May-18 16:48pm    
Mastering pointers is mandatory, in order to code in the C programming language.
Firstly, str1 is not a first, it's an array of first objects:
C++
first str1[4];
So your method definition has to match that, as it does with cal.
But there are problems here, as str2 is an array of 4 first objects, so this:
C++
str2[0].member=fun(str2[4]);
Tries to access a fifth element of teh array - C indexes start at zero, remember.
If you are trying to pass a single instance of first to fun, then declare it as
C++
void fun(first f) {
...
}
And call it with an element form one of your arrays:
C++
fun(str1[0]);
fun(str1[1]);
fun(str1[2]);
fun(str1[3]);
fun(str2[0]);
fun(str2[1]);
fun(str2[2]);
fun(str2[3]);
If you want to pass an array of first objects:
C++
void fun(first f[]) {
...
}
And pass it the array:
C++
fun(str1);
fun(str2);
But either way, this won't work:
C++
str2[0].member=fun(str2[4]);
Because fun is a void function - iut cannot return a value, so it can;t be used on the right of an equals sign.

I think you need to sit down and re-read your course notes: you appear to be a little confused as to what you are trying to do here.
 
Share this answer
 
Comments
Lilyanaa 10-May-18 16:34pm    
thanks for your notes, can you see my update please, I do something like that, but the error same incompatible type for argument 1 of fun
OriginalGriff 11-May-18 1:56am    
Make your mind up!
fun(str2[4]);
...
fun(str1);
Since str1 and str2 are declared the same way ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900