RELATION BETWEEN STRING
AND POINTERS
STRINGS IN C
• String in C programming is a sequence of characters terminated
with a null character‘0’.
• Strings are defined as an array of characters.
Declaration
char str_name[size];
EXAMPLE FOR STRING
• #include <stdio.h>
• int main()
• {
• char name[20];
• printf(“Enter name:“);
• scanf(“%s”, name);
• printf(“Your name is %s.”, name);
• return 0;
• }
POINTERS IN C
•A pointer is a variable that stores the memory
address of another variable as its value.
•In other words, it points to the location of a variable
and can indirectly access the variable.
• Pointers are declared using the *symbol.
• * => Asterisk
•Declaration
•int *ptr;
EXAMPLE FOR POINTERS
• #include<stdio.h>
• int main()
• {
• int* p, c, d;
• c = 5;
• d = -15;
• p = &c; printf(“%d”, *p); // Output: 5
• p = &d; printf(“%d”, *p); // Ouptut: -15
• return 0;
• }
EXAMPLE
• #include <stdio.h>
• int main(void)
• {
• char *strPtr = “Hello”;
• char *t = strPtr;
• while(*t != ‘0’)
• {
• printf(“%c”, *t);
• t++;
• }
• return 0;
• }
c ppt.pdf
c ppt.pdf

c ppt.pdf

  • 1.
  • 2.
    STRINGS IN C •String in C programming is a sequence of characters terminated with a null character‘0’. • Strings are defined as an array of characters. Declaration char str_name[size];
  • 3.
    EXAMPLE FOR STRING •#include <stdio.h> • int main() • { • char name[20]; • printf(“Enter name:“); • scanf(“%s”, name); • printf(“Your name is %s.”, name); • return 0; • }
  • 4.
    POINTERS IN C •Apointer is a variable that stores the memory address of another variable as its value. •In other words, it points to the location of a variable and can indirectly access the variable. • Pointers are declared using the *symbol. • * => Asterisk •Declaration •int *ptr;
  • 5.
    EXAMPLE FOR POINTERS •#include<stdio.h> • int main() • { • int* p, c, d; • c = 5; • d = -15; • p = &c; printf(“%d”, *p); // Output: 5 • p = &d; printf(“%d”, *p); // Ouptut: -15 • return 0; • }
  • 6.
    EXAMPLE • #include <stdio.h> •int main(void) • { • char *strPtr = “Hello”; • char *t = strPtr; • while(*t != ‘0’) • { • printf(“%c”, *t); • t++; • } • return 0; • }