C++ functions vs. C functions

This is a discussion on C++ functions vs. C functions within the C++ Programming forums, part of the General Programming Boards category; Is there an equivalent C++ function for every C function? Or sometimes we should use C function in C++ code? ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749

    C++ functions vs. C functions

    Is there an equivalent C++ function for every C function?

    Or sometimes we should use C function in C++ code?

    For example memset() is a C function but it seems that it much simple to set charset[]
    to '0' with it.

    Code:
    char charset[256];
    memset(charset,0,sizeof(charset));
    Compiler MSVC++ 2010 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    There's no "one to one" mapping, but there are C++ ways of doing things that are different than the C way of doing things things. In C, you represent character strings with char arrays that are terminated by the NUL character ('\0').

    In C++, you use std::string.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    Or it seems entirely possible that that char array was never intended to hold a nul-terminated string. In that case, std::fill is the C++ equivalent.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Using C code in C++ programs has been done - there was an example on her a few days ago, actually. It is possible, but probably more for backwards compatibility than for enhancing functionality. Sometimes there are things that are easier in one language than the other, but as mentioned above, there's the C++ way of doing things, and then there's the C way.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    An even simpler way to initialize the array with zero's is this:
    Code:
    char charset[256] = {0};
    As others have said, there's no 1 to 1 mapping of C to C++ functions. I think most C functions should have some type of C++ equivalent, although depending on what you are doing, it might be faster to write using C functions in some places...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, for char arrays GCC has this:

    Code:
      // Specialization: for one-byte types we can use memset.
      inline void
      fill(unsigned char* __first, unsigned char* __last, const unsigned char& __c)
      {
        __glibcxx_requires_valid_range(__first, __last);
        const unsigned char __tmp = __c;
        std::memset(__first, __tmp, __last - __first);
      }
    
      inline void
      fill(signed char* __first, signed char* __last, const signed char& __c)
      {
        __glibcxx_requires_valid_range(__first, __last);
        const signed char __tmp = __c;
        std::memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
      }
    
      inline void
      fill(char* __first, char* __last, const char& __c)
      {
        __glibcxx_requires_valid_range(__first, __last);
        const char __tmp = __c;
        std::memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
      }
    But indeed = {0} is the way to fill an array with all zero values.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    Code:
    char charset[256] = {0};
    Well that just cant be simpler. Great!

    I thought it would only have assigned to the first element.

    Thanks everybody!
    Compiler MSVC++ 2010 with Code::Blocks.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Note that this only works if you want to fill an array or struct with zeros. Recently...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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