Structures and floats

This is a discussion on Structures and floats within the C Programming forums, part of the General Programming Boards category; I'm using an UNIX os, the schools, and I'm having trouble literally assigning a value to a structure variable. Here's ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Angry Structures and floats

    I'm using an UNIX os, the schools, and I'm having trouble literally assigning a value to a structure variable.

    Here's the simple code...

    #include <stdio.h>

    structure example
    {
    float gpa;
    };

    typedef struct example Example;

    Example number;

    int main()
    {
    number->gpa = 5.65;
    return 0;
    }

    It complies, but when I run it, I get Segmentation fault (core dumped)..

    Why does this happen? Please help.

    Josh

  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Use the dot operator rather than the arrow ->
    Code:
    number.gpa = 5.65;
    The arrow is a shorthand way to derference an object. You did not define a pointer to the structure so you do not require using the arrow.
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Since the structure only has one variable (float gpa) why not just do something like this:
    Code:
    typedef unsigned float GPA; /* its unsigned since a gba isn't negative*/
    GPA grades = 0.01;

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Firstly, I think that except for C++, you must use the "struct" keyword to declare a new one of a certain type. Also you may omit one layer of that declaration:

    typedef struct example
    {
    float gpa;
    };

    struct example number;

    Second you are using pointer notation to access a struct, not a pointer to a struct.

    printf("/"number/" GPA data is %.2f...\n", number.gpa);

    Good Luck!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    Oops... I made a typo in my initial code...

    it was supposed to be Example* example.

    That's why I'm so bothered... because it crashes on a very simple application.

    Josh

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    remembe a pointer points to memory.

    try this

    int main()
    {
    Example ex;
    number = ex;
    number->gpa = var;
    return 0;
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,760
    > it was supposed to be Example* example.
    Whenever you have a pointer variable, it is YOUR responsibility to make sure it points somewhere. Judging from your initial post, this will be a global variable, and thus be initialised to the NULL pointer (this pretty much guarantees a segfault on *ix operating systems).

    &nbsp; Example *number; // your uninitialised pointer
    ....
    &nbsp; Example foo;
    &nbsp; number = &foo; // point at variable of the same type
    or
    &nbsp; number = malloc( sizeof(Example) ); // dynamically allocate

    Then you can do number->gpa stuff without problems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc ing array's or structures
    By Markallen85 in forum C Programming
    Replies: 4
    Last Post: 02-03-2003, 01:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21