전체 페이지뷰

2013년 2월 6일 수요일

Pointer , structuring

Pointer
This chapter covered a lot of ground. You explored pointers in detail. You should now understand the relationship between pointers and arrays (both one-dimensional and multidimensional arrays) and have a good grasp of their uses. I introduced the malloc(), calloc(), and realloc() functions for dynamically allocating memory, which provides the potential for your programs to use just enough memory for the data being processed in each run. You also saw the complementary function free() that you use to release memory previously allocated by malloc(), calloc(), or realloc(). You should have a clear idea of how you can use pointers with strings and how you can use arrays of pointers.
The topics I’ve discussed in this chapter are fundamental to a lot of what follows in the rest of the book, and of course to writing C programs effectively, so you should make sure that you’re quite comfortable with the material in this chapter before moving on to the next chapter. The next chapter is all about structuring your programs.

STRUCTURING YOUR PROGRAMS
You’re not done with functions yet, so I’ll postpone diving into another chunky example until the end of the next chapter, which covers further aspects of using functions. So let’s pause for a moment and summarize the key points that you need to keep in mind when creating and using functions:
• C programs consist of one or more functions, one of which is called main(). The function main() is where execution always starts, and it’s called by the operating system through a user command.
• A function is a self-contained named block of code in a program. The name of a function is in the same form as identifiers, which is a unique sequence of letters and digits, the first of which must be a letter (an underline counts as a letter).
- A function definition consists of a header and a body. The header defines the name of the function, the type of the value returned from the function, and the types and names of all the parameters to the function. The body contains the executable statements for the function, which define what the function actually does.
• All the variables that are declared in a function are local to that function.
• A function prototype is a declaration statement terminated by a semicolon that defines the name, the return type, and the parameter types for a function. A function prototype is required to provide information about a function to the compiler when the definition of a function doesn’t precede its use in executable code.
• Before you use a function in your source file, you’d either define the function or declare the function with a function prototype.
• Specifying a pointer parameter as const indicates to the compiler that the function does not modify the data pointed to by the function.
• Arguments to a function should be of a type that’s compatible with the corresponding param- eters specified in its header. If you pass a value of type double to a function that expects an integer argument, the value will be truncated, removing the fractional part.
• A function that returns a value can be used in an expression just as if it were a value of the same type as the return value.
• Copies of the argument values are transferred to a function, not the original values in the calling function. This is referred to as the pass-by-value mechanism for transferring data to a function.
• If you want a function to modify a variable that’s declared in its calling function, the address of the variable needs to be transferred as an argument.
That covers the essentials of creating your own functions. In the next chapter, you’ll add a few more techniques for using functions. You’ll also work through a more substantial example of applying functions in a practical context.

댓글 없음:

댓글 쓰기