![]() Pi = NULL /* assigns a 32 bit zero into pi */ This fact causes problems for novices who forget it. It is your responsibility to initialize pi before you use it. When you declare pi it contains whatever bit pattern was put there by the last process that wrote to that chunk of memory. Remember that C does not initialize variables for you. The only value you should ever assign into pi is the address of some integer variable or the NULL value which we will explain shortly. We will cover void * in our chapter on dynamic memory and again when we cover generic containers. The only exception is the void * type which is a generic pointer not associated with any particular type. Furthermore every pointer has a specific sub-type in that every pointer is a pointer to some type such as pointer to int, or pointer to char, or pointer to float, etc. The star means pi is a pointer to int, not an int, The pointer type is its own type. Int *pi /* creates a variable whose data type is pointer to int */ Once we explain how to declare, initialize and use pointers, we will show you the canonical scenario that requires the use of pointers to solve a problem for which there is no other solution. A pointer gives us an alias for a variable's name (and thus, its contents). The primary purpose of a pointer is to read and modify a variable without using that variable's real name. There are still many such machines around. Our discussions and illustrations will assume a 32 bit platform. On a 64 bit machine a pointer's size would be 8 bytes. When we say that a machine is a 32 bit address machine we are saying that a pointer is 32 bits wide and thus requires 4 bytes of storage (assuming 8 bits/byte). Since a pointer is used to store an address it must be large enough to store the address value of the highest byte of memory. The pointer type allows us to declare variables that hold the address of some other variable for later use. In order to save the address of v for later use, C provides a data type called the pointer type. The expression &v produces the memory address where that value is stored. The expression v produces the value of v. We understand that every variable v has to have a chunk of memory set aside to hold its value. 2.6: Strings - other forms of declaration and initialization Once pointers are understood, arrays become clear and strings are based on arrays.īlock & scope of variables and parametersĭeclaration, initialization, pass to functionsĪccessing rows, accessing individual characters We prefer to teach pointers as soon as possible because mastery of so many other concepts depends on understanding pointers and addressing. Many texts and courses leave the details of pointers for later chapters. The concept of pointers has historically been a difficult one.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |