?
Solved

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

Posted on 2011-02-15
4
Medium Priority
?
432 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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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 …
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 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.
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

752 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