Thread: Type casting

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    1

    Type casting

    Im a super newbie with c so this probably looks like crap but it works. I have a pointer array in memory that starts at Points_Array and steps through getting each record in memory. The first member in the structure is a jump address to the correct function that processes infomation in the structure.

    It compiles with warning: assignment makes pointer from integer without a cast [-Wint-conversion]

    I cant figure out how to type cast for the function below. Could someone show me how to do that.

    Thanks.

    Code:
    void (*pf)(uint8_t, uint8_t, uint32_t*);
    
    void processPoints(void)
    {
        uint8_t a = Pnt_Count;
        uint32_t *p;
        uint32_t *pa = Points_Array;
        while (a)
        {
            p =  (uint32_t*) *pa;
    
            pf = *p; /* warning is for this line */
    
            pf(PNT_PROCESS, a, p);
            --a;
            ++pa;
        }
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Your code would be easier to understand if you used real variable names.

    The only thing you can assign to pf without a complaint from the compiler is a function name. For example, say there was a function foo that had the same signature as pf, you would do
    pf = foo;
    and foo's signature would be something like:
    void foo(uint8_t mode, uint8_t size, uint32_t *array);

    Then the code would compile clean. It is merely a way to call foo() through the pf pointer.

    Though I note that you have other suspicious lines as well.
    p = (uint32_t *) *pa;
    I think this p variable is completely unnecessary, because all you are doing is dereferencing in the middle of the array and trying to cast the resulting integer into a pointer type.

    To me it looks like you can just call pf like so.
    pf(PNT_PROCESS, a, pa);


    You may not even need pf. Its use certainly isn't justified by this small code fragment.

    I have a pointer array in memory that starts at Points_Array and steps through getting each record in memory. The first member in the structure is a jump address to the correct function that processes infomation in the structure.
    For what it's worth, I don't see any structures in your code.
    Last edited by whiteflags; 06-21-2018 at 03:14 PM.

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    2
    You use wrong contain type.

    Code:
    /* Normally. */
    Delegate = &Method_name;
    Code:
    int m_plus(int a, int b){ return a + b; }
    int m_minus(int a, int b){ return a - b; }
    int (*d_m)(int, int);
    
    
    int main()
    {
        int _a = 10, _b = 5, _c = 0, _d = 0;
    
    
        void * _m[2] = {&m_plus, &m_minus};
    
    
        d_m = _m[0];
        _c = d_m(_a, _b);
    
    
        d_m = _m[1];
        _d = d_m(_a, _b);
        
        return 0;
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with type casting
    By Bissa221 in forum C Programming
    Replies: 1
    Last Post: 02-21-2014, 05:56 AM
  2. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 11:09 AM
  3. type casting
    By liats80 in forum C Programming
    Replies: 3
    Last Post: 10-15-2007, 07:38 PM
  4. difference between type conversion and type casting
    By Bargi in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 03:17 AM
  5. type casting
    By cyberCLoWn in forum C++ Programming
    Replies: 3
    Last Post: 12-31-2003, 08:13 AM

Tags for this Thread