亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? begpoint.txt

?? 有關(guān)指針的使用問題
?? TXT
?? 第 1 頁 / 共 3 頁
字號:
           UNDERSTANDING POINTERS (for beginners)

                      by  Ted Jensen

                       Version 0.0

      This material is hereby placed in the public domain.

                     September 5, 1993


                     TABLE OF CONTENTS


    INTRODUCTION;


    CHAPTER 1: What is a pointer?


    CHAPTER 2: Pointer types and Arrays


    CHAPTER 3: Pointers and Strings


    CHAPTER 4: More on Strings


    CHAPTER 5: Pointers and Structures


    CHAPTER 6: Some more on Strings, and Arrays of Strings


    EPILOG:


==================================================================


INTRODUCTION:


    Over a period of several years of monitoring various

telecommunication conferences on C I have noticed that one of the

most difficult problems for beginners was the understanding of

pointers.  After writing dozens of short messages in attempts to

clear up various fuzzy aspects of dealing with pointers, I set up

a series of messages arranged in "chapters" which I could draw

from or email to various individuals who appeared to need help in

this area.


    Recently, I posted all of this material in the FidoNet CECHO

conference.  It received such a good acceptance, I decided to

clean it up a little and submit it for inclusion in Bob Stout's

SNIPPETS file.


    It is my hope that I can find the time to expand on this text

in the future.  To that end, I am hoping that those who read this

and find where it is lacking, or in error, or unclear, would

notify me of same so the next version, should there be one, I can

correct these deficiencys.


    It is impossible to acknowledge all those whose messages on

pointers in various nets contributed to my knowledge in this

area.  So, I will just say Thanks to All.


    I frequent the CECHO on FidoNet via RBBSNet and can be

contacted via the echo itself or by email at:


     RBBSNet address 8:916/1.


I can also be reached via


Internet email at ted.jensen@spacebbs.com


Or     Ted Jensen

       P.O. Box 324

       Redwood City, CA 94064


==================================================================

CHAPTER 1: What is a pointer?


    One of the things beginners in C find most difficult to

understand is the concept of pointers.  The purpose of this

document is to provide an introduction to pointers and their use

to these beginners.


    I have found that often the main reason beginners have a

problem with pointers is that they have a weak or minimal feeling

for variables, (as they are used in C).  Thus we start with a

discussion of C variables in general.


    A variable in a program is something with a name, the value

of which can vary.  The way the compiler and linker handles this

is that it assigns a specific block of memory within the computer

to hold the value of that variable.  The size of that block

depends on the range over which the variable is allowed to vary.

For example, on PC's the size of an integer variable is 2 bytes,

and that of a long integer is 4 bytes.  In C the size of a

variable type such as an integer need not be the same on all

types of machines.


    When we declare a variable we inform the compiler of two

things, the name of the variable and the type of the variable.

For example, we declare a variable of type integer with the name

k by writing:


    int k;


    On seeing the "int" part of this statement the compiler sets

aside 2 bytes (on a PC) of memory to hold the value of the

integer.  It also sets up a symbol table. And in that table it

adds the symbol k and the address in memory where those 2 bytes

were set aside.


    Thus, later if we write:


    k = 2;


at run time we expect that the value 2 will be placed in that

memory location reserved for the storage of the value of k.


    In a sense there are two "values" associated with k, one

being the value of the integer stored there (2 in the above

example) and the other being the "value" of the memory location

where it is stored, i.e. the address of k.  Some texts refer to

these two values with the nomenclature rvalue (right value,

pronounced "are value") and lvalue (left value, pronunced "el

value") respectively.


    The lvalue is the value permitted on the left side of the

assignment operator '=' (i.e. the address where the result of

evaluation of the right side ends up).  The rvalue is that which

is on the right side of the assignment statment, the '2' above.

Note that rvalues cannot be used on the left side of the

assignment statement.  Thus:    2 = k;   is illegal.


    Okay, now consider:


    int j, k;

    k = 2;

    j = 7;    <-- line 1

    k = j;    <-- line 2


    In the above, the compiler interprets the j in line 1 as the

address of the variable j (its lvalue) and creates code to copy

the value 7 to that address.  In line 2, however, the j is

interpreted as its rvalue (since it is on the right hand side of

the assignment operator '=').  That is, here the j refers to the

value _stored_ at the memory location set aside for j, in this

case 7.  So, the 7 is copied to the address designated by the

lvalue of k.


    In all of these examples, we are using 2 byte integers so all

copying of rvalues from one storage location to the other is done

by copying 2 bytes.  Had we been using long integers, we would be

copying 4 bytes.


    Now, let's say that we have a reason for wanting a variable

designed to hold an lvalue (an address).  The size required to

hold such a value depends on the system.  On older desk top

computers with 64K of memory total, the address of any point in

memory can be contained in 2 bytes.  Computers with more memory

would require more bytes to hold an address.  Some computers,

such as the IBM PC might require special handling to hold a

segment and offset under certain circumstances.  The actual size

required is not too important so long as we have a way of

informing the compiler that what we want to store is an address.


    Such a variable is called a "pointer variable" (for reasons

which will hopefully become clearer a little later).  In C when

we define a pointer variable we do so by preceding its name with

an asterisk.  In C we also give our pointer a type which, in this

case, refers to the type of data stored at the address we will be

storing in our pointer.  For example, consider the variable

definition:


    int *ptr;


    ptr is the _name_ of our variable (just as 'k' was the name

of our integer variable).  The '*' informs the compiler that we

want a pointer variable, i.e. to set aside however many bytes is

required to store an address in memory.  The "int" says that we

intend to use our pointer variable to store the address of an

integer. Such a pointer is said to "point to" an integer.  Note,

however, that when we wrote  "int k;" we did not give k a value.

If this definiton was made outside of any function many compilers

will initialize it to zero.  Simlarly, ptr has no value, that is

we haven't stored an address in it in the above definition.  In

this case, again if the definition is outside of any function, it

is intialized to a value #defined by your compiler as NULL.  It

is called a NULL pointer.  While in most cases NULL is #defined

as zero, it need not be.  That is, different compilers handle

this differently.  Also note that while zero is an integer, NULL

need not be.


    But, back to using our new variable ptr.  Suppose now that we

want to store in ptr the address of our integer variable k.  To

do this we use the unary '&' operator and write:


    ptr = &k;


    What the '&' operator does is retrieve the lvalue (address)

of k, even though k is on the right hand side of the assignment

operator '=', and copies that to the contents of our pointer ptr.

Now, ptr is said to "point to" k.  Bear with us now, there is

only one more operator we need to discuss.


    The "dereferencing operator" is the asterisk and it is used

as follows:


    *ptr = 7;


will copy 7 to the address pointed to by ptr.  Thus if ptr

"points to" (contains the address of) k, the above statement will

set the value of k to 7.  That is, when we use the '*' this way

we are refering to the value of that which ptr is pointing

at, not the value of the pointer itself.


    Similarly, we could write:


    printf("%d\n",*ptr);


to print to the screen the integer value stored at the address

pointed to by "ptr".


    One way to see how all this stuff fits together would be to

run the following program and then review the code and the output

carefully.


-------------------------------------------------

#include <stdio.h>


int j, k;

int *ptr;



int main(void)

{

   j = 1;

   k = 2;

   ptr = &k;

   printf("\n");

   printf("j has the value %d and is stored at %p\n",j,&j);

   printf("k has the value %d and is stored at %p\n",k,&k);

   printf("ptr has the value %p and is stored at %p\n",ptr,&ptr);

   printf("The value of the integer pointed to by ptr is %d\n",

           *ptr);

   return 0;

}

---------------------------------------

To review:


    A variable is defined by giving it a type and a name (e.g.

     int k;)


    A pointer variable is defined by giving it a type and a name

     (e.g. int *ptr) where the asterisk tells the compiler that

     the variable named ptr is a pointer variable and the type

     tells the compiler what type the pointer is to point to

     (integer in this case).


    Once a variable is defined, we can get its address by

     preceding its name with the unary '&' operator, as in &k.


    We can "dereference" a pointer, i.e. refer to the value of

     that which it points to, by using the unary '*' operator as

     in *ptr.


    An "lvalue" of a variable is the value of its address, i.e.

     where it is stored in memory.  The "rvalue" of a variable is

     the value stored in that variable (at that address).


==================================================================

CHAPTER 2: Pointer types and Arrays


    Okay, let's move on.  Let us consider why we need to identify

the "type" of variable that a pointer points to, as in:


        int *ptr;


    One reason for doing this is so that later, once ptr "points

to" something, if we write:


        *ptr = 2;


the compiler will know how many bytes to copy into that memory

location pointed to by ptr.  If ptr was defined as pointing to an

integer, 2 bytes would be copied, if a long, 4 bytes would be

copied.  Similarly for floats and doubles the appropriate number

will be copied.  But, defining the type that the pointer points

to permits a number of other interesting ways a compiler can

interpret code.  For example, consider a block in memory

consisting if ten integers in a row.  That is, 20 bytes of memory

are set aside to hold 10 integer.


    Now, let's say we point our integer pointer ptr at the first

of these integers.  Furthermore lets say that integer is located

at memory location 100 (decimal).   What happens when we write:


    ptr + 1;


    Because the compiler "knows" this is a pointer (i.e. its

value is an address) and that it points to an integer (its

current address, 100, is the address of an integer), it adds 2 to

ptr instead of 1, so the pointer "points to" the _next_

_integer_, at memory location 102.  Similarly, were the ptr

defined as a pointer to a long, it would add 4 to it instead of

1.  The same goes for other data types such as floats, doubles,

or even user defined data types such as structures.


    Similarly, since ++ptr and ptr++ are both equivalent to

ptr + 1 (though the point in the program when ptr is incremented

may be different), incrementing a pointer using the unary ++

operator, either pre- or post-, increments the address it stores

by the amount sizeof(type) (i.e. 2 for an integer, 4 for a long,

etc.).


    Since a block of 10 integers located contiguously in memory

is, by definition, an array of integers, this brings up an

interesting relationship between arrays and pointers.


    Consider the following:


    int my_array[] = {1,23,17,4,-5,100};


    Here we have an array containing 6 integers.  We refer to

each of these integers by means of a subscript to my_array, i.e.

using my_array[0] through my_array[5].  But, we could

alternatively access them via a pointer as follows:


    int *ptr;


    ptr = &my_array[0];       /* point our pointer at the first

                                 integer in our array */


    And then we could print out our array either using the array

notation or by dereferencing our pointer.  The following code

illustrates this:

------------------------------------------------------

#include <stdio.h>


int my_array[] = {1,23,17,4,-5,100};

int *ptr;


int main(void)

{

    int i;

    ptr = &my_array[0];     /* point our pointer to the array */

    printf("\n\n");

    for(i = 0; i < 6; i++)

    {

      printf("my_array[%d] = %d   ",i,my_array[i]);   /*<-- A */

      printf("ptr + %d = %d\n",i, *(ptr + i));        /*<-- B */

    }

    return 0;

}

----------------------------------------------------

   Compile and run the above program and carefully note lines A

and B and that the program prints out the same values in either

case.  Also note how we dereferenced our pointer in line B, i.e.

we first added i to it and then dereferenced the the new pointer.

Change line B to read:


     printf("ptr + %d = %d\n",i, *ptr++);


and run it again... then change it to:


     printf("ptr + %d = %d\n",i, *(++ptr));


and try once more.  Each time try and predict the outcome and

carefully look at the actual outcome.


    In C, the standard states that wherever we might use

&var_name[0] we can replace that with var_name, thus in our code

where we wrote:


        ptr = &my_array[0];


    we can write:


        ptr = my_array;     to achieve the same result.


    This leads many texts to state that the name of an array is a

pointer.  While this is true, I prefer to mentally think "the

name of the array is a _constant_ pointer".  Many beginners

(including myself when I was learning) forget that _constant_

qualifier.  In my opinon this leads to some confusion.  For

example, while we can write ptr = my_array; we cannot write

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美日韩一二三区| 欧美一区二区三区成人| 久久99精品国产91久久来源| 国产精品全国免费观看高清| 91精品欧美福利在线观看| 99vv1com这只有精品| 韩国欧美国产1区| 亚洲一区二区影院| 欧美国产日韩一二三区| 欧美电影免费观看高清完整版| 日本乱人伦一区| 懂色av一区二区三区免费看| 日韩福利电影在线观看| 亚洲精品国产成人久久av盗摄| 亚洲精品在线网站| 91精品国产一区二区| 在线观看免费成人| 成人av在线播放网站| 精品一二三四区| 日韩中文字幕91| 亚洲国产一区二区三区| 日韩码欧中文字| 国产亚洲欧美一级| 精品国产一区二区亚洲人成毛片| 在线不卡一区二区| 在线免费观看视频一区| 94色蜜桃网一区二区三区| 国产.欧美.日韩| 国产精品综合一区二区| 国产在线麻豆精品观看| 老司机精品视频线观看86| 三级亚洲高清视频| 天堂va蜜桃一区二区三区漫画版| 夜夜精品视频一区二区| 一区二区三区四区激情| 亚洲综合色区另类av| 依依成人精品视频| 一区二区在线观看视频 | 91麻豆精品国产91久久久资源速度 | 一区二区在线免费观看| 一区在线观看视频| 亚洲欧洲av在线| 亚洲视频在线一区观看| 亚洲另类在线一区| 亚洲一区二区av在线| 亚洲第一av色| 日韩精品久久理论片| 日韩va欧美va亚洲va久久| 免费成人性网站| 美腿丝袜亚洲色图| 精品一区免费av| 国产一区二区在线观看视频| 国产剧情一区二区三区| 成人在线视频首页| 9人人澡人人爽人人精品| 99久久精品国产毛片| 色综合 综合色| 欧美丝袜丝交足nylons图片| 欧美高清激情brazzers| 91精品国产入口| 久久亚洲一级片| 1024成人网| 亚洲午夜久久久久久久久电影院| 日日夜夜精品视频免费| 韩国精品在线观看| av综合在线播放| 欧美私模裸体表演在线观看| 欧美大胆人体bbbb| 中国色在线观看另类| 一区二区三区四区乱视频| 日本午夜精品一区二区三区电影| 国产在线精品不卡| 99精品视频在线免费观看| 欧美亚洲动漫精品| 欧美成人猛片aaaaaaa| 国产精品美女一区二区在线观看| 亚洲精品第一国产综合野| 日韩精品亚洲专区| 国产suv精品一区二区6| 欧美亚洲日本国产| 久久综合五月天婷婷伊人| 最新久久zyz资源站| 日韩—二三区免费观看av| 成人黄色在线网站| 91精品国产综合久久精品图片| 久久精品人人做人人综合 | 激情综合色播激情啊| www.日韩精品| 欧美精品久久久久久久多人混战| 久久精品亚洲精品国产欧美| 亚洲一区二区中文在线| 国产一区不卡精品| 精品视频在线免费看| 久久精品日产第一区二区三区高清版| 亚洲免费看黄网站| 久久91精品久久久久久秒播| 一本大道久久a久久精二百| 欧美变态凌虐bdsm| 亚洲资源在线观看| 国产不卡一区视频| 欧美成人女星排名| 亚洲高清中文字幕| jiyouzz国产精品久久| 日韩一卡二卡三卡四卡| 亚洲黄色免费网站| 成人动漫中文字幕| 2欧美一区二区三区在线观看视频 337p粉嫩大胆噜噜噜噜噜91av | 欧洲国产伦久久久久久久| 久久久久免费观看| 奇米精品一区二区三区四区| 色综合咪咪久久| 国产日韩三级在线| 精品一区二区三区香蕉蜜桃| 538在线一区二区精品国产| 亚洲欧美影音先锋| 国产成a人亚洲精| 2023国产精品视频| 美女mm1313爽爽久久久蜜臀| 欧美在线啊v一区| 中文字幕亚洲精品在线观看| 国产不卡免费视频| 2017欧美狠狠色| 九九九久久久精品| 欧美一区二区三区视频在线观看 | 国产三级精品三级| 黄一区二区三区| 欧美一级专区免费大片| 午夜欧美在线一二页| 欧美色综合影院| 亚洲一区在线观看免费观看电影高清| 99久久精品国产麻豆演员表| 国产精品网站在线| 成人激情开心网| 国产精品毛片久久久久久| 不卡视频免费播放| 国产精品伦理一区二区| 成人综合激情网| 国产精品视频麻豆| 99视频超级精品| 亚洲色图欧美偷拍| 日本精品视频一区二区三区| 有坂深雪av一区二区精品| 欧美在线免费视屏| 视频一区中文字幕| 91精品一区二区三区在线观看| 午夜国产精品一区| 日韩欧美一区在线| 久久99热国产| 久久精品在这里| www.色综合.com| 悠悠色在线精品| 777久久久精品| 美女任你摸久久| 久久久精品免费观看| 高潮精品一区videoshd| 一色桃子久久精品亚洲| 日本韩国精品在线| 五月激情综合婷婷| 精品日韩一区二区| 国产成+人+日韩+欧美+亚洲| 亚洲欧美日韩在线| 欧美巨大另类极品videosbest| 免费观看30秒视频久久| 中文字幕乱码久久午夜不卡| 色婷婷亚洲婷婷| 日韩av电影免费观看高清完整版| 久久综合视频网| 91一区二区在线| 手机精品视频在线观看| 国产校园另类小说区| 色妹子一区二区| 日韩精品一卡二卡三卡四卡无卡| 2023国产一二三区日本精品2022| 99国产精品国产精品久久| 婷婷久久综合九色综合绿巨人| 欧美电影免费观看完整版 | 91精品黄色片免费大全| 国产精品18久久久久久久久久久久| 亚洲欧美精品午睡沙发| 欧美一区二区三区视频在线| 成人激情图片网| 日韩和的一区二区| 中文字幕免费一区| 欧美群妇大交群中文字幕| 国产成人免费视| 性久久久久久久| 中文字幕不卡在线| 欧美日韩第一区日日骚| 成人中文字幕电影| 奇米精品一区二区三区在线观看一| 成人免费一区二区三区视频| 日韩精品一区二区三区四区| 日本久久电影网| 国产精品一区一区三区| 亚洲电影一级片| 国产精品久久久久永久免费观看| 91精品国产综合久久久久久漫画 | 国产成人亚洲综合a∨婷婷图片| 亚洲一区二区黄色| 中文字幕视频一区二区三区久|