Solved

Pass an array of pointers to structs by reference to a function (C)

Posted on 2011-02-15
4
429 Views
Last Modified: 2012-05-11
Experts,

how do I pass a fixed-size array of pointers to structs by reference to a function in C?

Below is the code of what I'm trying to do but I'm getting the error:
cannot convert from 'MyStruct *' to 'MyStruct'

Thanks!


#include "stdafx.h"
#include <stdio.h>
#include <string.h>

#define MAX_SIZE_MY_STRUCTS 2

typedef struct _MyStruct
{
    char name[40];
    int index;
} MyStruct;

int populateStructs(MyStruct **my_struct_list)
{
    int i = 0;

    for (i=0; i < MAX_SIZE_MY_STRUCTS; i++) {
        (* my_struct_list[i]) = (MyStruct *)malloc(sizeof(MyStruct));
        strcpy(* my_struct_list[i]->name, "blah blah");
        (* my_struct_list[i])->index = i;
    }

    return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
    int i = 0;
    MyStruct *my_struct_list[MAX_SIZE_MY_STRUCTS] = {0};

    populateStructs(& my_struct_list);

    for (i=0; i < MAX_SIZE_MY_STRUCTS; i++) {
        printf("Name: %s\n",my_struct_list[i]->name);
        printf("Index: %d\n",my_struct_list[i]->index);
    }

	return 0;
}

Open in new window

0
Comment
Question by:php-newbie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 34901385
Change the function prototype to this..

int populateStructs(MyStruct *(*my_struct_list)[MAX_SIZE_MY_STRUCTS])

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 34901396
You will also need to change line 19 to this

(* my_struct_list[i]) = (MyStruct *)malloc(sizeof(MyStruct));

Open in new window

0
 
LVL 9

Accepted Solution

by:
AriMc earned 500 total points
ID: 34901463
Here's a working version (in MSVC 6):


#include <windows.h>
#include <stdio.h>
#include <string.h>

#define MAX_SIZE_MY_STRUCTS 2

typedef struct _MyStruct
{
    char name[40];
    int index;
} MyStruct;

int populateStructs(MyStruct **my_struct_list)
{
   int i = 0;

   for (i=0; i < MAX_SIZE_MY_STRUCTS; i++)
   {
      my_struct_list[i] = (MyStruct *)malloc(sizeof(MyStruct));
      strcpy(my_struct_list[i]->name, "blah blah");
      my_struct_list[i]->index = i;
   }

   return 0;
}


int main(int argc, char* argv[])
{
   int i = 0;
   MyStruct *my_struct_list[MAX_SIZE_MY_STRUCTS] = {0};

   populateStructs(my_struct_list);

   for (i=0; i < MAX_SIZE_MY_STRUCTS; i++)
   {
      printf("Name: %s\n",my_struct_list[i]->name);
      printf("Index: %d\n",my_struct_list[i]->index);
   }

   return 0;
}

Open in new window

0
 

Author Closing Comment

by:php-newbie
ID: 34906795
Thanks!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question