Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a pointer to a string be used in a printf?

Tags:

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?

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.

like image 533
andreihondrari Avatar asked Jul 23 '11 09:07

andreihondrari


People also ask

Can you have a pointer to a string?

A pointer to string in C can be used to point to the base address of the string array, and its value can be dereferenced to get the value of the string. To get the value of the string array is iterated using a while loop until a null character is encountered.

Can you printf a pointer?

You can print a pointer value using printf with the %p format specifier. To do so, you should convert the pointer to type void * first using a cast (see below for void * pointers), although on machines that don't have different representations for different pointer types, this may not be necessary.

How do I print a string using character pointer?

The first parameter to printf must be a const char* (a char* can convert implicitly to a const char* ) and points to the start of a string of characters. It stops printing when it encounters a \0 in that string. If there is not a \0 present in that string then the behaviour of that function is undefined.

Can we print string using printf?

We can print the string using %s format specifier in printf function. It will print the string from the given starting address to the null '\0' character. String name itself the starting address of the string. So, if we give string name it will print the entire string.


2 Answers

The "%s" format specifier for printf always expects a char* argument.

Given:

char s[] = "hello"; char *p = "world"; printf("%s, %s\n", s, p); 

it looks like you're passing an array for the first %s and a pointer for the second, but in fact you're (correctly) passing pointers for both.

In C, any expression of array type is implicitly converted to a pointer to the array's first element unless it's in one of the following three contexts:

  • 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.

(I think C++ has one or two other exceptions.)

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.

Section 6 of the comp.lang.c FAQ has an excellent discussion of this.

like image 81
Keith Thompson Avatar answered Sep 30 '22 13:09

Keith Thompson


printf("%s\n", ptr); 

Is this what you want?

By the way, from printf(3), here's the documentation for the s conversion specifier (i.e %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.

like image 35
Bertrand Marron Avatar answered Sep 30 '22 12:09

Bertrand Marron