November 2013
Beginner
325 pages
9h 47m
English
If you were dealing with C strings a lot, malloc-ing the memory and stuffing the characters in one by one would be a real pain. Instead, you can create a pointer to a string of characters (terminated with the zero character) by putting the string in quotes. Change your code to use a string literal:
int main (int argc, const char * argv[])
{
char x = '!'; // The character '!'
while (x <= '~') { // The character '~'
printf("%x is %c\n", x, x);
x++;
}
char *start = "Love";
printf("%s has %zu characters\n", start, strlen(start));
printf("The third letter is %c\n", start[2]);
return 0;
}
Build it and run it.
Notice that you do not need to malloc and free memory for a string literal. It is a constant and appears in memory ...
Read now
Unlock full access