Access strings using pointers

www.l‮ttua‬uri.com
Access strings using pointers

To access and print string elements, we can use loops and check for the '0' null character.

In the following example, we are using a 'while' loop to print the character 'str' for a string variable.

#include <stdio.h>
int main(void) {
    
  //string variable
  char str[6] = "Hello";
  
  //pointer variable
  char *ptr = str;
  
  //print the string
  while(*ptr != '\0') {
    printf("%c", *ptr);
    
    //move the ptr pointer to the next memory location
    ptr++;
  }
  
  return 0;
}
Created Time:2017-08-28 06:29:14  Author:lautturi