2
L1     void func1(){ ... /* func code */ }
L2     void func2(){ ... /* func code */ }

L3     uint32 array[3]= {0}; /*  typedef unsigned int uint32*/

L4     int main(){ 
L5     array[0] =  0x12345678;
L6     array[1] =  (uint32)&func1;
L7     array[2] =  (uint32)&func2;
L8     } 

The above code (Line 6 & 7) successfully casts the address of func1 and func2 in 32 bit and saves it at the desired index.

The compiler throws an error that "expression must have arithmetic type" when I try to initialize the array outside the function (see line 3 in the following code).

L1     void func1(){ ... /* func code */ }
L2     void func2(){ ... /* func code */ }


L3     uint32 array[3] = { 0x12345678, (uint32)&func1, (uint32)&func1 };
natgeoInn
  • 21
  • 3
  • Where is `uint32` defined and how? `uint32` is not a standard library type nor defined by the language. – chux - Reinstate Monica Dec 03 '16 at 09:06
  • Now `c` or `c++`, not the same? – Bence Kaulics Dec 03 '16 at 09:10
  • 1
    works fine for me if I use `unsigned long long` as `uint32`, and I get "error: initializer element is not constant" if I set 32 bit. If your compiler is 64 bit, and you're trying to put function pointers in 32 bit integers it's going to be a problem. – Jean-François Fabre Dec 03 '16 at 09:12
  • 1
    @Bence Kaulics thanks corrected the tag – natgeoInn Dec 03 '16 at 09:27
  • @ chux uint32 is 32 bits -> unsigned int – natgeoInn Dec 03 '16 at 09:27
  • @Jean-FrançoisFabre My architecture is 32 bit. the address of function is in 32 bit address space – natgeoInn Dec 03 '16 at 09:45
  • that said, my attempts were made with a 32 bit compiler. But your code gave me `func.c:6:35: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] uint32 array[3] = { 0x12345678, (uint32)func1, (uint32)func1 }; ^ func.c:6:3: error: initializer element is not constant uint32 array[3] = { 0x12345678, (uint32)func1, (uint32)func1 };`. I'm using gcc 6.2, and you? – Jean-François Fabre Dec 03 '16 at 09:48
  • @natgeoInn which C programming language standard you are using? – Sumit Gemini Dec 03 '16 at 10:26
  • @Jean-FrançoisFabre am using GHS. – natgeoInn Dec 04 '16 at 02:36
  • Consider using `uintptr_t`. – John Zwinck Dec 04 '16 at 02:42
  • The SOLUTION I found out is from the (GHS) compiler manual. I was using a flag -C99 (Strict ISO C99) which did not allow any non-standard constructs. So I changed it to -c99 (notice small c) which produces warnings on some non standard constructs rather than error. So Whew!!! I am able to compile. – natgeoInn Dec 06 '16 at 10:20

0 Answers0