Where is my error?

I work on C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>
int a,b;
int add(){
    return (a+b);
    }
int add_transaction[]={
    add
    };

int main()
{
    if(a==5&&b==6)
  printf("%d%s",add_transaction[0],"\n");
  if(a==1&&b==2)
  printf("%d%s",add_transaction[1],"\n");
  system("PAUSE");	
  return 0;
}


And i take this warning error:
 
[Warning] initialization makes integer from pointer without a cast 
Last edited on
When write add without parentheses somewhere in your code, it evaluates to a function pointer.
It is unclear to me what your code is supposed to do, so I can't say how to fix it (adding '()' on line 8 would remove the warning, although your code has problems apart from that..).
here is the right code

#include <stdio.h>
#include <stdlib.h>
int a,b;
int add(){
return (a+b);
}
int add_transaction[]={
add()
};

int main()
{
if(a==5&&b==6)
printf("%d%s",add_transaction[0],"\n");
if(a==1&&b==2)
printf("%d%s",add_transaction[1],"\n");
system("PAUSE");
return 0;
}
@asrar, there isn't much "right" about it...
this code can compile on Dev c++. Please tell me what environment you use for this code. and what you want to do with this code.
You initialise your array add_transaction[] with only one integer, so its size is one; You are thus out of bonds when accessing add_transaction[1]. Also the initialisation of the array is trying to add up a and b, which are never initialized;
@asrar Thank u for ur think.But it doesn't work:
https://rapidshare.com/files/2240070058/Compile_Error.bmp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>
int a,b;
int add(){
  return (a+b);
    }
int add_transaction[]={
    add,add
    };

int main()
{
    if(a==5&&b==6)
  printf("%d%s",add_transaction[0],"\n");
  if(a==1&&b==2)
  printf("%d%s",add_transaction[1],"\n");
  system("PAUSE");	
  return 0;
}

This is my revised code.I want to change add values in an array.
Couldn't you just paste that one line of error massage? And you never did say what you were trying to do.
Anyway, the quick fix would be to do
1
2
3
4
5
6
7
8
//...
int add_transaction[2];

int main()
{
   add_transaction[0] = add();
   add_transaction[1] = add();
   //... 
Hey there , you have declared your add_transaction[] array to be holding integers and trying to store int& in it
I mean
"add" gives the address of function where from it starts
and even it's integer value it cannot be stored in integer array . You have to do as follows

int (*add_transaction_array)(void)[2] = { add , add } ; // will work fine , or

That's it !!!

Now "add" will yield int& and our array is now capable of storing address of function having signature
int func_name(void) ;
I given an example.I want to be my array's element item 3,4,5,50,638...
@SwapnilGhode And i tried before ur suggested method.But funcs and arrays doesn't work together.
@hamsterman I don't take an error.But doesn't write numbers in screen:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>
int a,b;
int add(){
  return (a+b);
    }
int add_transaction[2];

int main()
{
    if(a==5&&b==6){
      add_transaction[0]=add();            
  printf("%d%s",add_transaction[0],"\n");}
  if(a==1&&b==2){
       add_transaction[1]=add();             
  printf("%d%s",add_transaction[1],"\n");}
  system("PAUSE");	
  return 0;
}
Last edited on
Topic archived. No new replies allowed.