c語言用指針定義一個類型進行輸入輸出


1 整型數組

//

#include "stdafx.h"
#include "stdlib.h"

int _tmain(int argc, _TCHAR* argv[])
{
int *q=(int*)malloc(sizeof(int)*5);   //malloc函數動態分配5個整型數的地址空間。

printf("Please input:");

for(int i=0;i<5;i++)

scanf("%d",q+i);  

for(int i=0;i<5;i++) 
 
printf("%d ",*(q+i));
    system("pause");
    return 0;
}

2 定義有一個字符串進行輸入輸出

#include "stdafx.h"
#include "stdlib.h"

int _tmain(int argc, _TCHAR* argv[])
{
char *str=(char*)malloc(100);   //malloc函數動態分配地址空間。

printf("Please input:");

scanf("%s",str);   


printf("%s",str);   //注意 不用加 *
    system("pause"); 
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM