Advertisement
ssoni

pointers.c

Mar 18th, 2022
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. int main(void)
  7. {
  8.  
  9.  
  10.     int age = 21;
  11.  
  12.     printf("Address of age is <%p>\n", &age);
  13.  
  14.     int *ip;
  15.     ip = &age;
  16.  
  17.     printf("Value at address pointed to by *ip is <%i>\n", *ip);
  18.  
  19.     char name[20];
  20.     char *p;
  21.  
  22.     //p = &name[0];
  23.     p = name;
  24.  
  25.     printf("Address of name is <%p>\n", &name);
  26.  
  27.     //populate the "string" (char array)
  28.     name[0]  = 'S';
  29.     name[1]  = 'i';
  30.     name[2]  = 'd';
  31.     name[3]  = '\0';
  32.  
  33.     //PRINT THE STRING (ARRAY)
  34.     for (p = name; *p != '\0'; p++ )
  35.     {
  36.         printf("%c", *p);
  37.     }
  38.     printf("\n");
  39.  
  40.  
  41.  
  42.     //Alternate method of popuating array
  43.     char name2[20];
  44.     char *p2;
  45.  
  46.     p2 = name2;
  47.     *p2 = 'S';
  48.     p2++;
  49.     *p2++ = 'i';
  50.     *p2++ = 'd';
  51.     *p2++ = '\0';
  52.  
  53.     //PRINT THE STRING (ARRAY)
  54.     for (p2 = name2; *p2 != '\0'; p2++ )
  55.     {
  56.         printf("%c", *p2);
  57.     }
  58.     printf("\n");
  59.  
  60.     char str[]="Hello Guru99!";
  61.     char *p3;
  62.  
  63.     p3=str;
  64.     printf("First character is:%c\n",*p3);
  65.     p3=p3+1;
  66.     printf("Next character is:%c\n",*p3);
  67.     printf("Printing all the characters in a string\n");
  68.     p3=str;  //reset the pointer
  69.     for(int i=0;i<strlen(str);i++)
  70.     {
  71.         printf("%c",*p3);
  72.         p3++;
  73.     }
  74.     printf("\n");
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement