Thread: Setting a value with a generic pointer.

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    68

    Setting a value with a generic pointer.

    I have a generic pointer cause I need to work with different data types (uint8_t, int, float)
    Code:
     void *iter_first;
    now I initialize it
    Code:
    uint8_t flag[8];
    iter_first = &flag[4];
    till now it's OK. but now I want to set a value and I should decide the size of the value.
    Code:
    int size = sizeof(iter_first);
    
    if (size == 1)
        (*((uint8_t *)iter_first)) = iter_first_val;
    else
        (*((int *)iter_first)) = iter_first_val;
    size always 4 so I override all the bunch flag[4] - flag[7].

    and if I try to cast
    Code:
    (uint8_t *)iter_first = &flag[4];
    I get - Error[Pe137]: expression must be a modifiable lvalue

    What do I miss?
    Last edited by john7; 12-26-2022 at 06:20 AM.

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Quote Originally Posted by john7 View Post

    Code:
    int size = sizeof(iter_first);
    
    if (size == 1)
        (*((uint8_t *)iter_first)) = iter_first_val;
    else
        (*((int *)iter_first)) = iter_first_val;
    Do you really need the if statement here? You are taking the sizeof a void pointer.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    68
    Quote Originally Posted by G4143 View Post
    Do you really need the if statement here? You are taking the sizeof a void pointer.
    How can I tell what data type it points to? It's important if I want to set a value to a variable. Strangely enough I can not cast the pointer

    Code:
    (uint8_t *)iter_first = &flag[4];

  4. #4
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Quote Originally Posted by john7 View Post
    How can I tell what data type it points to?
    You can't... Memory is not type-identifiable. Memory is memory. You either create a memory pointer that's typed to the type you want or you create some sort of scheme for that's used to identify a memory section as <enter type here>.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Something like
    Code:
    struct foo 
      enum { IS_CHAR, IS_INT, IS_DOUBLE } type;
      union {
        char *cp;
        int *ip;
        double *dp;
      } v;
    };
    
    int main  ( ) {
      struct foo bar;
      int val;
      bar.type = IS_INT;
      bar.v.ip = &val;
    
      /// 
    
      switch ( bar.type ) {
        case IS_INT:
      }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    68
    Quote Originally Posted by Salem View Post
    Something like
    yes but on 100 v I need 100 type - added RAM waste (in my case it's embedded, RAM limited system). I thought there is a way to avoid it.
    why the compiler rejects the cast
    Code:
    (uint8_t *)iter_first = &flag[4];
    what's criminal in it?
    Last edited by john7; 12-27-2022 at 06:15 AM.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    68
    but wait a minute
    isn't enough?
    Code:
    struct foo 
      union {
        char *cp;
        int *ip;
        double *dp;
      } v;
    };
    
    bar.v.cp = &var;
    the union overlays the type. does it?

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    68
    ОК. what about
    Code:
    if (val < 256)
        (*((uint8_t *)generic)) = val;
    else
       (*((int *)generic)) = val;
    any way it would be properly aligned in memory? wouldn't it?
    Last edited by john7; 12-27-2022 at 08:09 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > yes but on 100 v I need 100 type - added RAM waste
    It's a union.
    No matter how many pointer types you store in your union, it will only be as big as a pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generic Pointer Arithmetic
    By wiqxlzxsxj in forum C Programming
    Replies: 2
    Last Post: 02-09-2017, 07:37 AM
  2. How to create a generic Function Pointer?
    By Arshad Nazeer in forum C Programming
    Replies: 4
    Last Post: 01-08-2017, 06:18 AM
  3. generic pointer to char conversion problem
    By monkey_c_monkey in forum C++ Programming
    Replies: 5
    Last Post: 07-12-2012, 07:06 AM
  4. Generic pointer
    By neu_greg in forum C Programming
    Replies: 3
    Last Post: 04-02-2009, 02:30 PM
  5. Generic Template Pointer
    By LuckY in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2004, 01:13 PM

Tags for this Thread