Solved

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

Posted on 2011-02-15
4
424 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now