tft每日頭條

 > 科技

 > 用指針變量的處理方法編寫c語言

用指針變量的處理方法編寫c語言

科技 更新时间:2025-06-11 06:26:15

  指針變量是C語言的靈魂,是C的效率和靈活性所在,也是安全問題所在。文件指針變量(FILE*)是一個指針結構體FILE的指針變量。

  1 指針變量 Pointer Variables The rules for using pointer variables are similar to regular variables, except that you need to think about two types: the type of the pointer variable, and the type stored in the memory address to which the pointer variable points.

  使用指針變量的規則與常規變量類似,隻是需要考慮兩種類型:指針變量的類型和存儲在指針變量指向的内存地址中的類型。

  1.1 Declare pointer variable 聲明指針變量

  First, declare a pointer variable using type_name *var_name:

  使用type_name *var_name聲明指針變量:

  int *ptr; // stores the memory address of an int (ptr points to an int) char *cptr; // stores the memory address of a char (cptr points to a char)

  數形結合:

  用指針變量的處理方法編寫c語言(使用指針變量和文件指針變量的基本步驟)(1)

  (聲明後但暫未初始化的指針變量的值并不是空值或零值,有一個曆史值或垃圾值,其指向不确定,其解引用未定義,其狀态不确定。)

  Note that although ptr and cptr are both pointers, they refer to different types:

  請注意,盡管ptr和cptr都是指針,但它們指的是不同的類型:

  The type of ptr is pointer to int (int *). It can point to a memory location that stores an int value.

  ptr的類型是“指向int的指針”(int*)。它可以指向存儲int值的内存位置。

  The type of cptr is pointer to char (char *). It can point to a memory location that stores a char value.

  cptr的類型是“指向字符的指針”(字符*)。它可以指向存儲字符值的内存位置。

  1.2 Initialize pointer variable 初始化指針變量

  Next, initialize the pointer variable (make it point to something).

  接下來,初始化指針變量(使其指向某物)。

  Pointer variables store address values. A pointer should be initialized to store the address of a memory location whose type matches the type to which the pointer variable points. One way to initialize a pointer is to use the address operator (&) with a variable to get the variable’s address value:

  指針變量存儲地址值。應該初始化指針以存儲其類型與指針變量指向的類型匹配的内存位置的地址。初始化指針的一種方法是将地址運算符(&)與變量一起使用,以獲取變量的地址值:

  int x; char ch; ptr = // ptr gets the address of x, pointer points to x cptr = // cptr gets the address of ch, pointer points to ch

  數形結合:

  用指針變量的處理方法編寫c語言(使用指針變量和文件指針變量的基本步驟)(2)

  (初始化後的指針變量的狀态是确定的。)

  Initialize ptr to the address of x and cptr to the address of ch (to point to x and ch, respectively).

  将ptr初始化為x地址,将cptr初始化為ch地址(分别指向x和ch)。

  Here’s an example of an invalid pointer initialization due to mismatched types:

  以下是由于類型不匹配而導緻指針初始化無效的示例:

  cptr = // ERROR: cptr can hold a char memory location // (&x is the address of an int)

  Even though the C compiler may allow this type of assignment (with a warning about incompatible types), the behavior of accessing and modifying x through cptr will likely not behave as the programmer expects. Instead, the programmer should use an int * variable to point to an int storage location.

  盡管C編譯器可能允許這種類型的賦值(帶有關于不兼容類型的警告),但通過cptr訪問和修改x的行為可能不會像程序員預期的那樣。相反,程序員應該使用int*變量來指向int存儲位置。

  All pointer variables can also be assigned a special value, NULL, which represents an invalid address. While a null pointer (one whose value is NULL) should never be used to access memory, the value NULL is useful for testing a pointer variable to see if it points to a valid memory address. That is, C programmers will commonly check a pointer to ensure that its value isn’t NULL before attempting to access the memory location to which it points. To set a pointer to NULL:

  還可以為所有指針變量分配一個特殊值NULL,該值表示無效地址。雖然null指針(其值為null)不應用于訪問内存,但null值對于測試指針變量以查看其是否指向有效的内存地址非常有用。也就是說,C程序員通常會在嘗試訪問指針所指向的内存位置之前檢查指針,以确保其值不為NULL。要将指針設置為NULL:

  ptr = NULL; cptr = NULL;

  數形結合:

  用指針變量的處理方法編寫c語言(使用指針變量和文件指針變量的基本步驟)(3)

  Initialize ptr and cptr to NULL.

  将ptr和cptr初始化為NULL。

  Any pointer can be given the special value NULL, which indicates that it doesn’t refer to any particular address. Null pointers should never be dereferenced.

  任何指針都可以被賦予特殊值NULL,這表明它沒有引用任何特定的地址。空指針永遠不應取消引用。

  1.3 Use the pointer variable 使用指針變量

  Finally, use the pointer variable: the dereference operator (*) follows a pointer variable to the location in memory that it points to and accesses the value at that location:

  最後,使用指針變量:解引用運算符(*)跟随指針變量,指向内存中它所指向的位置,并訪問該位置的值:

  /* Assuming an integer named x has already been declared, this code sets the value of x to 8. */ ptr = /* initialize ptr to the address of x (ptr points to variable x) */ *ptr = 8; /* the memory location ptr points to is assigned 8 */

  Dereference ptr to access the memory it points to (x, whose value is 8).

  解引用ptr以訪問其指向的内存(x,其值為8)。

  數形結合:

  用指針變量的處理方法編寫c語言(使用指針變量和文件指針變量的基本步驟)(4)

  (指針變量的操作可以區分為己操作和他操作,己操作是指指針變量自身的操作,常用于數組中元素指針的地址偏移,用于改變指向。他操作是指針對目标對象的操作,是指針變量的解引用用做左值或右值。)

  2 FILE Pointer Variable 文件指針變量 To read or write a file in C, follow these stetw if (outfile == NULL) { printf(Error: unable to open outfile\n exit(1); }

  The fopen function returns NULL to report errors, which may occur if it’s given an invalid filename or the user doesn’t have permission to open the specified file (e.g., not having write permissions to the output.txt file).

  fopen函數返回NULL以報告錯誤,如果給定的文件名無效或用戶沒有打開指定文件的權限(例如,沒有對output.txt文件的寫入權限),則可能會發生錯誤。

  2.3 Using FILE* pointer variable 使用FILE*指針變量

  Use I/O operations to read, write, or move the current position in the file:

  使用輸入/輸出操作讀取、寫入或移動文件中的當前位置:

  int ch; // EOF is not a char value, but is an int. // since all char values can be stored in int, use int for ch ch = getc(infile); // read next char from the infile stream if (ch != EOF) { putc(ch, outfile); // write char value to the outfile stream }

  2.4 Close the file 關閉文件

  use fclose to close the file when the program no longer needs it:

  當程序不再需要文件時,使用fclose關閉文件:

  fclose(infile); fclose(outfile);

  (對于動态申請的堆内存,使用完後同樣需要使用free()手動釋放。)

  The stdio library also provides functions to change the current position in a file:

  // to reset current position to beginning of file void rewind(FILE *f); rewind(infile); // to move to a specific location in the file: fseek(FILE *f, long offset, int whence); fseek(f, 0, SEEK_SET); // seek to the beginning of the file fseek(f, 3, SEEK_CUR); // seek 3 chars forward from the current position fseek(f, -3, SEEK_END); // seek 3 chars back from the end of the file

  ref

  htt

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2025 - www.tftnews.com All Rights Reserved