Search code examples
c++cheader-filesgnu

Are C++ int, int[], float, and especially char *, char[], char are same as these of C. I believe they are premitive so are they same?


There are some datatypes to contain data (in memory I do think). So I like to know these types,

char,char *, char[], -- especially
int, int[], int *, float, decimal, short, long, if there are any other please mention

are same in C and C++.

If they are same then can I use memory functions and string functions and int, float, decimal,binary,etc functions available in C like memset,memcpy,memcmp,etc. and string functions like strcmp, strstr, strlen, etc. in cpp file and complete C++ library or kind of mixed C++ C file which is cpp or cc extension which need to be compiled with g++ or c++ compiler?

Can I assign socket buffer or copy it to C++ class member of same type like C char[] to char some_classobj->charsocket_datal[1024] or do C++ have their own versions of these functions? What's the header files in this case: header file I only like to ask also in this question.


Solution

  • are C++ int, int[], float, and especially char *, char[], char are same as these of C

    Yes.

    can I use memory functions and string functions and int, float, decimal,binary,etc functions available in C like memset,memcpy,memcmp,etc. and string functions like strcmp, strstr, strlen,etc. in cpp file and complete C++ library

    Those C standard library functions are included in the C++ standard library. The header names are changed slightly by adding prefix c and removing suffix .h for example: <string.h> -> <cstring> and the functions are in the namespace std. Note that for many cases for some of those functions there are better alternatives in the C++ standard library, so I recommend studying C++ thoroughly.

    The old C standard header names also exist, but their use is deprecated. The functions may also be declared in the global namespace, but that isn't guaranteed when using the C++ header.

    can I assign socket buffer or copy it to c++ class member of same type like C char[]

    Arrays aren't assignable in C++, just like they aren't in C. You could assign the class itself, just like you could assign a struct in C.

    Here is a good way to copy an array in C++:

    extern int from[42];
    extern int to[42];
    
    void example()
    {
        std::ranges::copy(from, to);
    }