'C' question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,

I have experimented with passing an array of POINT structures to a function.
I did this like this:

First I declare my function:

void f(POINT[]);

Then I innitialized the array as so:

POINT ptsBase[6] = {40,265, 130,265, 80,265, 80,60, 200,60, 200,85};

then I call my function passing the array of structures as so:

f(ptsBase);

and the function its self is written as so:

void f(POINT ptsBase[])
{ //some code}

This works fine and is no problem.

However, I am now trying to pass an array of structures to a function where
the structure is one that I created, but this doesn't work the same way since
it generates an error. I have never done this before. Here is my atempt:

Structure that I created/declared
================================
struct
{
int RNumber;
TCHAR *szRName;
}
RIndex[] =
{
1 ,TEXT("RedRail"),
2 ,TEXT("BlueRail"),
3 ,TEXT("GreenRail"),
4 ,TEXT("BlackRail #1"),
5 ,TEXT("BlackRail #2"),
6 ,TEXT("WhiteRail")
};

Here is the calling the function where I would like to pass it the array of
structures that I created
-------------------------------
case WM_PAINT:

hdc = BeginPaint(hwnd,&ps);
f(RoomIndex);
EndPaint(hwnd,&ps);
return 0;


---------------------------------
//My function
void f(struct RoomIndex[])
{
//some code...
}
==============================

This gives me the following error,

c:\_DTS_PROGRAMMING\vc++\MY_APPS_LAB\APPLICATION_1\APPLICATION_1.cpp(227) :
error C2664: 'RL1_txtDISPLAY_GB_ArSt2ic' : cannot convert parameter 1 from
'[6]' to 'RoomIndex []' Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast

I wish someone can help me understand what I am doing wrong. Its seems to
have something to do with casts, but I have never casted anything under this
context. I have looked for examples in books but unfortunately don't seem to
find any, the only documentation I have found is on structure basics,
pointers to structures, nested structures, understanding bit fields and
create unions, but nothing on passing an array of structures which the
programmer would create as done so in my sample.

All suggestions and help is very appreciated....

Thankyou guys!

--
Best regards
Robert
.



Relevant Pages

  • Re: two dimensional arrays:
    ... >Check out the following code for building a 2 dimensional array: ... there is no guarantee that a void** is in any way ... Since your two allocate functions use the same allocation logic, ... harmless) cast of the second argument to memcpy. ...
    (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: 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: Suggestions about transparent data structures
    ... You'll get a lot of suggestions from people recommending void*. ... here's an array ADT that uses it ... aDestructorCallback); ... array_index_type ArrayIndexOf(const Array array, const void* aItem, ...
    (comp.lang.c)
  • Re: multi dimensional arrays as one dimension array
    ... Changing the cast to void* only creates more work for the compiler. ... first convert array (which in this context is identical to &array ... If your system has built in hardware assist for bounds checking, ...
    (comp.lang.c)