Can't we access float variables in a C structure directly?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • durgavaraprasad
    New Member
    • Mar 2008
    • 2

    Can't we access float variables in a C structure directly?

    Hi I'm prasad.
    I am new to structures in C.
    I declared a structure as follows.
    But it is giving a run-time error that
    "scanf: floating point formats not matched
    Abnormal program termination
    " at the line indicated.
    Some of my friends told me that we can't access floating point variables in structures directly. We need the help of certain functions. Is it true? If so how to access them?

    #include<stdio. h>
    #include<conio. h>
    #include<alloc. h>

    struct struct1
    {
    int eno;
    char ename[20];
    float sal;
    };
    int main()
    {
    struct struct1 *p;
    clrscr();
    p=malloc(sizeof (struct struct1)*1);

    printf("Enter employee number: ");
    scanf("%d",&(*p ).eno);
    printf("Enter employee name: ");
    scanf("%s",(*p) .ename);
    printf("Enter employee salary: ");
    fflush(stdin);
    scanf("%f",&(*p ).sal);
    printf("NUMBER: %d\n",(*p).eno) ;
    printf("NAME: %s\n",(*p).enam e);
    printf("SALARY: %0.1f\n",(*p).s al);

    return 1;
    }

    Please help me. Thanks in advance.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    I changed the memory allocation line like this
    p=(struct struct1*)malloc (sizeof(struct struct1)*1);
    and it worked fine without any issues

    Raghuram

    Comment

    • durgavaraprasad
      New Member
      • Mar 2008
      • 2

      #3
      Hi I have tried that change but it's not working.
      Again the same error of 'floating points not linked'

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by durgavaraprasad
        Hi I have tried that change but it's not working.
        Again the same error of 'floating points not linked'
        i used gcc compiler in cygwin.
        Which compiler u are using?

        Raghuram

        Comment

        • manjuks
          New Member
          • Dec 2007
          • 72

          #5
          Originally posted by gpraghuram
          i used gcc compiler in cygwin.
          Which compiler u are using?

          Raghuram
          I tried in DEV C++ its working fine. Where as in Turbo C its giving that error. I also don't know why that error is coming.

          Comment

          Working...