Advertisements

It was a good idea that you posted the expected output, because the
program is completely meaningless and we couldn't figure out how to fix
it without this piece of information.
>-- all I get is "$)\n". (I admit I'm an idiot, OK?)

The size of the buffer is incorrectly calculated: you want one byte for
each member of the struct plus one byte for the string terminator.
Assuming a non-perverse implementation, you can obtain the number of
struct members (in this particular case) with the following expression:

sizeof *ms / sizeof ms -> a

or

sizeof(struct mystruct) / sizeof(int)

The former may look more cryptical, but it doesn't require any
maintenance if you decide to change the name of the struct or the type
of its members later.

You got the iteration count horribly wrong: sizeof ms is giving you the
size of a pointer to a structure, which is completely unrelated to the
number of members of the struct. You need exactly the same expression
as above:

This is a horrible mess. You have allocated memory for a single
struct, not for a whole array of them. What you need is to convert ms
to pointer to int and use that pointer for accessing each member of
the struct, as if you had an array of int's:

This still won't print out "$$$$$$$\0\n". First of all '\0' isn't a
printable character, it indicates the end of the string. Second,
the members of your mystruct are all ints, and since the size of an
int is usually larger than the one of a char you will have additional
(zero) bytes in between the '$'s (not even counting possible padding
bytes). The moment the printf() function finds one of the zero bytes
it will stop printing.
> return 0 ;
> }
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| -berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring

sizeof ( ms ) would be the size of a *pointer* to your struct.
to get the size of the actual object, you should use
sizeof (*ms);
> buf = (unsigned char)*((unsigned char *)&ms) ;

ms is *not* an array. it is a pointer to a single struct. I understand
what you are aiming for (i must, after all you posted the expected
output above , but I would recommend that you access each member
of a struct individually. the reason is because the compiler *may*
pad the struct, and accessing the struct as one block of memory is
bound to end in tears sooner or later, as you find 'gaps' in your
block of data would be filled with garbage.
> uld be filled with garbage.
> buf = '\0' ;
> '\0' ;
> printf( "%s\n", buf ) ;

even if your compiler did not pad the structure, this can still
fail if sizeof (int) != sizeof (char), as printf ("%s\n", buf)
needs an array of chars, not an array of integers.
>
> return 0 ;
> }

It would have been less effort on your part
to include error checking originally, than to answer.
That you are personally aware that error checking belongs in the code,
is good, but it is not all that matters.
Your code is out there for all to see,
and it is missing error checking.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!