Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #144099

Re: Print the values of the members of a structure

From James Kuyper <jameskuyper@alumni.caltech.edu>
Newsgroups comp.lang.c
Subject Re: Print the values of the members of a structure
Date 2019-05-29 07:57 -0400
Organization A noiseless patient Spider
Message-ID <qcls3d$ki2$1@dont-email.me> (permalink)
References <46591b57-d339-492b-bd14-28d5cf0a6448@googlegroups.com>

Show all headers | View raw


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".

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web