Pelles C forum

C language => Beginner questions => Topic started by: andres1950 on May 07, 2011, 11:01:04 AM

Title: struct: int, double
Post by: andres1950 on May 07, 2011, 11:01:04 AM
Greeting!

I have code:

#include <stdio.h>

struct test{

               char *name;
    int  age;}t1;

int main(void){

   
             struct test *pt_test;
             pt_test=&t1;   
   
             pt_test->name="Jooru";   
             pt_test->age=25  ;   

             printf("%s %d\n\n  ",*pt_test  );

return 0;
}

by int age; it's working, but by double age; printf %f givs 0.00   .. why !?

regards

Andres Maevere
Title: Re: struct: int, double
Post by: TimoVJL on May 07, 2011, 11:18:31 AM
You should use 2 parameters:
   printf("%s %d\n\n", pt_test->name, pt_test->age);
or
   printf("%s %f\n\n", pt_test->name, pt_test->age);
Title: Re: struct: int, double
Post by: andres1950 on May 07, 2011, 11:50:58 AM
Greeting and thanks for answer!

Yes, this variant  i have; but why int works with *pt_test and double not!??

regards

Andres Maevere
Title: Re: struct: int, double
Post by: Bitbeisser on May 07, 2011, 10:33:48 PM
Quote from: andres1950 on May 07, 2011, 11:50:58 AM
Greeting and thanks for answer!

Yes, this variant  i have; but why int works with *pt_test and double not!??

regards

Andres Maevere

Doesn't matter, it is just bad programming practice, you just rely on a side effect rather than proper instructions...

Ralf
Title: Re: struct: int, double
Post by: AlexN on May 09, 2011, 08:11:28 AM
Quote from: Bitbeisser on May 07, 2011, 10:33:48 PM
Doesn't matter, it is just bad programming practice, you just rely on a side effect rather than proper instructions...

... and if you really want to know it (for scientific interest) you can look in the disassembler listing of the debugger to look, what the compiler is doing in each case.
Title: Re: struct: int, double
Post by: Bitbeisser on May 09, 2011, 04:23:26 PM
Quote from: AlexN on May 09, 2011, 08:11:28 AM
Quote from: Bitbeisser on May 07, 2011, 10:33:48 PM
Doesn't matter, it is just bad programming practice, you just rely on a side effect rather than proper instructions...

... and if you really want to know it (for scientific interest) you can look in the disassembler listing of the debugger to look, what the compiler is doing in each case.
Well, not sure if that helps the OP in this case. :-\

Basically, he is just putting the address of s struct on the stack but refers to multiple elements in the printf statement. And printf, without checking if the number of parameters matches, is just picking the values to display from the stack or whatever it just happens to find. It will show the string just because its address just happens to match that of the struct but anything after that will be just random...

Ralf
Title: Re: struct: int, double
Post by: andres1950 on May 20, 2011, 01:24:25 AM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined( __GNUC__ ) || defined( _MSC_VER )
#pragma pack(2)             // Byte-aligned: no padding.
    #endif
struct test{   

    char *name;
    double  age;};

int main(void){ 
       
     struct test *pt_test = malloc( sizeof(struct test) );
     struct test *pt_test2 = malloc( sizeof(struct test) );   
         
             pt_test->name="Jooru"; 
             pt_test->age=25599.0  ;
             pt_test2->name="Joosu"; 
             pt_test2->age=35009254788.78  ;       

             printf("%s %.2f %s %.2f\n\n  ", *pt_test, *pt_test2  );

return 0;                                     
}                           /*Output: Jooru 25599.00   Joosu  35009254788.78 */


  So is it working...good or bad practice don't know ! ???
Title: Re: struct: int, double
Post by: AlexN on May 20, 2011, 11:03:52 AM
Quote from: andres1950 on May 20, 2011, 01:24:25 AM
  So is it working...good or bad practice don't know ! ???
The question is, matches your struct to the form like printf() expect the data on the stack. Does it match, it works, does it match it don't work.
This is also reason, why it a bad programming practice, because it fits exactly to one compiler and if you use an other compiler or you update your compiler, it is not sure that your code still works. And this errors are very hard to find.
Title: Re: struct: int, double
Post by: andres1950 on May 20, 2011, 09:17:05 PM
Greeting!

OK, I agree, it's not portable, it works only with Pelles C. But is this bonus or minus for Pelles C !?  By adding to source code  getchar(); exe file is working with winXPpro(32)  fine. ???

best regards

Andres
Title: Re: struct: int, double
Post by: Bitbeisser on May 21, 2011, 07:41:00 AM
Quote from: andres1950 on May 20, 2011, 09:17:05 PM
Greeting!

OK, I agree, it's not portable, it works only with Pelles C. But is this bonus or minus for Pelles C !?
You don't seem to understand the basic issue here that people are trying to make you aware of.

It is NOT "working", it is more or less an "accident" that is makes look like you get the right output.

And that is no excuse for not doing it the proper way. It might not be a big deal with a simple program, but if you have a larger project, this can cost you endless hours of troubleshooting and it can cost you your job if you ever try to do programming for a living...

Ralf
Title: Re: struct: int, double
Post by: andres1950 on May 24, 2011, 02:16:38 PM
Greeting and thanks for answers!

C programming is for me at first hobby..and i don't say that i am right..i only ask why is it so...!??

In structure are all elements side by side, every element has his amount and format,... why is it not possible to point in beginning of structure and by sufficient  memory to read all elements together..

best regards

Andres Maevere
Title: Re: struct: int, double
Post by: TimoVJL on May 24, 2011, 02:46:39 PM
Read these:

http://c-faq.com/struct/align.html (http://c-faq.com/struct/align.html)
http://c-faq.com/struct/align.esr.html (http://c-faq.com/struct/align.esr.html)
http://msdn.microsoft.com/en-us/library/83ythb65(v=VS.71).aspx (http://msdn.microsoft.com/en-us/library/83ythb65(v=VS.71).aspx)
Title: Re: struct: int, double
Post by: CommonTater on May 24, 2011, 05:54:33 PM
Quote from: andres1950 on May 24, 2011, 02:16:38 PM
Greeting and thanks for answers!

C programming is for me at first hobby..and i don't say that i am right..i only ask why is it so...!??

In structure are all elements side by side, every element has his amount and format,... why is it not possible to point in beginning of structure and by sufficient  memory to read all elements together..

best regards

Andres Maevere

You can read/write it as a blob but you will be reading them as raw binary data, not as variables with names that you can access.  In fact this technique is used to save and load data in structs to and from storage devices all the time.

The first version with no malloc worked only because nothing else was accessing whatever random spot in memory the pointer was aimed at.  Simply a fluke...

The second version with pack(2) will work because the compiler supports it and for no other reason.  If it was compiled again elsewhere you could easily end up with two versions of your program that cannot access each other's data files.

Title: Re: struct: int, double
Post by: Bitbeisser on May 24, 2011, 07:03:14 PM
Quote from: andres1950 on May 24, 2011, 02:16:38 PM
Greeting and thanks for answers!

C programming is for me at first hobby..and i don't say that i am right..i only ask why is it so...!??
As I already mentioned, purely by accident...
Even if it is just a for a hobby, it is a well intended advice that you stop being lazy and get used to do things the proper way.
Computer programming is a precise "business", the computer/CPU will do exactly what you tell it to do, which is not necessarily what you intend to do.
QuoteIn structure are all elements side by side, every element has his amount and format,... why is it not possible to point in beginning of structure and by sufficient  memory to read all elements together..
For a program with a few lines, you might get lucky that it works.
But any change to the struct, be it either by different alignment depending on CPU/OS/compiler or by manual changes of order or adding/removing an element of that struct and "all hell can break loose"...

Please, we don't try to pick on you, we just try to make you aware of a very bad practice/habit you seem to have adopted and show you how to do something the right way...

Ralf
Title: Re: struct: int, double
Post by: andres1950 on May 26, 2011, 04:16:36 PM
Thanks!  :)
Title: Re: struct: int, double
Post by: andres1950 on May 26, 2011, 05:55:37 PM
..and it seems, that it wheels while here is not padding...Pelles C supports this, other compilers not..
Title: Re: struct: int, double
Post by: CommonTater on May 26, 2011, 06:32:59 PM
Andres... just an observation if I may...

In my experience people who start out as hobbiests often make the best craftsmen... for example, the guy who's building his own furniture is likely to become a far better cabinet maker.  Not because he has extra practice or knows some big secret... because he cares about the work. 

This also extends in to the programming craft... people who got into it because they actually enjoy programming (like you and I) very often make far better programmers than those who got into it for the money or fell into it with the family business (etc.)

The difference is that we chose this.  Since hobby programming (or knitting, for that matter) carries no time pressure and is, for us, it's own reward, the rewards are clearly bigger when we work hard at getting it right.

Enjoy the hobby my friend... I can think of no better passtime!
Title: Re: struct: int, double
Post by: TimoVJL on May 26, 2011, 06:41:14 PM
Test this packing if you want save it for general use.
#pragma pack(push,1)
struct test{
    char *name;
    double  age;
};
#pragma pack(pop)