iOS Development Note Series-Basic 7 (C language Features)

Source: Internet
Author: User
Tags date1

Objective-c is a C language extension, so there are a lot of C language basic features, here is only part of the list.

Block (Blocks)

  A block is an extension to the C language that is not part of the standard ANSI C definition, but is added to the language by Apple. It looks much like a function, it can pass arguments to it, it also has a return value, and unlike a function, a block is defined inside a function or method, and can access any variable outside of the block within a function or method. In general, it can access these variables but cannot modify their values, and there is a special block modifier (consisting of two underlined characters in front of the block) that modifies the value of the variable within the block. The block itself can also be passed as a parameter to a function or method.

One of the advantages of a block is that it allows the system to be executed by other threads that are assigned to other processors or applications.

The block is identified with the insertion character "^" at the beginning. followed by a parenthesis that identifies the list of parameters required by the block. Like what:

^ (void) {    ...} // No semicolon // This block does not accept parameters
// You can also assign a block to a variable, such as: int (^myblock) (void) =^ (void) { ...
return somevalue;}; // end of assignment with semicolon

The block can be accessed but cannot modify variables outside of the block inside the function, but if the variable definition is preceded by a __block modifier, it can be, for example, a variable outside the block in the function defined as

int foo=1;

Structure

In addition to arrays, OBJECTIVE-C also provides a tool for another combination of elements, the structure. Like what:

struct date{int  month; int Day ; int Year ;} // struct Date today; // declares today variable to be a struct date type today.day=1; // The dot symbol is a special syntax for working with struct variables to access struct members

The initialization of a struct can be directly assigned, such as:

struct date today={1,2,2001}// at which time the struct date is assigned sequentially today={1}// assigns only the first element to struct date today={.month=1,. day=2 ,. year=2001}// Specify order assignment

A typedef is often used to change the name of a struct so that it does not have to be marked with a "struct" when declaring a variable, such as:

structstruct  cgpoint cgpoint; // you don't need to write a struct when declaring a variable. Cgpoint p;

Some of the following are legal:

struct Date {...} date1,date2; // struct Date {...} date1={1,2,2001}; // struct  //  {...} date1={...}; // struct {...} dates[]; // declaring arrays, elements as structures

Pointer

  A pointer works indirectly, and its value is an address that represents the memory address of the variable it points to, declaring it by adding an asterisk after the variable name type, such as int *intptr, so that the intPtr can no longer be assigned directly to the value of type int, but to the memory address. Like intptr=&somevalue;.

You can use the indirect addressing operator (*), such as int x=*intptr, to refer to the contents of Somevalue by pointer variable intPtr;

Pointers can also point to complex data types, such as structs, in addition to the basic data types

struct s{...}; struct s svalue; struct s *sptr; //svalue.property=1; sptr=&svalue; (*sptr). property=2; Sptr->property=3; // This is a special operator for struct pointers

function pointers

A function pointer is a pointer variable to a function, and you need to know the return value type of the function and the number and type of the parameter, such as to declare a pointer to a function that returns int without parameters, as follows

Int (*fnptr) (void)

The brackets on both sides of the *fnptr are required and can then be used: fnptr=loopfunc; (Loopfunc is a function that returns int without arguments).

A function pointer can be passed as a parameter to other functions, another common application of a function pointer is to create an allocation table, the function itself cannot be saved in an array element, but you can store the function pointer in an array, and you can create a table that contains the function pointer that you want to invoke.

Command-line arguments

The location where the program starts executing is the main function until the main function is finished and control is returned to the system. When the main function is called, the system passes two parameters to it, the first is argc (argument count), is an integer value, the second is a argv (argument vector), is a character pointer array, and the length is argc+1.

int main (int argc, char *argv[]) {...}

The first element of the argv array is the name pointer of the executing program, or an empty string if there is no program name in the system, and the other items of the array point to the value that is referred to by the command line executed by the initiator. The last pointer in the array argv ARBV[ARGC] is specified as null.

iOS Development Note Series-Basic 7 (C language Features)

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.