Thread: coding

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Red face coding

    I CANNOT FIGURE OUT WHATS WRONG WITH THIS CODING, im doing this for class, and it making me crazy, there are too many errors, just beginning to learn this, and its giving me a hard, time, it would be nice, if anyone could tell me whats wrong,
    thank you





    Typedef struct
    {
    int deno;
    int nume;
    }fraction

    fraction fr1;

    fraction *ptr = fr1;
    *ptr.deno = 2;

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Use code tags plz.
    Code:
    struct fraction
    {
    	int deno; 
    	int nume;
    };
    
    int main()
    {
    	fraction fr1;
    	fraction *ptr = &fr1;
    	ptr->deno = 2;
    	return 0;
    }
    Good luck with your studys.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    problem is with . operator

    /*Try this thing*/
    [code:]
    typedef struct
    {
    int deno;
    int nume;
    }fraction;
    main()
    {
    fraction fr1;
    fraction *ptr=&fr1;
    ptr->deno=2;
    /*whenever you use pointer to access use ->.whenever compiler sees -> immediately it takes left operand as pointer*/
    }
    [\code]
    or make change in BlackMagic guy code as
    struct fraction fr1;
    Saravanan.T.S.
    Beginner.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Lightbulb

    First of all, I don't see why you need the typedef struct there at all. And secondly if you want to use a typedef struct, you might want to give it a second argument?

    typedef unsigned int U_INT;
    /*The unsigned int is what your typedef'ing... and now whenever the compiler sees U_INT in your code, it knows your talking about an unsigned int */

    typedef unsigned int U_INT;
    main()
    {
    U_INT x = 0; /*The compiler replaces this with
    unsigned int x = 0; */
    }

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Its [code] and [/code].

    >>main()
    Should be int main( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    when you use a struct without typedef you can declare variables (usually global) outside of main.

    ex:
    Code:
    struct blah
    {
    	int a;
    	int b;
    } std_blah, rem;
    now std_blah and rem are gobal variables of type 'struct blah'

    as with typedef struct...
    Code:
    typedef struct
    {
    	int a;
    	int b;
    } blah;
    your declaring rem as a TYPE of struct.

    ex:
    Code:
    struct first
    {
    	int a;
    };
    
    typedef struct
    {
    	int b;
    } second;
    
    int main()
    {
    	struct first blah1; //initializes first... notice the 'struct' in front
    	second blah2; //initializes second... no need for struct since second is a TYPE rather than just a struct...
    	return 0;
    }
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    main() Is perfectly legal, no need for a return type. Remember that main is just a function, so like any other function, it can have a return type of nothing (void).

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    On some systems the main function returns an exit code

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by khoxxxy
    On some systems the main function returns an exit code
    Like Salem said, we're not going through this again . Read the FAQ and other posts if you're unsure, but please don't start on about it here
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 02:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM