C语言 可以在 printf 中使用指向字符串的指针吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6799470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a pointer to a string be used in a printf?
提问by andreihondrari
I am thinking of something like:
我在想类似的事情:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(void) {
//test pointer to string
char s[50];
char *ptr=s;
printf("\nEnter string (s): ");
fgets(s, 50, stdin);
printf("S: %s\nPTR: %s\n", s, *ptr);
system("PAUSE");
return 0;
}
Or should I use a for loop with *(s+i) and the format specifier %c? Is that the only possible way to print a string through a pointer and a simple printf?
还是应该使用带有 *(s+i) 和格式说明符 %c 的 for 循环?这是通过指针和简单的 printf 打印字符串的唯一可能方法吗?
Update: The printf operates with the adress of the first element of the array so when I use *ptr I actually operate with the first element and not it's adress. Thanks.
更新: printf 使用数组第一个元素的地址,所以当我使用 *ptr 时,我实际上使用第一个元素而不是地址。谢谢。
回答by Keith Thompson
The "%s"format specifier for printfalwaysexpects a char*argument.
该"%s"格式说明符printf总是需要一个char*说法。
Given:
鉴于:
char s[] = "hello";
char *p = "world";
printf("%s, %s\n", s, p);
it lookslike you're passing an array for the first %sand a pointer for the second, but in fact you're (correctly) passing pointers for both.
它看起来像你传递的第一阵列%s和第二的指针,但实际上你(正确地)传递了两个指针。
In C, any expression of array type is implicitly converted to a pointer to the array's first element unlessit's in one of the following three contexts:
在 C 中,任何数组类型的表达式都会隐式转换为指向数组第一个元素的指针,除非它位于以下三个上下文之一中:
- It's an argument to the unary "&" (address-of) operator
- It's an argument to the unary "sizeof" operator
- It's a string literal in an initializer used to initialize an array object.
- 它是一元“&”(寻址)运算符的参数
- 这是一元“sizeof”运算符的参数
- 它是用于初始化数组对象的初始化程序中的字符串文字。
(I think C++ has one or two other exceptions.)
(我认为 C++ 有一两个例外。)
The implementation of printf()sees the "%s", assumes that the corresponding argument is a pointer to char, and uses that pointer to traverse the string and print it.
的实现printf()sees "%s",假设相应的参数是一个指向 char 的指针,并使用该指针遍历字符串并打印它。
Section 6 of the comp.lang.c FAQhas an excellent discussion of this.
comp.lang.c FAQ 的第 6 节对此进行了很好的讨论。
回答by Bertrand Marron
printf("%s\n", ptr);
Is this what you want?
这是你想要的吗?
By the way, from printf(3), here's the documentation for the sconversion specifier (i.e %s):
顺便说一下,从printf(3),这里是s转换说明符(即%s)的文档:
If no l modifier is present: The const char *argument is expected to be a pointer to an array of character type (pointer to a string). Characters from the array are written up to (but not including) a terminating null byte ('\0'); if a precision is specified, no more than the number specified are written. If a precision is given, no null byte need be present; if the precision is not specified, or is greater than the size of the array, the array must contain a terminating null byte.
如果不存在 l 修饰符:const char *参数应该是指向字符类型数组的指针(指向字符串的指针)。数组中的字符被写入(但不包括)终止空字节('\0');如果指定了精度,则写入的数字不超过指定的数字。如果给出精度,则不需要出现空字节;如果未指定精度,或大于数组的大小,则数组必须包含终止空字节。
回答by arthur
you should do "printf("S: %s\nPTR: %s\n", s, ptr); " instead of printf("S: %s\nPTR: %s\n", s, *ptr);
你应该做 "printf("S: %s\nPTR: %s\n", s, ptr); " 而不是 printf("S: %s\nPTR: %s\n", s, *ptr);
difference between ptr and *ptr is: ptrgives you the addressin the memory of the variable you are pointing to and *ptr gives rather the valueof the pointed variable In this case is *ptr = ptr[0]
ptr 和 *ptr 之间的区别是:ptr为您提供指向的变量在内存中的地址,而 *ptr 提供的是指向变量的值在这种情况下是 *ptr = ptr[0]
this code will show what i mean:
这段代码将显示我的意思:
printf("\tS: %s\n\tPTR: %s\n\tAddress of the pointed Value: %x\n\tValue of the whole String: %s\n\tValue of the first character of the String: %c\n", s, ptr,ptr,ptr,*ptr);
回答by MAIGA
In my experience you should get segmentation fault when you try to use %s directive with *p.
根据我的经验,当您尝试将 %s 指令与 *p 一起使用时,您应该会遇到分段错误。

