write in advance
pointers, arrays, structures, unions, enumeration are all compounds types. they are consist of basic built-in types. in this blog, i am going to share some of my records here, hoping to better understand and have a good acknowledge of them.
Pointers
pointer itself is simple. it takes the address as its basic value and values on the address as a derived value. you can have pointers associated with arrays, structures, and strings. you can also see pointers used to hold a newly-allocated memory. you can see function pointers pointed to functions with specific prototype as an argument passed to another functions. pointers are of cruciality and i decide to dive into it in a separate blog. so here i am going to omit it.
Array
an array can hold seceral values, aoo of the same types , in a single data object. By using an index, or subscript, one can accss the individual elements in array.
const int ArrSize = 30;
char name[ArrSize]; // an array
array is easy in this way. The real obstacle of array comes into 2-dimension array or higher dimension array, which needs a related nested loop to handle. In addition to this, one can have an array of pointers to specific types, which can be easily confused with a specifi pointer to an array with specific numbers. Here is the code:
const int ArrSize = 40;
char * ptc[ArrSize]; // an array of 40 char* pointer
char (*prr)[ArrSize]; // prr, a pointer points to an array of 40 char
String
The string i am talking about here is the C-Style string, excluding the C++ string class, which i may take a separate blog to talk about. one thins to notice is that srings itself, at least in the opinion of C, is a special array whose elements are all type of char and end with null character('\0'). So, if u already know some string-related functions like strcmp(). strlen(), and strcat(), u would find that all they need is the start of the string, or in another word, the address of the string. If a string is hold in a char array, then the array name also acts as the address; if it is pointed to by a pointer, then u can pass the pointer as the argument these funcitons need.
const int Arr = 30;
char name[Arr] = {"hahaha"};
std::strcat(name, "lalala");
std::strlen(name);
std::cout << name << std::endl; // hahahalalala
char* ptc = new char [std::strlen(name) + 1];
std::strcpy(ptc, name);
std::cout << ptc; //hahahalalala
here, as u see, i pass the array name or the pointer as the argument to those functions to test them. u can put the code snippet provided here in your main() function , remember to #include<iostream>, and remember that funcitons above are placed in std namespace, that is the reason why i put std:: before them.
i would like to share a variant functions of strcpy(). the strncpy(). when i first use it ,i met some problems, it is a good time to record it.
strncpy(char* target, const char* source, int maxlength);
// the funciton copy the stirng in the source address to the address of target
// with maximun number of characters to be copied
if the function run outs of space before it reaches the end of the string, it doesn't add the null character.
char name[5];
strncpy(name,"i am C++",4); // it copy no more than 4 characters
name[4] = '\0';
however, if it works as expected, it would add padding '\0' until it fills the array, which is what we expected.
char love[6];
strncpy(love,"C++",6);
// in array love, it is C++\0\0\0
so when using the strncpy(), take care with the length and remember to set the last char '\0' whenever necessary.
Structure
a structure can hold several values of different types in a single data object, and one can use the membership operator to access individual members. If u assign a structue to a pointer pointed to that type, u can use the operator -> to access the structure variable. Here is the example:
struct stu
{
const char* name;
int id;
}
stu one { "jordan", 23 } ; //initializaiton
one.name = "kobe"; // assigement,
one.id = 24; // struct variable use member operator to access variables in structure
stu* pstu = one; // initialization
std::cout << one-> name << " " << one->id; // pointers use -> to access variables
First, one need to create a structure template that defines what members the structure holds. The name, or tags, then become a new type identifier, which can be used to declare structure variables of that type.
since structure has a lot in common with class, (class is models by structure at first), here, i am not going to record too much about structure, because it is much more simple than class and i have a basic knowledge of class. With the sturdy with class, u would find that structure is a piece of case. So, here, all u need to know, is what i have written above.
Union
a union is a data form that can hold different data types, but only one type in a time, with the member name indicating which variable is acting. The size of the union is the size of its largest member.
union id
{
long in_num;
const char* name;
}
id.in_num = 3L; // store a long value
id.name = "Hahaha"; // long value is lost
union
{
long ln_num;
const char* name;
}
ln_num = 4L;
name = "lulalal";
a union can be anoymous, its variables share the same address. it is said that a union is often used in operating system, embedded systems, or hadrware data structures.
enumeration
C++ enum facility provides an alternative to const for creating symbolic constants. it allow one to define new types but in a fairly restricted fashion. Here is the code snippet
enum spectrum{red, orange, yellow, green, blue, violet, indigo, ultraviolet};
// spectrum is termed an enumeration
// red, orange, yellow and the like act as symbolic constants for the integer value 0-7
// these constants are called enumerators
by default, enumerators are assigned interger values starting with 0 for the first one, 1 for the second and so forth.
spectrum band; // use an enumeration name to declare a variable
band = red; // valid
band = 10086; // invalid, 10086 not a enumerator
--band; // invalid
++band; // disallow
band += 1; // not allow
when using the enumeration, here are some rules u should keep in mind:
one can assign an enumeration name to declare a variable of the enumeration type
only the assignment operator is defined for enumerators, so in the snippet, ++band is not allowed.
enumerators are of integer type and can be promoted to type int, but type int are not converted automatically to the enumeration type
int num = yellow;
num = spectrum(7); // type cast
num += red; // red and num is automatically promopted to int, so it is valid
and then, show u how to set enumrator values
enum {zero, null = 0,two = 2, hundred = 100, last}; //anonymous
// zero == null == 0, last == 101
then comes the value range
each enumeration has a range. one can assign any integer value in the range
enum bits{one = 1, ten = 10};
bits aflag;
aflag = bits(8); //valid
the upper limit is 2^n - 1, in the conditon that 2^n > largest value in enumerations
if the smallest value is o or greater, the lower limit for the range is 0
if the smallest value is negative, it is 2^m + 1, in the condition that 2^m < smallest value in enumerations
Summary
here is the overview of some compounds types in C++, you can take this blog as a reference.
Type is an important part in C++. Different types are used for different purposes and have their own characterstics. On the other hand, they connet each other in such a close way that we may find it interesting, especially in the view of pointers and arrays. Take care when u deal with types so that u may avoid type mismatch error. When using functions or methods, be sensetive with types may bring u some benefit.
i would remind u that what i written here is not complete, and even the enumeration part takes the largest portion, it is in fact used not as frequent as pointers, arrays and so on. u have to code and meet problems then u can really have a commad of them.
if u find any problems here, feel free to point to and i would modify immediately.
hopefully, it would be of help to u.