Thread: Passing pointer to array of struct pointers

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    2

    Unhappy Passing pointer to array of struct pointers

    Hello everyone,

    I've been learning C for a few months now and I'm stuck on an aspect of passing pointers.


    Here's what I think is a pointer to an array pointer of struct pointers:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define ARR_SIZE 5
    
    
    struct my_struct{
        int num_one;
        int num_two;
    };
    
    
    int main (void)
    {
        struct my_struct* struct_arr[ARR_SIZE];
        for(int i = 0; i < ARR_SIZE; i++)
        {
            struct_arr[i] = malloc(sizeof(struct my_struct));
            struct_arr[i]->num_one = i;
            struct_arr[i]->num_two = i + 50;
        }
    
    
        struct my_struct *(*p)[] = &struct_arr;
    
        for(int i = 0; i < ARR_SIZE; i++)
        {
            printf("array member: %d\n", i);
            printf("num_one: %d - num_two: %d\n\n", (*p)[i]->num_one, (*p)[i]->num_two);
        }
        return 0;
    }
    I can access and modify the elements by using (*p)[i], as shown above. But what is the syntax to pass *(*p)[] to a function to modify?
    Code:
    void mod_arr (struct my_struct *(*ms))
    {
        for(int i = 0; i < ARR_SIZE; i++)
        {
            (*ms)[i]->num_one = 50;
            (*ms)[i]->num_two = 30;
        }
    }
    //I'm trying to call the function like this:
    mod_arr(*(*p));
    I've tried what feels like every variation of syntax in the arguments and I cannot find the correct way to pass. Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you generally don't need a pointer to an array itself unless the array is part of an array of arrays. So in this case, you're overcomplicating by doing this.

    Having said that, the syntax would be:
    Code:
    void mod_arr(struct my_struct *(*ms)[ARR_SIZE])
    Then you would call the function like this:
    Code:
    mod_arr(&struct_arr);
    Or if you prefer:
    Code:
    struct my_struct *(*p)[ARR_SIZE] = &struct_arr;
    mod_arr(p);
    Note that you shouldn't omit ARR_SIZE from the parameter declaration: in this case the parameter is a pointer to an array itself, so the size of the array matters. This is unlike passing an array that is implicitly converted to a pointer to its first element, in which case the array size doesn't matter because all that is passed is a pointer to the first element.
    Last edited by laserlight; 08-20-2019 at 08:23 PM.
    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
    Aug 2019
    Posts
    2
    Thanks for your reply, I can see now that I've added an unnecessary layer. And that I can use mod_arr(&(*p));
    Cheers!

    I just saw your edit... I'm really over complicating my calls

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing struct pointer to function.
    By Durango2011 in forum C Programming
    Replies: 3
    Last Post: 10-28-2011, 07:10 PM
  2. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  3. Passing struct pointers
    By Molokai in forum C Programming
    Replies: 5
    Last Post: 05-20-2010, 10:53 PM
  4. Replies: 11
    Last Post: 12-30-2009, 04:04 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