473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Passing members of structs as pointers

Sample code:-

typedef struct __Y
{
int a;
char b[5];
} Y;

Y my_struct;
Y *my_ptr_struct;

void my_func (char *str);

main
{
my_ptr_struct = &my_struct;

my_func(&my_struct.b);
my_func(&my_ptr_struct->b);
}

Both func calls gave me compiler warnings of pointer mismatch. So I
changed to (char *) &my_struct.b and (char *) &my_ptr_struct.b. Is
this correct passing of pointers to members?

Thank-you.
Jun 22 '07 #1
9 1521
John Paul II (no******@the.vatican) said:
Sample code:-

typedef struct __Y
{
int a;
char b[5];
} Y;

Y my_struct;
Y *my_ptr_struct;

void my_func (char *str);

main
{
my_ptr_struct = &my_struct;

my_func(&my_struct.b);
Lose the ampersand. &my_struct.b has type char (*)[5], whereas you want
char *, which you can get with my_struct.b, because of The Rule (i.e.
the Standard's guarantee that A[i] and *(A + I) are synonymous).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 22 '07 #2
John Paul II (no******@the.vatican) wrote:
typedef struct __Y
That is an identifier which you are not allowed to use. Identifiers
starting with __, or with _ and a capital letter, are reserved for the
implementation; and under many circumstances, ones starting with _ and
anything else as well. Besides, you don't even need the tag here if
you're going to refer to it by a typedef name anyway. Dike it out.
{
int a;
char b[5];
} Y;

Y my_struct;
Y *my_ptr_struct;

void my_func (char *str);

main
This should not have compiled. Please copy & paste, don't re-type, code
you are asking about. That way you won't introduce typos.
{
my_ptr_struct = &my_struct;

my_func(&my_struct.b);
my_func(&my_ptr_struct->b);
}

Both func calls gave me compiler warnings of pointer mismatch. So I
changed to (char *) &my_struct.b and (char *) &my_ptr_struct.b. Is
this correct passing of pointers to members?
No. In fact, your question has nothing to do with struct members as
such. The problem lies with getting the address to an array, regardless
of whether it is a struct member. Try, for example, this code:

void my_func (char *str);

int main(void)
{
char my_array[5];

my_func(&my_array);
}

You should get a very similar warning.

Casting the pointer to char * is the wrong solution. In fact, casting
away warnings is very nearly always the wrong solution. In this case,
the right solution is to pass the right address: a pointer to the first
member of mystruct.b, rather than to the entire array. So you want:

my_func(&(*my_struct.b));

or, equivalently,

my_func(&(my_ptr_struct->b[0]));
Richard
Jun 22 '07 #3

"John Paul II" <no******@the.vaticanha scritto nel messaggio news:7q********************************@4ax.com...
Sample code:-

typedef struct __Y
{
int a;
char b[5];
} Y;

Y my_struct;
Y *my_ptr_struct;

void my_func (char *str);

main
{
my_ptr_struct = &my_struct;

my_func(&my_struct.b);
my_func(&my_ptr_struct->b);
}

Both func calls gave me compiler warnings of pointer mismatch. So I
changed to (char *) &my_struct.b and (char *) &my_ptr_struct.b. Is
this correct passing of pointers to members?
That will happen to work, here, but is a bad idea.
Use my_func(my_struct.b), and my_struct.b will decay into
&mystruct.b[0]. Reading the www.c-faq.com section about arrays and
pointers will help.
Jun 22 '07 #4

"Richard Bos" <rl*@hoekstra-uitgeverij.nlha scritto nel messaggio news:46***************@news.xs4all.nl...
John Paul II (no******@the.vatican) wrote:
>typedef struct __Y

That is an identifier which you are not allowed to use. Identifiers
starting with __, or with _ and a capital letter, are reserved for the
implementation; and under many circumstances, ones starting with _ and
anything else as well. Besides, you don't even need the tag here if
you're going to refer to it by a typedef name anyway. Dike it out.
>{
int a;
char b[5];
} Y;

Y my_struct;
Y *my_ptr_struct;

void my_func (char *str);

main

This should not have compiled. Please copy & paste, don't re-type, code
you are asking about. That way you won't introduce typos.
>{
my_ptr_struct = &my_struct;

my_func(&my_struct.b);
my_func(&my_ptr_struct->b);
}

Both func calls gave me compiler warnings of pointer mismatch. So I
changed to (char *) &my_struct.b and (char *) &my_ptr_struct.b. Is
this correct passing of pointers to members?

No. In fact, your question has nothing to do with struct members as
such. The problem lies with getting the address to an array, regardless
of whether it is a struct member. Try, for example, this code:

void my_func (char *str);

int main(void)
{
char my_array[5];

my_func(&my_array);
}

You should get a very similar warning.

Casting the pointer to char * is the wrong solution. In fact, casting
away warnings is very nearly always the wrong solution. In this case,
the right solution is to pass the right address: a pointer to the first
member of mystruct.b, rather than to the entire array. So you want:

my_func(&(*my_struct.b));

or, equivalently,

my_func(&(my_ptr_struct->b[0]));
or equivalently,
my_func(my_struct.b);
or equivalently,
my_func(my_ptr_struct.b);
Jun 22 '07 #5
"Army1987" <pl********@for.itwrites:
"Richard Bos" <rl*@hoekstra-uitgeverij.nlha scritto nel messaggio news:46***************@news.xs4all.nl...
[...]
>>So you want:

my_func(&(*my_struct.b));

or, equivalently,

my_func(&(my_ptr_struct->b[0]));

or equivalently,
my_func(my_struct.b);
or equivalently,
my_func(my_ptr_struct.b);
That last one should be

my_func(my_ptr_struct->b);

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 22 '07 #6

"Keith Thompson" <ks***@mib.orgha scritto nel messaggio news:ln************@nuthaus.mib.org...
"Army1987" <pl********@for.itwrites:
>"Richard Bos" <rl*@hoekstra-uitgeverij.nlha scritto nel messaggio news:46***************@news.xs4all.nl...
[...]
>>>So you want:

my_func(&(*my_struct.b));

or, equivalently,

my_func(&(my_ptr_struct->b[0]));

or equivalently,
my_func(my_struct.b);
or equivalently,
my_func(my_ptr_struct.b);

That last one should be

my_func(my_ptr_struct->b);
Yes.
Jun 22 '07 #7
To those who replied.

Yes, I wasn't seeing that the members of a struct are dereferenced by
-therefore they are pointers to the first character in the array.

Thank-you for setting me straight!
Jun 22 '07 #8
John Paul II no******@the.vatican said:
To those who replied.

Yes, I wasn't seeing that the members of a struct are dereferenced by
->
No, they aren't. -dereferences a struct pointer, not the members of a
struct. P->M and (*P).M are synonymous.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 22 '07 #9
On Fri, 22 Jun 2007 14:08:01 +0200, "Army1987" <pl********@for.it>
wrote:
>my_func(my_struct.b);
or equivalently,
my_func(my_ptr_struct.b);
Obviously you meant my_ptr_struct->b
Remove del for email
Jun 22 '07 #10

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

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
3
by: sd2004 | last post by:
I am still learning, could someone show/explain to me how to fix the error. I can see it is being wrong but do not know how to fix. could you also recommend a book that I can ref. to ?...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
11
by: edson | last post by:
Greetings For certain operations I would like to have easy access to struct members. Here is an example. struct mystruct { char member1; char member2; char member3; };
14
by: Kerem Gümrükcü | last post by:
Hi, i want to emulate a (synchronous) BroadCastSystemMessage with EnumWindows and SendMessage. I dont want to use the BroadcastSystemMessage because it needs the SE_TCB_NAME Privilege (you...
0
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.