Re: Aaahhrg! Trouble passing an array of structs to a function!



Hello Barry,

Why do you have two different structure types which are identical?

This was just for example purpose, in reality they have different data.

I like your idea of using a pointer to void as a parameter. I used void
pointers once before, but not in this way. I did come up with a resolution,
but I get warnings as you will see in the code sample below.

You still have to provide the function
enough information to figure what the void* actually points to.

This is understood as I can compare the data from the array passed in to the
other parameters past in the function.

All I want to do is when in a function (for example in F1() shown below) I
want to call another function ( GetArrayElementAddress()) to return the
address of a specific element from an array of my choice (in F1(), I chose
the pc array) based on some comparisons as described in the previous
paragraph.

For example, in F1() (see code below) I may want the start address of the
array element in pc which contains 101 as its winID member data. Therefore in
F1(), the GetArrayElementAddress() must be able to return the address of the
second element of pc array all this by using void pointers. I am hoping that
by using void pointers this way the GetArrayElementAddress() function will be
able to carry out this functionality for any array of structures that I pass
to it.

Anyways, this is what I tried but gives me several warnings as shown below:

c:\dts_visual_c++\test1\test1\newsg.c(42) : warning C4047: 'function' :
'void **' differs in levels of indirection from 'PC [3]'

c:\dts_visual_c++\test1\test1\newsg.c(42) : warning C4022:
'GetArrayElementAddress' : pointer mismatch for actual parameter 1

c:\dts_visual_c++\test1\test1\newsg.c(52) : warning C4047: 'function' :
'void **' differs in levels of indirection from 'MN [3]'

c:\dts_visual_c++\test1\test1\newsg.c(52) : warning C4022:
'GetArrayElementAddress' : pointer mismatch for actual parameter 1

Please consider this code:
==================================
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define xxx sizeof(pc/pc[0])
#define yyy sizeof(mn/mn[0])

typedef struct pc_g{
long winID;
char *name;
long code;
}PC;

PC pc[3];

typedef struct mn_g{
long winID;
char *name;
long connections;
}MN;

MN mn[3];

void *GetArrayElementAddress(void *c[], long win_ID, char *name)
{
int x = 0;

// other code here which tests win_ID and name!
// and sets x to the appropriate element in array!

return &c[x];
}



F1()
{
long p=11;
PC *x;

x =GetArrayElementAddress(pc, 101, "entry");
x->winID = p;
}


F2()
{
long p;
MN *x;

x =GetArrayElementAddress(mn, 100, "admin");
p = x->winID;
}


int main ()
{

pc[0].winID = 100;
pc[0].name = "entry";
pc[0].code = 1000;

pc[1].winID = 101;
pc[1].name = "control";
pc[1].code = 1001;

pc[2].winID = 102;
pc[2].name = "Image";
pc[2].code = 1002;

F1();
F2();

return 0;
}
===================================

I don't get it?

void *GetArrayElementAddress(void *c[], long win_ID, char *name)
{....}

The above prototype requires a pointer of void type! So I am passing pc
which is the first actual location of the contents in the array. Then should
I pass it like this?

x =GetArrayElementAddress(&pc, 101, "entry");

But this doesn't work either?????

Any help much appreciated!

--
Best regards
Roberto


"Barry Schwarz" wrote:

On Mon, 16 Mar 2009 11:42:07 -0700, Robby
<Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hello,

I am quite upset with myself. Haven't we gone through examples for passing
arrays of structures to functions. I thought we did! But try this and try
that and nada.... nothing works.

Can anyone please consider the following code and help me see what I am
doing wrong. Thanking you in advance for your help!

==============================
#include <stdlib.h>

typedef struct pc_g{
long winID;
char *name;
long code;
}PC;

PC pc[3];

typedef struct mn_g{
long winID;
char *name;
long code;
}MN;

Why do you have two different structure types which are identical?
What would be the problem with making mn an array of three PC?


MN mn[3];

F1()
{
F3(pc);

By implication, F3 returns an int

//F3(mn); << I may have to pass a different array to same function parameter

Not as you have currently coded it.

}


void F3(PC pc[])

Where is the int you promised the function would return. You have to
stop lying to the compiler.

{ <<<< error here! ????
int x;
}

Also, what if I would require to pass more than one array of structures type
via a single function parameter????? Please view the F1() function for
illustration of this.

Let's assume you meant to have arrays of really different types as
opposed to your example. You have a couple of choices:

One is a variadic function where the first parameter(s)
provide the function with enough detail to deal with the "unspecified"
parameters.

Another is to use a pointer to void which can hold the
starting address of any array. You still have to provide the function
enough information to figure what the void* actually points to.

--
Remove del for email

.



Relevant Pages

  • Re: segfault w/ block, but not file scope
    ... void foo{ ... possess by-reference arguments, even ordinary local variables ... The "value" of the array is a pointer to the array's ...
    (comp.lang.c)
  • Re: qsort semantics
    ... must pass a pointer to the first object of the array to be sorted. ... int cmp(const void *a, const void *b) { ... You cast a to "pointer to an array of N char". ...
    (comp.lang.c)
  • Re: allocating memory for array of pointers to char
    ... >> checking up on allocation and so on, the types of your objects are ... > pointer inside a linked-list element. ... > typedef struct listelem_t { ... "value" is a pointer to an array of unknown length. ...
    (comp.lang.c)
  • Re: A little help please
    ... Well I guess it would be if I didn't know I was passing an array of ints ... incrementing the pointer by nBytes I was pointing to next array element.? ... array to a void* using a static cast. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: 2-dimensional arrays and functions
    ... Here are my examples again incase you missed it in this pointer pandemonium: ... int main{ ... dynamically created 2D array in ppArray ... void pArrayFunction; ...
    (alt.comp.lang.learn.c-cpp)

Loading