Google News
logo
C Program to Print String using Pointer
In the following  example of a C program that prints a string using a pointer :
Program :
#include <stdio.h>

int main() {
    char str[] = "Hello, world!";
    char *ptr;

    ptr = str;

    while (*ptr != '\0') {
        printf("%c", *ptr);
        ptr++;
    }

    return 0;
}
Output :
Hello, world!
* In this example, the program creates a character array str that contains the string "Hello, world!".

* The program then creates a character pointer ptr and initializes it to point to the first character of str.

* Next, the program uses a while loop to iterate through each character in str by dereferencing the pointer ptr to get the value pointed to by ptr, printing the value using printf, and incrementing the pointer to point to the next character in the array. The loop continues until the null character \0 is encountered, indicating the end of the string.