Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
max float value 3 72
Please explain C language error codes ? 5 51
Finding Divisors 5 60
Coding for the first time 9 99
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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