Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #144090 > unrolled thread
| Started by | ab.avramidis@cloudsolutions.co.uk |
|---|---|
| First post | 2019-05-29 02:19 -0700 |
| Last post | 2019-05-29 07:57 -0400 |
| Articles | 14 — 8 participants |
Back to article view | Back to comp.lang.c
Print the values of the members of a structure ab.avramidis@cloudsolutions.co.uk - 2019-05-29 02:19 -0700
Re: Print the values of the members of a structure mark.bluemel@gmail.com - 2019-05-29 02:32 -0700
Re: Print the values of the members of a structure Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-05-29 08:28 -0600
Re: Print the values of the members of a structure ab.avramidis@cloudsolutions.co.uk - 2019-05-29 07:35 -0700
Re: Print the values of the members of a structure Bart <bc@freeuk.com> - 2019-05-29 11:47 +0100
Re: Print the values of the members of a structure Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-05-29 12:16 +0100
Re: Print the values of the members of a structure Bart <bc@freeuk.com> - 2019-05-29 15:45 +0100
Re: Print the values of the members of a structure Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-05-29 08:34 -0600
Re: Print the values of the members of a structure Clifford Heath <no.spam@please.net> - 2019-05-30 11:55 +1000
Re: Print the values of the members of a structure Clifford Heath <no.spam@please.net> - 2019-06-02 22:58 +1000
Re: Print the values of the members of a structure Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-06-02 17:09 +0100
Re: Print the values of the members of a structure Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-06-02 23:05 -0700
Re: Print the values of the members of a structure Clifford Heath <no.spam@please.net> - 2019-06-03 17:38 +1000
Re: Print the values of the members of a structure James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-05-29 07:57 -0400
| From | ab.avramidis@cloudsolutions.co.uk |
|---|---|
| Date | 2019-05-29 02:19 -0700 |
| Subject | Print the values of the members of a structure |
| Message-ID | <46591b57-d339-492b-bd14-28d5cf0a6448@googlegroups.com> |
Hi,
Lets assume that we have the following part of code:
typedef struct data {
int age;
char name;
} MyData;
int main() {
MyData a, *b;
a.age = 34;
strcpy (a.name, "ab");
//printf("%d", a.age);
//printf("%s", a.name);
}
I was wondering If there is any way to print those values (34,"ab") without calling one by one the members of the structure but by using either the definer a or pointer b?
Meaning something like that: printf("%s, %d", *b); Or any other possible way to achieve that.
Thanks
--
*CloudManager - User Lifecycle Management for G Suite*
Find out more at
*https://cloudm.co/cloudmanager <https://cloudm.co/cloudmanager>*
This
email is confidential and may contain privileged material. If you are not
the intended recipient then you must not copy it, forward it, use it for
any purpose, or disclose it to another person. Instead please return it to
the sender immediately. Please then delete your copy from your system and
confirm that you have deleted the relevant material.
[toc] | [next] | [standalone]
| From | mark.bluemel@gmail.com |
|---|---|
| Date | 2019-05-29 02:32 -0700 |
| Message-ID | <81e184c2-f7a4-4820-9adf-b74f6d1071a3@googlegroups.com> |
| In reply to | #144090 |
On Wednesday, 29 May 2019 10:19:35 UTC+1, ab.av...@cloudsolutions.co.uk wrote:
> Hi,
>
> Lets assume that we have the following part of code:
>
> typedef struct data {
> int age;
> char name;
> } MyData;
>
> int main() {
>
> MyData a, *b;
>
> a.age = 34;
> strcpy (a.name, "ab");
Have you even tried compiling this?
> //printf("%d", a.age);
> //printf("%s", a.name);
>
> }
>
> I was wondering If there is any way to print those values (34,"ab")
> without calling one by one the members of the structure but by using
> either the definer a or pointer b?
No.
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2019-05-29 08:28 -0600 |
| Message-ID | <1bwoi9z65e.fsf@pfeifferfamily.net> |
| In reply to | #144091 |
mark.bluemel@gmail.com writes:
> On Wednesday, 29 May 2019 10:19:35 UTC+1, ab.av...@cloudsolutions.co.uk wrote:
>> Hi,
>>
>> Lets assume that we have the following part of code:
>>
>> typedef struct data {
>> int age;
>> char name;
>> } MyData;
>>
>> int main() {
>>
>> MyData a, *b;
>>
>> a.age = 34;
>> strcpy (a.name, "ab");
>
> Have you even tried compiling this?
I don't think he was asking for help debugging that bit of code, he was
using it as an example for his question. His point was perfectly clear
whether the code compiled or not.
>> //printf("%d", a.age);
>> //printf("%s", a.name);
>>
>> }
>>
>> I was wondering If there is any way to print those values (34,"ab")
>> without calling one by one the members of the structure but by using
>> either the definer a or pointer b?
>
> No.
[toc] | [prev] | [next] | [standalone]
| From | ab.avramidis@cloudsolutions.co.uk |
|---|---|
| Date | 2019-05-29 07:35 -0700 |
| Message-ID | <1e9d31d5-b0f8-43d7-92b6-dc615d7f6aa0@googlegroups.com> |
| In reply to | #144105 |
On Wednesday, 29 May 2019 15:28:54 UTC+1, Joe Pfeiffer wrote:
> mark.bluemel@gmail.com writes:
>
> > On Wednesday, 29 May 2019 10:19:35 UTC+1, ab.av...@cloudsolutions.co.uk wrote:
> >> Hi,
> >>
> >> Lets assume that we have the following part of code:
> >>
> >> typedef struct data {
> >> int age;
> >> char name;
> >> } MyData;
> >>
> >> int main() {
> >>
> >> MyData a, *b;
> >>
> >> a.age = 34;
> >> strcpy (a.name, "ab");
> >
> > Have you even tried compiling this?
>
> I don't think he was asking for help debugging that bit of code, he was
> using it as an example for his question. His point was perfectly clear
> whether the code compiled or not.
>
> >> //printf("%d", a.age);
> >> //printf("%s", a.name);
> >>
> >> }
> >>
> >> I was wondering If there is any way to print those values (34,"ab")
> >> without calling one by one the members of the structure but by using
> >> either the definer a or pointer b?
> >
> > No.
@Joe Pfeiffer you are the only one that feeling me!!
Its just an example to put you on the mood of my question. Not troubleshooting the code but give me an advice or answer.
Anyway thanks for your replies!
--
*CloudManager - User Lifecycle Management for G Suite*
Find out more at
*https://cloudm.co/cloudmanager <https://cloudm.co/cloudmanager>*
This
email is confidential and may contain privileged material. If you are not
the intended recipient then you must not copy it, forward it, use it for
any purpose, or disclose it to another person. Instead please return it to
the sender immediately. Please then delete your copy from your system and
confirm that you have deleted the relevant material.
[toc] | [prev] | [next] | [standalone]
| From | Bart <bc@freeuk.com> |
|---|---|
| Date | 2019-05-29 11:47 +0100 |
| Message-ID | <6rtHE.562383$QT2.130500@fx16.am4> |
| In reply to | #144090 |
On 29/05/2019 11:21, Stefan Ram wrote:
> A Google poster writes:
>> Subject: Print the values of the members of a structure
>
> One cannot directly print structure members automatically in
> standard C. Some debuggers or implementations might provide
> such a feature as an extension.
>
> One can implement such a feature in C by adding run-time
> information about the structs or implementing custom records.
> For example, when one uses the Python run-time, one then can
> automatically print Python records (objects) using C code.
You don't need runtime info, not when the format string is constant and
known to the compiler.
For example:
printf("%S", a);
could be compiled as though the user had written:
printf("%d %s", a.age, a,name);
There will still be plenty of problems: no control over formats; what to
do with unions; how to deal with arrays. But for simple cases like this,
it can handy.
(One old compiler I have, when it targeted C code, would take this input
source:
mydata a
print a
and generate this C:
printf("(%d,%s)",a.age,a.name);
(For unions, it printed all the union fields, so some UB because only
one might be valid. Small standalone arrays are printed, but not arrays
within structs, or arrays of structs.))
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2019-05-29 12:16 +0100 |
| Message-ID | <87a7f5jytk.fsf@bsb.me.uk> |
| In reply to | #144095 |
Bart <bc@freeuk.com> writes:
> On 29/05/2019 11:21, Stefan Ram wrote:
>> A Google poster writes:
>>> Subject: Print the values of the members of a structure
>>
>> One cannot directly print structure members automatically in
>> standard C. Some debuggers or implementations might provide
>> such a feature as an extension.
>>
>> One can implement such a feature in C by adding run-time
>> information about the structs or implementing custom records.
>> For example, when one uses the Python run-time, one then can
>> automatically print Python records (objects) using C code.
>
>
> You don't need runtime info, not when the format string is constant
> and known to the compiler.
>
> For example:
>
> printf("%S", a);
>
> could be compiled as though the user had written:
>
> printf("%d %s", a.age, a,name);
>
> There will still be plenty of problems: no control over formats; what
> to do with unions; how to deal with arrays. But for simple cases like
> this, it can handy.
The OP is happy to provide the format, just not the member names. It's
not a massive problem, but a simple solution would be to provide
something like a syntax "macro": __MEMBERS__(a) which expands to the
list of names. Of course it can't be a macro in the usual sense because
of how they are currently specified. Types don't exist when macros are
expanded, much less declarations that link names to types.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Bart <bc@freeuk.com> |
|---|---|
| Date | 2019-05-29 15:45 +0100 |
| Message-ID | <iWwHE.428924$jo3.103224@fx10.am4> |
| In reply to | #144098 |
On 29/05/2019 12:16, Ben Bacarisse wrote:
> Bart <bc@freeuk.com> writes:
>
>> On 29/05/2019 11:21, Stefan Ram wrote:
>>> A Google poster writes:
>>>> Subject: Print the values of the members of a structure
>>>
>>> One cannot directly print structure members automatically in
>>> standard C. Some debuggers or implementations might provide
>>> such a feature as an extension.
>>>
>>> One can implement such a feature in C by adding run-time
>>> information about the structs or implementing custom records.
>>> For example, when one uses the Python run-time, one then can
>>> automatically print Python records (objects) using C code.
>>
>>
>> You don't need runtime info, not when the format string is constant
>> and known to the compiler.
>>
>> For example:
>>
>> printf("%S", a);
>>
>> could be compiled as though the user had written:
>>
>> printf("%d %s", a.age, a,name);
>>
>> There will still be plenty of problems: no control over formats; what
>> to do with unions; how to deal with arrays. But for simple cases like
>> this, it can handy.
>
> The OP is happy to provide the format, just not the member names. It's
> not a massive problem, but a simple solution would be to provide
> something like a syntax "macro": __MEMBERS__(a) which expands to the
> list of names. Of course it can't be a macro in the usual sense because
> of how they are currently specified. Types don't exist when macros are
> expanded, much less declarations that link names to types.
If the correct set of format codes is provided, and it is not otherwise
meaningful to pass a struct to a printf-family function, then the
compiler could generate code to split the struct up into individual members.
(Although it can get tricky when the struct argument is the result of a
complex expression, so that it really requires the struct to be copied
to a temporary.)
Not sure about the problem with using macros, unless you intend this as
a a generic macro that works with any struct type.
But dedicated macros are simple enough, and they seem to work:
#include <stdio.h>
typedef struct {int age; char* name;} MyData;
#define MyData_members(a) a.age, a.name
MyData a = {36,"Homer"};
MyData b = {10,"Bart"};
int main(void) {
printf("%d %s\n",MyData_members(a));
printf("%d %s\n",MyData_members(b));
}
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2019-05-29 08:34 -0600 |
| Message-ID | <1bsgsxz5vm.fsf@pfeifferfamily.net> |
| In reply to | #144095 |
Bart <bc@freeuk.com> writes:
> On 29/05/2019 11:21, Stefan Ram wrote:
>> A Google poster writes:
>>> Subject: Print the values of the members of a structure
>>
>> One cannot directly print structure members automatically in
>> standard C. Some debuggers or implementations might provide
>> such a feature as an extension.
>>
>> One can implement such a feature in C by adding run-time
>> information about the structs or implementing custom records.
>> For example, when one uses the Python run-time, one then can
>> automatically print Python records (objects) using C code.
>
>
> You don't need runtime info, not when the format string is constant
> and known to the compiler.
>
> For example:
>
> printf("%S", a);
>
> could be compiled as though the user had written:
>
> printf("%d %s", a.age, a,name);
>
> There will still be plenty of problems: no control over formats; what
> to do with unions; how to deal with arrays. But for simple cases like
> this, it can handy.
This would actually be a very reasonable thing to add to the language;
the problem with control over the format string could be managed as it
is now (in the case of gcc), with the compiler having knowledge of the
proper format characters to go with types. Unions would indeed be a
problem...
But, back to the OP's question, "no".
[toc] | [prev] | [next] | [standalone]
| From | Clifford Heath <no.spam@please.net> |
|---|---|
| Date | 2019-05-30 11:55 +1000 |
| Message-ID | <PKGHE.15766$9A4.8903@fx02.iad> |
| In reply to | #144107 |
On 30/5/19 12:34 am, Joe Pfeiffer wrote:
> Bart <bc@freeuk.com> writes:
>
>> On 29/05/2019 11:21, Stefan Ram wrote:
>>> A Google poster writes:
>>>> Subject: Print the values of the members of a structure
>>>
>>> One cannot directly print structure members automatically in
>>> standard C. Some debuggers or implementations might provide
>>> such a feature as an extension.
>>>
>>> One can implement such a feature in C by adding run-time
>>> information about the structs or implementing custom records.
>>> For example, when one uses the Python run-time, one then can
>>> automatically print Python records (objects) using C code.
>>
>>
>> You don't need runtime info, not when the format string is constant
>> and known to the compiler.
>>
>> For example:
>>
>> printf("%S", a);
>>
>> could be compiled as though the user had written:
>>
>> printf("%d %s", a.age, a,name);
>>
>> There will still be plenty of problems: no control over formats; what
>> to do with unions; how to deal with arrays. But for simple cases like
>> this, it can handy.
>
> This would actually be a very reasonable thing to add to the language;
> the problem with control over the format string could be managed as it
> is now (in the case of gcc), with the compiler having knowledge of the
> proper format characters to go with types. Unions would indeed be a
> problem...
>
> But, back to the OP's question, "no".
In 2001, I implemented a library called DDT (Dynamic Data Typing), which
did more-or-less this. Essentially it had a set of data types (a
superset of C's, including some things like our shared-ptr Unicode
strings, and GUIDs, etc). For each data type, it had structure packing
(alignment) rules and parameter-passing (stack) alignment rules, which
were populated at compile time. We had a data-type definition language
that supported arbitrary-depth arrays and structures, which generated
into three main artefacts: C structure definitions, function prototypes,
and a compact "signature" (dynamic data type description) for each
compound type. The signatures were generated as string values in a
#define, one generated include file from each data-type source file.
The support library could take a va_alist and the signature, or a
pointer to a struct and the signature, and could transcode the data into
various forms - including calling an arbitrary function with parameters
unpacked from a structure. We also had transcoders for network transport
(byte ordering and packing rules), so you could trivially write client
and server API stubs for RPC or asynchronous messaging.
We also had a "printf"-style function, but where the required parameters
are translated to a type signature, and the parameters unpacked from the
stack as described previously. Further, we supported positional
parameters (e.g. %1$s) so the format string could be re-ordered without
having to re-order the function parameters. This was useful when the
format string was drawn from an I18N message catalog.
Finally, for use in C++, we had a type-safe parameter-list structure
called ArgList, so you could say DisplayMessage(MSG_SOME_NAME, Arglist
<< name_var << 23); (for example) and get type-safe varargs
parameter-list unpacking and message formatting. Run-time checking, of
course, but safe.
All up, it's basically nothing you couldn't do with run-time type
reflection. But it was extraordinarily effective. The shipped products
totaled 3 million lines of code, and is running critical system
functions in tens of millions of enterprise computers still today; and
the features I've described radically improved the code safety and
system reliability (type-safe internationalizable printf, woohoo!).
These days, it's possible to do much of this using ASN.1 tools, and
that's probably where I'd start if I did it again.
Clifford Heath.
[toc] | [prev] | [next] | [standalone]
| From | Clifford Heath <no.spam@please.net> |
|---|---|
| Date | 2019-06-02 22:58 +1000 |
| Message-ID | <GJPIE.36175$xY3.32737@fx01.iad> |
| In reply to | #144129 |
On 30/5/19 11:55 am, Clifford Heath wrote:
> On 30/5/19 12:34 am, Joe Pfeiffer wrote:
>> Bart <bc@freeuk.com> writes:
>>
>>> On 29/05/2019 11:21, Stefan Ram wrote:
>>>> A Google poster writes:
>>>>> Subject: Print the values of the members of a structure
>>>>
>>>> One cannot directly print structure members automatically in
>>>> standard C. Some debuggers or implementations might provide
>>>> such a feature as an extension.
>>>>
>>>> One can implement such a feature in C by adding run-time
>>>> information about the structs or implementing custom records.
>>>> For example, when one uses the Python run-time, one then can
>>>> automatically print Python records (objects) using C code.
>>>
>>>
>>> You don't need runtime info, not when the format string is constant
>>> and known to the compiler.
>>>
>>> For example:
>>>
>>> printf("%S", a);
>>>
>>> could be compiled as though the user had written:
>>>
>>> printf("%d %s", a.age, a,name);
>>>
>>> There will still be plenty of problems: no control over formats; what
>>> to do with unions; how to deal with arrays. But for simple cases like
>>> this, it can handy.
>>
>> This would actually be a very reasonable thing to add to the language;
>> the problem with control over the format string could be managed as it
>> is now (in the case of gcc), with the compiler having knowledge of the
>> proper format characters to go with types. Unions would indeed be a
>> problem...
>>
>> But, back to the OP's question, "no".
>
> In 2001, I implemented a library called DDT (Dynamic Data Typing), which
> did more-or-less this. Essentially it had a set of data types (a
> superset of C's, including some things like our shared-ptr Unicode
> strings, and GUIDs, etc). For each data type, it had structure packing
> (alignment) rules and parameter-passing (stack) alignment rules, which
> were populated at compile time. We had a data-type definition language
> that supported arbitrary-depth arrays and structures, which generated
> into three main artefacts: C structure definitions, function prototypes,
> and a compact "signature" (dynamic data type description) for each
> compound type. The signatures were generated as string values in a
> #define, one generated include file from each data-type source file.
>
> The support library could take a va_alist and the signature, or a
> pointer to a struct and the signature, and could transcode the data into
> various forms - including calling an arbitrary function with parameters
> unpacked from a structure. We also had transcoders for network transport
> (byte ordering and packing rules), so you could trivially write client
> and server API stubs for RPC or asynchronous messaging.
>
> We also had a "printf"-style function, but where the required parameters
> are translated to a type signature, and the parameters unpacked from the
> stack as described previously. Further, we supported positional
> parameters (e.g. %1$s) so the format string could be re-ordered without
> having to re-order the function parameters. This was useful when the
> format string was drawn from an I18N message catalog.
>
> Finally, for use in C++, we had a type-safe parameter-list structure
> called ArgList, so you could say DisplayMessage(MSG_SOME_NAME, Arglist
> << name_var << 23); (for example) and get type-safe varargs
> parameter-list unpacking and message formatting. Run-time checking, of
> course, but safe.
>
> All up, it's basically nothing you couldn't do with run-time type
> reflection. But it was extraordinarily effective. The shipped products
> totaled 3 million lines of code, and is running critical system
> functions in tens of millions of enterprise computers still today; and
> the features I've described radically improved the code safety and
> system reliability (type-safe internationalizable printf, woohoo!).
>
> These days, it's possible to do much of this using ASN.1 tools, and
> that's probably where I'd start if I did it again.
>
> Clifford Heath.
... Crickets.
Was this story useful to anyone?
Clifford Heath.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2019-06-02 17:09 +0100 |
| Message-ID | <8736ks55r5.fsf@bsb.me.uk> |
| In reply to | #144287 |
Clifford Heath <no.spam@please.net> writes: > On 30/5/19 11:55 am, Clifford Heath wrote: <cut description of a solution> > > ... Crickets. > > Was this story useful to anyone? Well I was doing the same sort of thing, so it was not really news to me, and it's possible that the description was broad-brush enough that no one felt the need to comment. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Date | 2019-06-02 23:05 -0700 |
| Message-ID | <867ea3gq5o.fsf@mailhub.linuxsc.com> |
| In reply to | #144287 |
Clifford Heath <no.spam@please.net> writes: >> [superstructure for providing dynamic type information] > > ... Crickets. > > Was this story useful to anyone? I found it interesting. I suspect this is one of those things that people usually don't really appreciate right away, but after using for a while would never want to give it up. IME it's very difficult to convey the value of such things just by explaining them. It sounds like what you did would have a lot of value, based on your retrospective. I didn't have much reaction beyond that so I didn't say anything.
[toc] | [prev] | [next] | [standalone]
| From | Clifford Heath <no.spam@please.net> |
|---|---|
| Date | 2019-06-03 17:38 +1000 |
| Message-ID | <F74JE.58905$4O5.22943@fx46.iad> |
| In reply to | #144320 |
On 3/6/19 4:05 pm, Tim Rentsch wrote:
> Clifford Heath <no.spam@please.net> writes:
>
>>> [superstructure for providing dynamic type information]
>>
>> ... Crickets.
>>
>> Was this story useful to anyone?
>
> I found it interesting. I suspect this is one of those things
> that people usually don't really appreciate right away, but
> after using for a while would never want to give it up. IME
> it's very difficult to convey the value of such things just
> by explaining them. It sounds like what you did would have a
> lot of value, based on your retrospective. I didn't have much
> reaction beyond that so I didn't say anything.
Interesting, thanks for your feedback, and you're probably right.
There was quite a lot more to this software infrastructure, but I didn't
want to rant on forever... I contemplated writing a book on this subject
("Aspects of Software Library Infrastructure"), from the perspective of
"what is the nature of each part of the problem, and what kinds of
characteristics is it reasonable to expect from a software library
solution". Then perhaps to review some popular frameworks against those
expectations...
Maybe I should still write that book one day. It's just daunting
thinking of the evaluation of existing framework libraries... a massive
task.
Clifford Heath.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Date | 2019-05-29 07:57 -0400 |
| Message-ID | <qcls3d$ki2$1@dont-email.me> |
| In reply to | #144090 |
On 5/29/19 5:19 AM, ab.avramidis@cloudsolutions.co.uk wrote:
> Hi,
>
> Lets assume that we have the following part of code:
>
> typedef struct data {
> int age;
> char name;
> } MyData;
>
> int main() {
>
> MyData a, *b;
>
> a.age = 34;
> strcpy (a.name, "ab");
That's a constraint violation. a.name has the type "char". The first
argument of strcpy is supposed to be a pointer to char. You could meet
that requirement by writing &a.name, but that's not the right way to fix
this problem.
"ab" is a string literal - its presence in your code causes the creation
of an unnamed array of 3 char, initialized with {'a', 'b', '\0'}. The
terminating '\0' is automatically added to make sure that the array
contains a complete null-terminated string. This string literal has a
value which is of type char[3]. In most cases, lvalues of array type
such as this get implicitly converted into a value which is a pointer to
the first element of that array.
This is a convenient feature of C, but one which confuses newbies for a
while, leading them to be confused about the difference between pointers
and arrays. It has never stopped confusing Bart.
strcpy() starts reading from that array, one character at a time, until
it reaches the terminating null character, and attempts to write all
three of those characters in consecutive positions in memory, starting
at the place pointed at by it's first argument. Therefore, you need to
set aside enough space to store at least 3 chars. a.name has only enough
space for a single char. At a minimum, you should change the declaration
of a.name to
char name[3];
You could still write
strcpy(a.name, "ab");
that's because a.name is also an lvalue of array type, and therefore
gets implicitly converted to a pointer to the first element of that
array, which is precisely what you need to pass to strcpy().
Given the context, I suspect you should set aside more space for the
name, at least "char name[20];". And if you do decide to use a fixed
size, always check that what you're copying into that array will fit in
the amount of space you've provided (including the terminating null
character).
There are other, more complicated ways of dealing with this that would
allow for names that are arbitrarily long, and which use up less memory
for short names than for long ones. However, I doubt that you're ready
for such complications yet.
> //printf("%d", a.age);
> //printf("%s", a.name);
>
> }
>
> I was wondering If there is any way to print those values (34,"ab") without calling one by one the members of the structure but by using either the definer a or pointer b?
>
> Meaning something like that: printf("%s, %d", *b); Or any other possible way to achieve that.
No. the only thing that printf() knows about the second and later
arguments that you give it is what you've told it about those arguments
in the format string which is passed as its first argument. printf()
only recognizes a small set of format specifiers, none of which do
anything as complicated as telling it the layout of a struct type. The
simplest way to do this in C is:
printf("%s, %d", a.name, a.age);
Consider that, if printf() did have a format specifier capable of
telling it the layout of your struct, then the format string which did
so would have to be significantly more complicated than "%s, %d".
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web