473,466 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with pointer to array of structures

I am trying to pass a pointer to an array of structures to function,
but
I have some problems. I am using MS Visual C++ 6.0, but I think
this is more of a C-problem than Windows programming specific. Here
is the relevant part of my code.

typedef struct
{
int iControlID;
char controlTxt[64];
} CONTROL_TXT;

void SetControlTxts(HWND hDlg, CONTROL_TXT *ctrlTxt[]);

void MyFunc(void)
{
static CONTROL_TXT ctrlTxts[] =
{
{ 1, "text 1" },
{ 2, "text 2" },
{ 3, "text 3" },
{ 0, "" } // marks end of table
};

<clip>

SetControlTxts(hDlg, &ctrlTxts[0]);
}
void SetControlTxts(HWND hDlg, CONTROL_TXT *ctrlTxt[])
{
int i, id;
char txt[64];

for (i = 0; &ctrlTxt[i]->iControlID != 0; i++)
{
id = &ctrlTxt[i]->iControlID;
strcpy(txt, &ctrlTxt[i]->controlTxt);
//strcpy(txt, (char*)ctrlTxt[i]->controlTxt);
//SetDlgItemText(hDlg, ctrlTxt[i]->iControlID,
ctrlTxt[i]->controlTxt);
SetDlgItemText(hDlg, id, txt);
}

return;

}

I get some compiler warnings. Function call "SetControlTxts(hDlg,
&ctrlTxts[0]);
":
warning C4047: 'function' : 'struct CONTROL_TXT ** ' differs in levels
of indirection from 'struct CONTROL_TXT *'
warning C4024: 'SetControlTxts' : different types for formal and
actual parameter 2

"id = &ctrlTxt[i]->iControlID;":
warning C4047: '=' : 'int ' differs in levels of indirection from 'int
*'

"strcpy(txt, &ctrlTxt[i]->controlTxt);":
warning C4047: 'function' : 'const char *' differs in levels of
indirection from 'char (*)[64]'
warning C4024: 'strcpy' : different types for formal and actual
parameter 2

Strcpy also causes access violation in Visual C++. However, I can get
valid value of "id", the debugger shows it correctly.

I also tried casting to char*:
strcpy(txt, (char*)&ctrlTxt[i]->controlTxt);
but it also causes access violation.

Any info what am I doing wrong here?

Timo
Nov 14 '05 #1
3 5049
nrk
Timo wrote:
I am trying to pass a pointer to an array of structures to function,
but
I have some problems. I am using MS Visual C++ 6.0, but I think
this is more of a C-problem than Windows programming specific. Here
is the relevant part of my code.

typedef struct
{
int iControlID;
char controlTxt[64];
} CONTROL_TXT;

void SetControlTxts(HWND hDlg, CONTROL_TXT *ctrlTxt[]);
The second parameter here is a pointer-to-pointer-to CONTROL_TXT. IOW, this
prototype is equivalent to:
void SetControlTxts(HWND hDlg, CONTROL_TXT **ctrlTxt);

However, what you're passing is essentially a pointer-to CONTROL_TXT (Search
the group archives for Chris Torek's "The" rule to see why this is the
case). So what you really want is either one of:
void SetControlTxts(HWND hDlg, CONTROL_TXT *ctrlTxt);
or
void SetControlTxts(HWND hDlg, CONTROL_TXT ctrlTxt[]);

void MyFunc(void)
{
static CONTROL_TXT ctrlTxts[] =
{
{ 1, "text 1" },
{ 2, "text 2" },
{ 3, "text 3" },
{ 0, "" } // marks end of table
};

This declares ctrlTxts to be an array of CONTROL_TXT. When passed as a
parameter to a function, it decays into a pointer to the first element of
the array.
<clip>

SetControlTxts(hDlg, &ctrlTxts[0]);
This is unnecessary. You can simply write:
SetControlTxts(hDlg, ctrlTxts);
to get the same effect.
}
void SetControlTxts(HWND hDlg, CONTROL_TXT *ctrlTxt[])
Again, change this definition to match the prototype as shown above.
{
int i, id;
char txt[64];

for (i = 0; &ctrlTxt[i]->iControlID != 0; i++)
-> binds closer than &. So what you're doing is equivalent to:
&(ctrlTxt[i]->iControlID) != 0

Unfortunately, your choice of integer constant here is such that, the
compiler thinks this is fine, even though this is quite clearly not what
you want.

Why not:
for ( i = 0; ctrlTxt[i].iControlID != 0; i++ )
?
Similar fixes can be applied to other such mistakes.
{
id = &ctrlTxt[i]->iControlID; See above. However, here your compiler has a legitimate reason to complain.
The rhs is clearly an address and lhs is an int. Hence the noise.
strcpy(txt, &ctrlTxt[i]->controlTxt); See above.
//strcpy(txt, (char*)ctrlTxt[i]->controlTxt);
//SetDlgItemText(hDlg, ctrlTxt[i]->iControlID,
ctrlTxt[i]->controlTxt);
SetDlgItemText(hDlg, id, txt);
}

return;

}

I get some compiler warnings. Function call "SetControlTxts(hDlg,
&ctrlTxts[0]);
":
warning C4047: 'function' : 'struct CONTROL_TXT ** ' differs in levels
of indirection from 'struct CONTROL_TXT *'
warning C4024: 'SetControlTxts' : different types for formal and
actual parameter 2

"id = &ctrlTxt[i]->iControlID;":
warning C4047: '=' : 'int ' differs in levels of indirection from 'int
*'

"strcpy(txt, &ctrlTxt[i]->controlTxt);":
warning C4047: 'function' : 'const char *' differs in levels of
indirection from 'char (*)[64]'
warning C4024: 'strcpy' : different types for formal and actual
parameter 2

All fine and excellent warnings. It's good that you've set your compiler's
warning levels high enough to catch these mistakes.
Strcpy also causes access violation in Visual C++. However, I can get
valid value of "id", the debugger shows it correctly.

I also tried casting to char*:
strcpy(txt, (char*)&ctrlTxt[i]->controlTxt);
but it also causes access violation.

This tells you that you are not yet strong enough in the language to be
using dubious constructs such as casts. Never ever use a cast to silence
the compiler unless you're absolutely 200% certain that you know what
you're doing. Casts do *not* fix incorrect code, and often prevent you
from getting useful diagnostics from the compiler.

-nrk.
Any info what am I doing wrong here?

Timo


--
Remove devnull for email
Nov 14 '05 #2
Timo wrote:
a pointer to an array of structures CONTROL_TXT *ctrlTxt[]


That's an array of pointers to structures.

You want:
CONTROL_TXT *ctrlTxt

Make the call like this:
SetControlTxts(hDlg, ctrlTxts);

Access members like this:
ctrlTxt[i].iControlID

--
pete
Nov 14 '05 #3
pete wrote:
.... snip ...
Whenever you pass *any* array name as an argument, the name of
the array is converted to a pointer to it's first element.

I prefer
void func(int *array);
over
void func(int array[]);

Even though they mean exactly the same thing,
the true type of the parameter, is a pointer.


However func may then be called with a pointer to an array[1] of
int, or with a void*, or with the address of some individual int.
So it makes more sense to make the requisite parameter form
explicit. Thus I would use:

void func(int *i); /* pointer to an individual integer */
void func(int a[]); /* pointer to an actual array, but .. */
void func(int a[], size_t sz); /* an actual array, safer */

of course, arrays may have internal end markers, such as strings,
but such markers do not indicate maximum capacity.

The point of the above is that the user of func has an idea of its
requirements from the prototype alone.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Frank Münnich | last post by:
Hi there.. My name is Frank Münnich. I've got a question about pointers that refer to an array of a structure. How do I declare that type? If I have declared a structure struct mystruc {...
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
8
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
15
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h>...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
7
by: Neil | last post by:
What I am doing wrong This works batPointer = adaptors.adaptor->batData; adaptors.batteries = batPointer->battery; where: batData is a pointer to a struct batPointer is a pointer to a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.