Solved

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 83
mixing C++ & C# in Vis Studio 2013 7 177
I could not build boost code, 10 88
Header of docx file 17 103
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 goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

810 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