Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char* ptr;
- strcpy(ptr, "hello world"); // crash here!
- char* ptr;
- scanf("%s", ptr); // crash here!
- {
- int data = 0;
- int* ptr = &data;
- ...
- }
- int* ptr = malloc(sizeof(int));
- /*** examples of incorrect use of pointers ***/
- // 1.
- int* bad;
- *bad = 42;
- // 2.
- char* bad;
- strcpy(bad, "hello");
- /*** examples of correct use of pointers ***/
- // 1.
- int var;
- int* good = &var;
- *good = 42;
- // 2.
- char* good = malloc(5+1); // allocates memory for 5 characters and 1 terminator
- strcpy(good, "hello");
- int* p1 = NULL; // pointer to nowhere
- int* p2; // uninitialized pointer, pointer to "anywhere", cannot be used yet
- char * strcpy ( char * destination, const char * source );
- char* ptr = malloc(32);
- strcpy(ptr, "hello world");
- char str[32];
- strcpy(str, "hello world");
- char *ptr = malloc(32);
- scanf("%31[^n]", ptr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement