How to use char type pointer

Assign value to a char pointer


#include <stdio.h>
/* w w  w .  j a  va  2  s . co  m*/
int main(void)
{
  char *p;

  p = "one two three";

  printf(p);

  return 0;
}

Our own string copy function based on pointer.


#include <stdio.h>
//  w w w .  j a va2 s . c  om
void mycpy(char *to, char *from);

int main(void)
{
  char str[80];

  mycpy(str, "this is a test");
  printf(str);

  return 0;
}

void mycpy(char *to, char *from)
{
  while(*from) 
      *to++ = *from++;
  
  *to = '\0'; /* null terminates the string */
}

Reverse string content using its pointer


#include <stdio.h>
#include <string.h>
//  w  w w .  j  ava  2s  .  c om
int main(void)
{
  char str1[] = "Pointers are fun and hard to use";
  char str2[80], *p1, *p2;

  /* make p point to end of str1 */
  p1 = str1 + strlen(str1) - 1;

  p2 = str2;

  while(p1 >= str1)
    *p2++ = *p1--;

  /* null terminate str2 */
  *p2 = '\0';

  printf("%s %s", str1, str2);

  return 0;
}




















Home »
  C Language »
    Language Advanced »




File
Function definition
String
Pointer
Structure
Preprocessing