Thread: Help: Convert C to C++

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    16

    Help: Convert C to C++

    Hello,
    I'm trying to convert a program from C to C++.
    Code:
     
    #define RANDSIZL   (4)  /* I recommend 8 for crypto, 4 for simulations */
    #define RANDSIZ    (1<<RANDSIZL)
    
    /* context of random number generator */
    struct randctx{
      int randcnt;
      int randrsl[RANDSIZ];
      int randmem[RANDSIZ];
      int randa;
      int randb;
      int randc;
    };
    
    typedef  struct randctx  randctx;
    The typedef line doesn't make sense to me. A new name (randctx) for struct randctx? If I delete the typedef line it should still compile, but it doesn't. Any idea why? What's the point of having such a typedef line?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A new name (randctx) for struct randctx?
    Yes.

    If I delete the typedef line it should still compile, but it doesn't.
    Did you compile with a C++ compiler?

    Any idea why? What's the point of having such a typedef line?
    So that you can use the name randctx instead of struct randctx, but obviously this does not apply in C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    16
    The code I posted compiles both wit h gcc and g++. The program that I'm converting won't compile (gcc) if I remove the typedef line.

    What's the easiest way to convert C to C++ if you don't know C?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Compile the program as it is with a C++ compiler, and fix the errors.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It should work. Otherwise, what's the error?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    16
    I can't post the error cause it's too big. The program that I'm trying to convert to c++ is called Flam3.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Post a few compile errors first. Fix them. Go on to the next.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Arlenik View Post
    Code:
     
    /* context of random number generator */
    struct randctx{
      int randcnt;
      int randrsl[RANDSIZ];
      int randmem[RANDSIZ];
      int randa;
      int randb;
      int randc;
    };
    
    typedef  struct randctx  randctx;
    The typedef line doesn't make sense to me. A new name (randctx) for struct randctx? If I delete the typedef line it should still compile, but it doesn't. Any idea why? What's the point of having such a typedef line?
    In C, the declaration of struct randctx declares a struct tag named "randctx" and (effectively) a type named "struct randctx". The typedef makes randctx an alternative name for the type "struct randctx".

    In C++, the declaration of struct randctx implicitly also declares a type named randctx, so the typedef is not required.

    Hence you can expect the above to compile happily with a C compiler, but not with a C++ compiler (with some ambiguity in that statement depending on compatibility settings supported by some compilers).

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Compiles just fine even in pedantic mode. C++ was made with C compatibility in mind, and therefore has no problem with a typedef overriding the struct name.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Convert to title case
    By gertie in forum C Programming
    Replies: 18
    Last Post: 07-06-2008, 10:58 PM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM