Thread: An assignmentof a void argument

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

    An assignmentof a void argument

    If I write
    Code:
    void COM_Esp8266ParamRead(char* param, void *ret_val)
    {
        if(strcmp(param,"locip")==0)
        {
             ret_val = esp_params.loc_ip;  //char loc_ip[15]
        }
    }
    It compiles OK.
    But this way
    Code:
    void COM_Esp8266ParamRead(char* param, void *ret_val)
    {
        if(strcmp(param,"locip")==0)
        {
             *(char*)ret_val = esp_params.loc_ip;  //char loc_ip[15]
        }
    }
    I get an error
    Error[Pe513]: a value of type "char *" cannot be assigned to an entity of type "char"
    Why I can not cast it?
    Last edited by john7; 01-27-2019 at 03:01 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can cast it, but then you can neither assign to it (the cast result is an rvalue) nor dereference it for assignment (the result of the dereference is a char, but you're assigning a pointer to char, hence the error message).

    What is the pointer to void actually supposed to point to?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    68
    Quote Originally Posted by laserlight View Post
    You can cast it, but then you can neither assign to it (the cast result is an rvalue) nor dereference it for assignment (the result of the dereference is a char, but you're assigning a pointer to char, hence the error message).

    What is the pointer to void actually supposed to point to?
    It's void because I have a struct
    Code:
    typedef struct
    {
        uint8_t ena;
        uint8_t debug;
        uint8_t mode; 
        uint8_t con_mode; 
        
        char rem_ip[15];
        char rem_port[4];
        char loc_ip[15];
        char loc_port[4];
    
        uint32_t timeout;
    } ESP8266_PARAMS;
    with some different data types.
    So I want to provide a generic function to read\write the struct parameters.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I didn't ask why it is a pointer to void; I asked what is it actually supposed to point to.

    If it points to the first char in an array of char that you want to copy this string to, then perhaps strcpy (or strncpy, or strncat) would work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    68
    Quote Originally Posted by laserlight View Post
    I didn't ask why it is a pointer to void; I asked what is it actually supposed to point to.

    If it points to the first char in an array of char that you want to copy this string to, then perhaps strcpy (or strncpy, or strncat) would work.
    thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Argument inside the Argument of a Function Declaration
    By manasij7479 in forum C++ Programming
    Replies: 3
    Last Post: 06-11-2011, 05:53 AM
  2. Replies: 5
    Last Post: 01-24-2011, 05:37 AM
  3. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  4. 'void' in function argument
    By lruc in forum C Programming
    Replies: 14
    Last Post: 08-25-2008, 10:12 PM
  5. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM

Tags for this Thread