日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言中的void指针,C程序中void指针的概念

發(fā)布時(shí)間:2025/3/20 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言中的void指针,C程序中void指针的概念 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

到目前為止,我對(duì)void指針的輕描淡寫如下。

當(dāng)使用關(guān)鍵字void聲明指針變量時(shí),它將成為通用指針變量。任何數(shù)據(jù)類型(char,int,float等)的任何變量的地址都可以分配給void指針變量。

main()

{

int *p;

void *vp;

vp=p;

}

由于可以將其他數(shù)據(jù)類型的指針分配給void指針,因此我在absolut_value(下面顯示的代碼)函數(shù)中使用了它。使具有一般功能

我試圖編寫一個(gè)簡(jiǎn)單的C代碼,將整數(shù)或浮點(diǎn)數(shù)作為參數(shù),并嘗試使其為+ ve(如果為負(fù))。我寫了下面的代碼,

#include

void absolute_value ( void *j) // works if used float, obviously it must work but thats not my interest here.

{

if ( *j < 0 )

*j = *j * (-1);

}

int main()

{

int i = 40;

float f = -40;

printf("print intiger i = %d \n",i);

printf("print float f = %f \n",f);

absolute_value(&i);

absolute_value(&f);

printf("print intiger i = %d \n",i);

printf("print float f = %f \n",f);

return 0;

}

但是我遇到了錯(cuò)誤,所以我知道我對(duì)void指針的理解是不正確的:(。因此,我現(xiàn)在將著手收集點(diǎn),為什么會(huì)這樣。

我需要了解更多關(guān)于void指針的內(nèi)容。

我們需要強(qiáng)制轉(zhuǎn)換void指針變量以取消引用。這是因?yàn)榭罩羔槢]有與之關(guān)聯(lián)的數(shù)據(jù)類型。編譯器無法知道(或猜測(cè)?)void指針指向的數(shù)據(jù)類型。因此,要獲取由void指針指向的數(shù)據(jù),我們使用在void指針位置內(nèi)保存的正確類型的數(shù)據(jù)進(jìn)行類型轉(zhuǎn)換。

void main()

{

int a=10;

float b=35.75;

void *ptr; // Declaring a void pointer

ptr=&a; // Assigning address of integer to void pointer.

printf("The value of integer variable is= %d",*( (int*) ptr) );// (int*)ptr - is used for type casting. Where as *((int*)ptr) dereferences the typecasted void pointer variable.

ptr=&b; // Assigning address of float to void pointer.

printf("The value of float variable is= %f",*( (float*) ptr) );

}

如果程序員不確定最終用戶輸入的數(shù)據(jù)的數(shù)據(jù)類型,則空指針非常有用。在這種情況下,程序員可以使用void指針指向未知數(shù)據(jù)類型的位置。可以以要求用戶通知數(shù)據(jù)類型的方式來設(shè)置程序,并且可以根據(jù)用戶輸入的信息來執(zhí)行類型轉(zhuǎn)換。下面是一個(gè)代碼段。

void funct(void *a, int z)

{

if(z==1)

printf("%d",*(int*)a); // If user inputs 1, then he means the data is an integer and type casting is done accordingly.

else if(z==2)

printf("%c",*(char*)a); // Typecasting for character pointer.

else if(z==3)

printf("%f",*(float*)a); // Typecasting for float pointer

}

關(guān)于空指針,您應(yīng)記住的另一個(gè)重要點(diǎn)是–不能在空指針中執(zhí)行指針?biāo)阈g(shù)。

void *ptr;

int a;

ptr=&a;

ptr++; // This statement is invalid and will result in an error because 'ptr' is a void pointer variable.

所以現(xiàn)在我明白了我的錯(cuò)誤。我正在更正。

參考文獻(xiàn):

http://www.antoarts.com/void-pointers-in-c/

http://www.circuitstoday.com/void-pointers-in-c。

新代碼如下所示。

#include

#define INT 1

#define FLOAT 2

void absolute_value ( void *j, int *n)

{

if ( *n == INT) {

if ( *((int*)j) < 0 )

*((int*)j) = *((int*)j) * (-1);

}

if ( *n == FLOAT ) {

if ( *((float*)j) < 0 )

*((float*)j) = *((float*)j) * (-1);

}

}

int main()

{

int i = 0,n=0;

float f = 0;

printf("Press 1 to enter integer or 2 got float then enter the value to get absolute value\n");

scanf("%d",&n);

printf("\n");

if( n == 1) {

scanf("%d",&i);

printf("value entered before absolute function exec = %d \n",i);

absolute_value(&i,&n);

printf("value entered after absolute function exec = %d \n",i);

}

if( n == 2) {

scanf("%f",&f);

printf("value entered before absolute function exec = %f \n",f);

absolute_value(&f,&n);

printf("value entered after absolute function exec = %f \n",f);

}

else

printf("unknown entry try again\n");

return 0;

}

總結(jié)

以上是生活随笔為你收集整理的c语言中的void指针,C程序中void指针的概念的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。