Thread: Passing a pointer to struct array

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

    Passing a pointer to struct array

    I have a struct
    Code:
    typedef struct
    {
       char *name;
       float value;
       void *pntr;
       uint8_t found;
    }DICTIONARY;
    Initialize it
    Code:
    static DICTIONARY pid_dict[] = 
    {
        {"-p" ,  0, 0, 0 },
        {"-i" ,  0, 0, 0 },
        {"-d" ,  0, 0, 0 },
        {"-max", 0, 0, 0 },
        {"-min", 0, 0, 0 },
        {"-var", 0, 0, 0 },
    };
    Function to work with the struct
    Code:
    uint32_t ParseDictionary(DICTIONARY *dict[], char** args, uint32_t argc)
    {
        uint32_t dict_len = (sizeof (dict)/sizeof (dict[0]));
        
        uint32_t i, j;
        float val;
        
        for (i = 0; i < argc; i++)
        {
            for (j = 0; j < dict_len; j++)
            {
                if (strcmp(args[i], dict[j]->name)==0)
                {
                    if (IsFloat(args[i+1]))
                    {
                        val = atof(args[i+1]);
                        dict[j]->value = val;
                        dict[j]->found = 1;
                    }
                    else
                        return MSG_INV_ARG;
                }
            }
        }
        
        return MSG_OK; 
    }
    Now I want to pass it to a function
    Code:
    ParseDictionary(&pid_dict, &args[1], argc);
    I get - Error[Pe167]: argument of type "DICTIONARY (*)[6]" is incompatible with parameter of type "DICTIONARY **"
    And this way
    Code:
    ParseDictionary(&pid_dict[0], &args[1], argc);
    I get -Error[Pe167]: argument of type "DICTIONARY *" is incompatible with parameter of type "DICTIONARY **"

    What is wrong?
    Last edited by john7; 02-09-2020 at 04:49 AM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Code:
    uint32_t ParseDictionary(DICTIONARY *dict[], char** args, uint32_t argc)
    
    {
    
        uint32_t dict_len = (sizeof (dict)/sizeof (dict[0]));
    First, the function should be

    Code:
    uint32_t ParseDictionary(DICTIONARY *dict, char** args, uint32_t argc)
    Second, you can't calculate dict_len the way you are doing it because the actual array is not in scope (when you pass it it decays to a pointer so the function has no idea what sizeof (dict) is). So, you'll have to pass then length of the array as well

    Edit: there are other ways to do this, but passing the length of the array is, in my opinion, the most flexible

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have an array of DICTIONARY objects, so when you pass it as an argument, it is converted to a pointer to its first element. Hence, you should declare your function like this:
    Code:
    uint32_t ParseDictionary(DICTIONARY dict[], uint32_t dict_len, char** args, uint32_t argc)
    {
    Then call it like this:
    Code:
    ParseDictionary(pid_dict, sizeof(pid_dict) / sizeof(pid_dict[0]), &args[1], argc);
    I added the dict_len parameter because your approach of using sizeof to compute the array length simply won't work within the function as the dict parameter is a pointer, despite the apparent use of array notation to declare it.

    You should be aware that the type of the result of sizeof is size_t, which is not necessarily equivalent to uint32_t.

    Also, it is a common convention to use fully uppercase names for constants and/or macros. Consequently, you might wish to rename DICTIONARY to Dictionary.
    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

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    68
    This way
    uint32_t ParseDictionary(DICTIONARY *dict, char** args, uint32_t argc)
    I can not iterate - on dict[j]->name I get Error[Pe044]: expression must have pointer type

    This way
    uint32_t ParseDictionary(DICTIONARY dict[], char** args, uint32_t argc)
    I pass it by value - a local copy - I can not modify a member - dict[j]->value = val; only a local copy dict[j].value = val;

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by john7
    I can not iterate - on dict[j]->name I get Error[Pe044]: expression must have pointer type
    That's because you're trying to treat dict[j] as a pointer when it is an object. Write dict[j].name instead.

    Quote Originally Posted by john7
    I pass it by value - a local copy - I can not modify a member - dict[j]->value = val; only a local copy dict[j].value = val;
    C only has pass by value semantics. Pass by reference can still be done through a pointer... and that's exactly what you're doing here, so you're mistaken when you say that you're dealing with "a local copy".
    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

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    68
    I see. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointer to array of struct pointers
    By _jagged in forum C Programming
    Replies: 2
    Last Post: 08-20-2019, 08:39 PM
  2. passing struct pointer to function.
    By Durango2011 in forum C Programming
    Replies: 3
    Last Post: 10-28-2011, 07:10 PM
  3. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  4. Passing Pointer-to-struct to a function!!
    By Lau in forum C Programming
    Replies: 5
    Last Post: 11-18-2002, 12:59 PM
  5. Passing a pointer to a struct
    By Natase in forum C Programming
    Replies: 2
    Last Post: 10-02-2001, 10:29 AM

Tags for this Thread