C Expert Programming note (iii)--analysis of the Declaration of C language

Source: Internet
Author: User
Tags volatile

1. Analysis of several C-language declarations

char (*J) [20];

j = (char (*) [+]) malloc (20); J is a pointer to an array

const int * GRAPE;

int const * GRAPE;

int * Const grape_jelly;

const int * Const GRAPE_JAM;

int const * Const GRAPE_JAM;

char * const * (*next) (); Next is a pointer to the function that does not receive the parameter, and its return value is a pointer to char * Const.

2. Restrictions on some unlawful declarations and legal statements

Illegal: The return value of a function cannot be a function; The return value of a function cannot be an array; there is no function in the array.

Legal: The return value of the function allows a function pointer; the return value of the function allows a pointer to an array, a function pointer is allowed in the array, and other arrays are allowed inside the array.

3. Priority rules for claims

A. The declaration begins with its name and is then read in order of precedence.

B. Priority levels from high to low are:

The part of the B.1 declaration that is enclosed in parentheses

B.2 suffix operator:

parentheses () indicate that this is a function, and

square brackets [] indicate that this is an array.

B.3 prefix operator: an asterisk * means "point to ...". 's pointer. "

C. If the const and/or volatile keyword is followed by a type specifier (such as int,long, etc.), it acts on the type specifier. In other cases, the const and/or volatile keywords are used for the asterisk of the pointer immediately to the left of it.

4. Use typedef to simplify claims

In the ANSI C standard, the Declaration of Signal () is void (*signal (int sig, Void (*FUNC) (int)))) (int);

According to the rule B.2, we know that signal is a function (unlike the above next,next is directly enclosed by parentheses), the function's argument is the int sig, Void (*FUNC) (int), and the return value is void (*) (int).

We use typedef to extract the generic part of the signal Declaration, simplifying this declaration.

typedef void (*PTR_TO_FUNC) (int);

Then signal's statement became: Ptr_to_func signal (int sig, Ptr_to_func);

5. The difference between typedef int X[10] and # define x INT[10]

typedef int X[10] Sets X to the type of an integer array containing 10 elements. #define X INT[10] simply replaces the text.

typedef int X[10];

x arr1, arr2, ARR3; Legal

#define X INT[10];

x arr1, arr2, ARR3; Illegal

#define PEACH INT

unsigned peach i; Legal

typedef int Banana;

unsigned banana i; Illegal

#define INT_PTR int *

INT_PTR Chalk, cheese; Chalk to int*, cheese to int

typedef int * INT_PTR;

INT_PTR Chalk, cheese; Both chalk and cheese are int*

6. Several suggestions for using typedef

Do not use typedef on structures for the sake of convenience. The only benefit is that you can save writing structs, but the struct keyword can prompt you for this information.

typedef should be used in arrays, structs, pointers, and combinations of functions, portable types, and providing a simple name for subsequent coercion type conversions.

C Expert Programming note (iii)--analysis of the Declaration of C language

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.