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;
}

猜你喜欢

转载自www.cnblogs.com/mhq-martin/p/11392106.html