Expert C Programming Study Notes

Source: Internet
Author: User

The following code is compiled in vs2005,

I. P19

Pass the char ** type to the const char **

 

1 Foo (const char ** p ){}

2

3 void main (INT argc, char ** argv)

4 {

5 Foo (arvg );

6}

If this code is compiled, the compiler sends a warning message:

Line 5: Warning argument is incompatible with prototype

(Row 3: warning: the parameter does not match the prototype ).

The question is: the real parameter char * s is compatible with the form parameter const char * P (all string processing functions in the standard library look like this ), why is the real parameter char ** argv incompatible with the form parameter const char ** P?

In the ansi c standard, each real parameter should have its own type, in this way, the value can be assigned to the object of the corresponding parameter type (the object type cannot contain a qualifier ).

This means that the parameter passing process is similar to assigning values. Therefore, unless a char ** type value can be assigned to a const char ** type object, a Diagnostic message is generated. To make the assignment legal, you must meet one of the following conditions:

Both operands are pointer to compatible types with or without delimiters. The type pointed to by the Left pointer must have all the delimiters of the type pointed to by the right pointer.

This condition allows the real parameter char * in the function call to match the const char.

1 char * CP;

2 const char * CCP;

3 CCP = CP; // right

4 cp = CCP; // generate a compilation warning

To thoroughly understand why char ** is incompatible with const char **, let's first review const float *: it is not a type with a qualifier -- its type is "pointer to a float type with a const qualifier", that is, the const qualifier is the type pointed to by the modifier pointer, instead of the pointer itself.

Similarly, const char ** is also a pointer type without a qualifier. Its type is "pointer to a char type pointer with a const qualifier ".

Because char ** and const char ** are pointer types without delimiters, But they point to different types (the former points to char * and the latter points to const char *), therefore, they are two different types and are incompatible. Therefore, parameters with the actual participation type of char ** being const char ** are incompatible. It violates the constraints listed in the italics listed above.

I can't quite understand the above explanation !!!

Compilation result:

Use C to compile 0 warnings and 0 errors. (It seems that ansi c is not followed)

Compile error c2664 in C ++: you cannot convert parameter 1 from "char **" to "const char **"

 

II,

Last section of P30

Switch has some problems, one of which is .....

In the last section of P31, in C, the const keyword does not really represent a constant,

I wrote an example:

 

void fun(){int x=1;const int cy =2;switch (x){int temp=3;temp++; case 1: {int y=temp;y++;break;}case cy: {break;}default:break;}

Compile with C

Error c2051: The Case expression is not a constant.

(If case Cy: entire segment is commented out) A warning is reported: The uninitialized local variable "Temp" is used ",

At runtime, 7th rows are not executed at all and are directly executed to 11th rows.

Compile with C ++

Error c2360: the "Temp" initialization operation is skipped by the "case" label (that is, the temp definition is illegal)

However, in C ++, Case Cy: Is used legally.

 

Iii. p34

 

Char * STR [] =

{"I ",

"Love"

"You"

};

Love is followed by a comma. If you modify STR [2], other variables are modified.

 

Char * text = "ABC \

Def ";

Char * text2 = "ABC"

"Def ";

Text and text2 are not equal. Text can have more tabs.

 

 

Iv. p64

 

The priority rules declared in C language are as follows:

 

A declares that it reads data starting from its name and then reads data in order of priority;

The priority of B is from high to low:

B .1 The Section enclosed in parentheses in the statement;

B .2 suffix OPERATOR: brackets () indicate that this is a function, and square brackets [] indicate that this is an array;

B .3 prefix OPERATOR: asterisk * identifies "pointing ...... Pointer ";

C. If the const and (or) Volatile keywords follow the type specifiers (such as int and long), it acts on the type specifiers. In other cases, const and (or) the volatile keyword acts on the pointer asterisk next to its left.

Example: char * const * (* Next )();

A next -- next is the declared name.

B .1 (* Next) -- next is a point ...... Pointer

B .2 (* Next) () -- next is a function pointer.

B .3 * (* Next) () -- next is a function pointer. This function returns a pointer ...... Pointer

C char * const -- a constant pointer to a character type

Therefore, char * const * (* Next) (); indicates that next is a function pointer, which returns a constant pointer to the character type.

Let's analyze a statement by ourselves:

INT (* Foo () [];

First, let's take a look at the result. foo is a function that returns a pointer to an integer array. Right? The following describes the specific derivation process:

A foo -- foo is the declared name.

B .2 Foo () -- foo is a function.

B .3 (* Foo () -- foo is a function that returns a pointer ...... Pointer

B .2 (* Foo () [] -- foo is a function that returns a pointer to an array.

C int (* Foo () [] -- foo is a function that returns a pointer to an integer array.

 

 

V. p68

Differences between typedef and macro

 

Section 6 P83--P86

Differences between array and pointer access

My personal feelings are clear in the following examples:

 

/* 1. C */Char Jan [10] = "Jan"; char Feb [10] = "FEB";/* 2. C */extern char Jan []; extern char * Feb; int main () {char * P = Jan; char c_jan = Jan [1]; char c_jan2 = * (Jan + 1); char C_p = P [1]; char c_p2 = * (p + 1 ); /* the error address during running cannot be accessed */Char c_feb = Feb [1]; return 0;}/* corresponding Assembly */char * P = Jan; 0042d66e mov dword ptr [p], offset _ Jan (493000 h) Char c_jan = Jan [1]; 0042d675 mov Al, byte PTR [_ Jan + 1 (493001 h)] 0042d67a mov byte PTR [c_jan], Al char c_jan2 = * (Jan + 1); 0042d67d mov Al, byte PTR [_ Jan + 1 (493001 h)] 0042d682 mov byte PTR [c_jan2], Al char C_p = P [1]; 0042d685 mov eax, dword ptr [p] 0042d688 mov Cl, byte PTR [eax + 1] 0042d68b mov byte PTR [C_p], CL char c_p2 = * (p + 1); 0042d68e mov eax, dword ptr [p] 0042d691 mov Cl, byte PTR [eax + 1] 0042d694 mov byte PTR [c_p2], CL // The error address during runtime cannot access char c_feb = Feb [1]; 0042d697 mov eax, dword ptr [_ FEB (49300ch)] 0042d69c mov Cl, byte PTR [eax + 1] 0042d69f mov byte PTR [c_feb], CL return 0; 0042d6a2 XOR eax, eax

 

 

VII. p103

About

A bug related to interpositioning in SunOS

For better understanding, I wrote an executable example.

 

1. c

double  sqrt(double x, double y){return x+y;}void fun();int  main(){fun();return 0;}

2. c

#include <math.h>double fun(double x){double xx = sqrt(x);return xx;}

After running, we found that SQRT in the fun function ran to double SQRT (Double X, Double Y). In this function, we must resolutely avoid such functions with the same name.

However, this phenomenon does not occur in C ++,

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////////

P172

Char CH = 'a ';
Printf ("% d/N", sizeof (CH), sizeof ('A '));

// Save As. c output 1 4

// Save As. cpp output 1 1

Table 8-1 explains

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////

P176

Description and definition of functions

1. c

# Include <stdio. h>
Void newdef (float D, char I)
{
Printf ("newdef: Float = % F, char = % x/N", D, I );
}

 

2. c

Int main ()
{
Newdef (10.0, 3 );
Return 0;
}
When compiling in Vs, the following error occurs: Warning c4013: "newdef" undefined; suppose the external return int

Running result: newdef: Float = 0.000000, char = 0

 

If you change two. c files to. cpp, an error is reported.

Error c3861: "newdef": unable to find the identifier

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////

Bottom of P201

When is the array and pointer the same?

 

Rule 1: The array name in the expression (different from the declaration) is treated as a pointer to the first element of the array by the compiler.
Rule 2: The subscript is always the same as the pointer offset.
Rule 3: In the function declaration, the array name is treated as a pointer to the first element of the array by the compiler.

 

P212

Figure 9-8 storage of multi-dimensional arrays

 

P225

How are array and pointer parameters modified by the compiler?

"The array name is rewritten as a pointer parameter" rules are not recursively defined. arrays of arrays will be rewritten as "array Pointers" instead of "Pointer"

Parameters matching
Array char C [8] [10]; char (*) [10]; array pointer
Pointer array char * C [15]; char ** C; pointer
Array pointer char (* C) [64]; char (* C) [64]; unchanged
Pointer char ** C; unchanged

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////

The P266--P268 mentioned the difference between C and C ++.

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.