0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
user786
  • 3,902
  • 4
  • 40
  • 72
  • 1
    Each datatype can hold a certain number of bytes. In the case the data assigned to it is larger than what fits into those bytes an overflow can occur. But for the most part pointers are the same size but the type is useful when dereferencing or pointer arithmetic for example `char* c ; char c[1]` is one byte from the base of the pointer but `int* i ; i[1]` is four bytes from the base of the pointer. – Irelia Aug 12 '21 at 03:43
  • I am asking c++ version of memcmp memset memcpy header files names. – user786 Aug 12 '21 at 03:50
  • 2
    All the basic types are the same in C and C++. – Barmar Aug 12 '21 at 04:15
  • 1
    The header file you want is `` – jkb Aug 12 '21 at 04:19

1 Answers1

2

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);
}
eerorika
  • 232,697
  • 12
  • 197
  • 326