输出打印数组---printf函数

365娱乐app官方版下载 2025-07-22 17:19:52 admin 218 562
输出打印数组---printf函数

输出打印数组---printf函数

printf(“%s\n”,str);语句可将字符指针所指的字符串一次性的输出,除了字符类型,其他类型的数组不能用数组名来整体性的输出它的全部元素,只能逐个元素输出,用for循环

下面我们来一个验证:

#include

#include

void f(char **p)

{

*p += 2;

}

int main()

{

char *a[] = { "123", "456", "7891" };

int b[] = { 1, 2, 3, 4, 5, 6 };

char **p;

p = a;

f(p);

//printf("%s", *p);

printf("%s", b);//若将非字符型数组用%s这种形式输出,则我们编译没问题

system("pause");

return 0;

}

以上代码编译成功

运行后为:

以下为测试代码,关于字符串和数组的输出:

#include

#include

void f(char **p)

{

*p += 2;

}

int main()

{

char *a[] = { "123", "456", "7891" };

int b[6] = { 1, 2, 3, 4, 5, 6 };

char *c ="abcdef";

char d[4] = { '1', '2', '6', '5' };

char **p;

p = a;

f(p);

printf("*p: %s\n", *p);

printf("b数组:");

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

{

printf(" %d ", b[i]);

}

printf("\n");

printf("指针变量c: %s \n", c);

printf(": %s\n", d);

system("pause");

return 0;

}

相关推荐