C Language Basics Tutorial (iv) Pointers, structures, unions, and enumerations (1)

Source: Internet
Author: User
Tags definition

This section specifically describes the pointers described in section II. This paper introduces the new data types of turbo C: structure, union and enumeration, where the structure and union are the five basic data types (integer, floating-point, character, pointer and no value). An enumeration is a collection of named integer constants.
Finally, the type description (typedef) and preprocessing instructions are expounded.

1. Pointer (point)

Learn the turbo C language, if you can not use the pointer to write effective, correct and flexible procedures, you may think that you do not learn C language. pointers, addresses, arrays, and their interrelationships are the most distinctive parts of the C language. Using pointers in a standardized way can make a program simple and straightforward, so we should not only learn how to use pointers correctly, but also learn to use pointer variables correctly in various situations.
1. Pointers and Addresses
1.1 The basic concept of pointers and the definition of pointer variables
Definition of 1.1.1 pointer variable
We know that variables in the computer occupy a storage area, the value of the variable is stored in this area, within the computer, by accessing or modifying the contents of this area to access or modify the corresponding variables. In Turbo C, one of the forms of access to a variable is to first find the address of the variable and then access it through the address, which is the pointer and its pointer variable to be discussed here.
A pointer to a variable is actually the address of a variable. The address of a variable, although formally similar to an integer, is conceptually different from the previously introduced integer and belongs to a new data type, the pointer type. In Turbo C, generally use "pointer" to indicate the type of such an expression &x, and use "address" as its value, that is, if x is an integer variable, the type of the expression &x is a pointer to an integer, and its value is the address of the variable x. Similarly, if double D; The &d type is a pointer to the precision number D, and the &d value is the address of the double-precision variable d. Therefore, pointers and addresses are used to describe two aspects of an object. Although the values of &x and &d are the addresses of integer variables x and double-precision variable D, the types of &x and &d are different, one is the pointer to an integer variable x, and the other is a pointer to a double-precision variable d. In many cases, the two terms pointer and address are mixed.
We can define a variable of a pointer type in the following ways.
int *ip;
First of all, it is a pointer type of variable, note that in the definition do not omit to write the symbol "*", otherwise it is a general integer variable. In addition, an int in the definition indicates that the pointer variable is a variable of the pointer type that points to an integer, and sometimes it is also called an IP pointer to a whole number. IP is a variable that specifically stores the address of an integer variable.
The general definition of pointer variables is:
Type identifier * identifier;
Where the identifier is the name of the pointer variable, preceded by the "*" number, which indicates that the variable is a pointer variable, and the first "type identifier" represents the type of the variable that the pointer variable is pointing to. A pointer variable can only point to a variable of the same type, that is to say, we cannot define a pointer variable that can point to both an integer variable and a double-precision variable.
The pointer variable allows an initialization entry in the definition. Such as:
int I, *ip=&i;
Note that this is initialized with &i to IP instead of *IP. As with general variables, the pointer variable is initialized to NULL for an external or static pointer variable in the definition without an initialization item, and its value is 0. Turbo c stipulates that when the pointer value is zero, the pointer does not point to any valid data, and sometimes the pointer is also called a null pointer. Therefore, when you call a function to return a pointer (described in section Fifth), you often use a return value of NULL to indicate that some error conditions occur in a function call.

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.