一会把使用指针的方式传上来
源代码如下:
#include <stdio.h>
#include <stdlib.h>
void Prtf1();//第一种函数调用-直接使用printf函数
void Prtf2();//第二种函数调用-字符数组
void Prtf3();//第三种函数调用-直接使用puts函数
void Prtf4();//第四种函数调用-用数组首地址方式访问
void Prtf5();//第五种函数调用-用指针方式访问
void Prtf6();//第六种函数调用-用指针方式访问(指针的定义赋值不同,区别方法五)
char str[5]= "Hello";
int main()
{
Prtf1();
Prtf2();
Prtf3();
Prtf4();
Prtf5();
Prtf6();
return 0;
}
//第一种函数调用-直接使用printf函数
void Prtf1()
{
printf("Hello\n");
}
//第二种函数调用-字符数组
void Prtf2()
{
int i;
for(i=0; i<5; i++)
{
printf("%c",str[i]);
}
printf("\n");
}
//第三种函数调用-直接使用puts函数
void Prtf3()
{
puts("Hello");
}
//第四种函数调用-用数组首地址方式访问
void Prtf4()
{
int i;
for(i=0; i<5; i++)
{
printf("%c",*(str+i));
}
printf("\n");
}
//第五种函数调用-用指针方式访问
void Prtf5()
{
int i;
char *ptr_str;
for(i=0; i<5; i++)
{
ptr_str = &str[0];//把数组首元素地址给指针
printf("%c",*(ptr_str+i));
}
printf("\n");
}
//第六种函数调用-用指针方式访问(指针的定义赋值不同,区别方法五)
void Prtf6()
{
int i;
char *ptr_str;
for(i=0; i<5; i++)
{
ptr_str = str;//把数组名给指针
printf("%c",*(ptr_str+i));
}
printf("\n");
}