當前位置:才華齋>計算機>C語言>

C語言指標的長度和型別詳解

C語言 閱讀(1.55W)

指標是C語言的精髓,以下是本站小編搜尋整理的關於C語言指標的長度和型別詳解,對於初學者深入理解C語言程式設計有很好的參考價值,有需要的朋友可以參考一下!想了解更多相關資訊請持續關注我們應屆畢業生考試網!

C語言指標的長度和型別詳解

一般來說,如果考慮應用程式的相容性和可移植性,指標的長度就是一個問題,在大部分現代平臺上,資料指標的長度通常是一樣的,與指標型別無關,儘管C標準沒有規定所有型別指標的長度相同,但是通常實際情況就是這樣。但是函式指標長度可能與資料指標的長度不同。

指標的長度取決於使用的機器和編譯器,例如:在現代windows上,指標是32位或是64位長

  測試程式碼如下:

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

#include<stddef.h>

struct p{

int n;

float f;

};

int main()

{

struct p *sptr;

printf("sizeof *char: %d", sizeof(char*));

printf("sizeof *int: %d", sizeof(int*));

printf("sizeof *float: %d", sizeof(float*));

printf("sizeof *double: %d", sizeof(double*));

printf("sizeof *struct: %d", sizeof(sptr));

return 0;

}

執行結果如下圖所示:

  指標相關的預定義型別:

① size_t:用於安全地表示長度

② ptrdiff_t:用於處理指標算術運算

③ intptr_t:用於儲存指標地址

④ uintptr_t:用於儲存指標地址

分述如下:

  一、size_t型別

size_t 型別是標準C庫中定義的,應為unsigned int,在64位系統中為 long unsigned int。 C語言中,此型別位於標頭檔案stddef.h中。它是一個與機器相關的unsigned型別,其大小足以保證儲存記憶體中物件的大小,它的目的`是提供一種可移植的方法來宣告與系統中可定址的記憶體區域一致的長度:

因為C/C++標準只定義一最低的位數,而不是必需的固定位數。而且在記憶體裡,對數的高位對齊儲存還是低位對齊儲存各系統都不一樣。為了提高程式碼的可移植性,就有必要定義這樣的資料型別。一般這種型別都會定義到它具體佔幾位記憶體等。當然,有些是編譯器或系統已經給定義好的。經測試發現,在32位系統中size_t是4位元組的,而在64位系統中,size_t是8位元組的,這樣利用該型別可以增強程式的可移植性。

size_t型別用作sizeof操作符的返回型別,同時也是很多函式的引數型別,包括malloc和strlen

在宣告例如字元數、或者陣列索引這樣的長度變數時用size_t是好的做法,它經常用於迴圈計數器、陣列索引,有時候還用在指標算術運算上

列印size_t型別的值要小心,這是無符號值,如果選錯格式說明符,可能會得到不可靠的結果,推薦的格式說明符是%zu,在某些情況下可以考慮用%u或%lu替代

  二、ptrdiff_t型別

ptrdiff_t是C99標準庫中定義的一個與機器相關的資料型別,定義在stddef.h這個檔案內。ptrdiff_t型別變數通常用來儲存兩個指標減法操作的結果。

ptrdiff_t通常被定義為long int型別,size_t 是unsigned 型別,而 ptrdiff_t 則是 signed 整型。

這兩種型別的差別體現了它們各自的用途:size_t 型別用於指明陣列長度,它必須是一個正數;ptrdiff_t 型別則應保證足以存放同一陣列中兩個指標之間的差距,它有可能是負數。

#include<stdio.h>

#include<stddef.h>

#include<string.h>

int main(void)

{

char str[] = "Hello world!";

char *pstart = str;

char *pend = str + strlen(str);

ptrdiff_t difp = pend - pstart;

printf("%d", difp);

return 0;

}

  三、intptr_t與uintptr_t型別

intptr_t與uintptr_t型別用來存放指標地址,它們提供了一種可移植且安全的方法宣告指標,而且與系統中使用的指標的長度相同,對於把指標轉化為整數形式很有用。uintptr_t是intptr_t的無符號版本

關於intptr_t的型別定義如下:

/* Types for `void *' pointers. */

#if __WORDSIZE == 64

# ifndef __intptr_t_defined

typedef long int intptr_t;

# define __intptr_t_defined

# endif

typedef unsigned long int uintptr_t;

#else

# ifndef __intptr_t_defined

typedef int intptr_t;

# define __intptr_t_defined

# endif

typedef unsigned int uintptr_t;

#endif

從定義可以看出,intptr_t在不同的平臺是不一樣的,始終與地址位數相同,因此用來存放地址。

概念上, 儘管地址是指標, 記憶體管理常常使用一個無符號的整數型別更好地完成; 核心對待實體記憶體如同一個大陣列, 並且記憶體地址只是一個數組索引. 進一步地, 一個指標容易解引用; 當直接處理記憶體存取時, 你幾乎從不想以這種方式解引用. 使用一個整數型別避免了這種解引用, 因此避免了 bug. 因此, 核心中通常的記憶體地址常常是 unsigned long, 利用了指標和長整型一直是相同大小的這個事實, 至少在 Linux 目前支援的所有平臺上.C99 標準定義了 intptr_t 和 uintptr_t 型別給一個可以持有一個指標值的整型變數

測試程式碼:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <stdint.h>

#include <string.h>

#include <assert.h>

#define ID_STR_LEN 12

#define NAME_STR_LEN 10

typedef struct student

{

char id[ID_STR_LEN];

char name[NAME_STR_LEN];

uint8_t age;

}student;

student * create_student()

{

student *stu = (student *)malloc(sizeof(student));

if (stu == NULL)

return NULL;

memset(stu, 0, sizeof(student));

return stu;

}

void *free_student(student *stu)

{

if (stu)

free(stu);

return 0;

}

static void init_student(student * stu)

{

assert(stu);

const char *id = "2013112210";

const char *name = "Anker";

uint8_t age = 21;

memcpy(stu->id, id, strlen(id));

memcpy(stu->name, name, strlen(name));

stu->age = age;

}

static int handle_student(intptr_t handle)

{

if (handle == 0)

{

return -1;

}

student *stu = (student*)handle;

printf("id: %s", stu->id);

printf("name: %s", stu->name);

printf("age: %u", stu->age);

return 0;

}

int main(void)

{

student *stu;

stu = create_student();

init_student(stu);

//將指標轉換為intptr_t型別

intptr_t handle = (intptr_t)stu;

handle_student(handle);

free_student(stu);

return 0;

}