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;
}
相關文章
相關標籤/搜索