thenewboston

C Programming Tutorial - 45 - Strings and Pointers

Aug 22nd, 2014
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. int main()
  8. {
  9.     char movie1[] = "The Return of Bucky Dude!";
  10.     char * movie2 = "The Wolfman from Dirt Swamp!";
  11.  
  12.     // array names are constants, can’t be changed
  13.     // array names point to the first elements address
  14.     // can't put arrays on the left hand side of an equals sign
  15.     // changing it will mess up the entire array, won't know where any other elements are
  16.     // to change it, we need to use strcpy() or change each individual element
  17.     // movie1 = "hey now";
  18.     // movie1[] = "hey now";
  19.  
  20.     // you CAN put pointers on the left hand side
  21.     // movie2 can't hold characters or strings, only addresses
  22.     // C is going to put the string into memory somewhere, and then give you it's address
  23.     // just saying start at this address, end when it gets to null 0
  24.     movie2 = "brown cow";
  25.  
  26.     // now we can work with strings, just like they are any other variable!
  27.     puts(movie1);
  28.     puts(movie2);
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment