NO

Author Topic: struct: int, double  (Read 9051 times)

andres1950

  • Guest
struct: int, double
« 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

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: struct: int, double
« Reply #1 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);
May the source be with you

andres1950

  • Guest
Re: struct: int, double
« Reply #2 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

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: struct: int, double
« Reply #3 on: May 07, 2011, 10:33:48 PM »
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

Online AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: struct: int, double
« Reply #4 on: May 09, 2011, 08:11:28 AM »
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.
best regards
 Alex ;)

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: struct: int, double
« Reply #5 on: May 09, 2011, 04:23:26 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

andres1950

  • Guest
Re: struct: int, double
« Reply #6 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 ! ???

Online AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: struct: int, double
« Reply #7 on: May 20, 2011, 11:03:52 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.
best regards
 Alex ;)

andres1950

  • Guest
Re: struct: int, double
« Reply #8 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

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: struct: int, double
« Reply #9 on: May 21, 2011, 07:41:00 AM »
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

andres1950

  • Guest
Re: struct: int, double
« Reply #10 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

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
May the source be with you

CommonTater

  • Guest
Re: struct: int, double
« Reply #12 on: May 24, 2011, 05:54:33 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.


Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: struct: int, double
« Reply #13 on: May 24, 2011, 07:03:14 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.
Quote
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..
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

andres1950

  • Guest
Re: struct: int, double
« Reply #14 on: May 26, 2011, 04:16:36 PM »
Thanks!  :)