1 / 10

Strings and Pointers in C

Charles Clute Tom Most Michael Hein. Strings and Pointers in C. Strings in C. There is no String . . . But there’s hope!. Strings are character arrays. char volume[6]; char volume[6] = "STRG01"; char volume[] = "STRG01";. Creating New Strings. Creating garbage strings

tab
Télécharger la présentation

Strings and Pointers in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Charles Clute Tom Most Michael Hein Strings and Pointers in C

  2. Strings in C • There is no String . . . But there’s hope! Strings are character arrays char volume[6]; char volume[6] = "STRG01"; char volume[] = "STRG01";

  3. Creating New Strings • Creating garbage strings • Creating strings with given characters • Creating strings with given characters and size • Creating a pointer to a string (dynamic allocation) char str[6]; char str[6] = "STRG01"; char str[] = "STRG01"; char* str = string_name; // no & necessary

  4. Characters in strings can be created in two ways: integral values and character literals (surrounded by “'”) • Strings in C end at the first null byte (use strlen() to get the length) • Null character: str[0] = 'S';str[0] = 0xE2; // hex literals are handy char zerobyte = '\0'; // escape characterchar zerobyte = 0; // or 0x00 hex

  5. String variables have an implied pointer which has special properties: • It has the string name (no subscripts) • It is a constant (you can't alter its value) • The sizeof operator returns the size of the array, not that of a pointer.

  6. Strings can be looped though in two different ways: using subscripts and using pointers. Subscript: inti; char str[6]; for (i = 0; i < sizeof(volume); i++) str[i] = '0'; Pointer: char str[6]; char *ptr; for (i = 0, ptr = str; i < sizeof(volume); ptr++, i++) *ptr = i;

  7. strtok() • Syntax:#include <string.h>char* strtok(char *str1, const char *str2); • The strtok() function returns a pointer to the next "token" in str1, where str2 contains the delimiters that determine the token.

  8. strstr() • Syntax:#include <string.h>char *strstr(const char *haystack, const char *needle); • The function strstr() returns a pointer to the first occurrence of needle in haystack, or NULL if no match is found.

  9. Other String Functions • strcpy() — copies one string to another (buffer overflow, ahoy!) • strcat() — appends one string to another • strchr() — finds a character in a string (there are several others in this vein) • strlen() — finds the length of the string (slow!) • memcpy() — copies blocks of memory. Fast and safe. • memmove() — like memcpy(), but works if the blocks overlap

  10. Pointers • Function Pointers • When dereferenced, a function pointer invokes a function, passing it zero or more arguments just like a normal function. • Syntax: void (*foo)(int a);foo = my_int_func; // (no &) • Void Pointers • A pointer to void is a generic pointer that can be used to hold an address, but cannot be 'deferenced': that is to say, you can't use it with an asterisk before it to update a variable.

More Related