Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //linux, 64bit, gcc, -Wall -Wextra -Wpedantic -Wshadow
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char a_str[] = "little weasel";
- char u_str[] = "žebenkštis";
- char *p_str;
- int main()
- {
- printf( "%s\n", a_str );
- printf("Length of string a_str = %zu\n", strlen(a_str) );
- printf("sizeof of a_str = %zu\n", sizeof a_str );
- printf("sizeof of *a_str = %zu\n", sizeof *a_str );
- printf("sizeof of a_str[0] = %zu\n\n", sizeof a_str[0] );
- printf( "%s\n", u_str );
- printf("Length of string u_str = %zu\n", strlen(u_str) );
- printf("sizeof of u_str = %zu\n", sizeof u_str );
- printf("sizeof of *u_str = %zu\n", sizeof *u_str );
- printf("sizeof of u_str[0] = %zu\n\n", sizeof u_str[0] );
- p_str = malloc( strlen(a_str) + 1 );
- if ( !p_str )
- {
- printf("Error: cannot allocate memory.\n");
- return 1;
- }
- // copies a_str, including the ending null character
- strcpy( p_str, a_str );
- printf( "%s\n", p_str );
- printf("Length of string p_str = %zu\n", strlen(p_str) );
- printf("sizeof of p_str = %zu\n", sizeof p_str );
- printf("sizeof of *p_str = %zu\n", sizeof *p_str );
- printf("sizeof of p_str[0] = %zu\n\n", sizeof p_str[0] );
- free ( p_str );
- return 0;
- }
- /*
- output:
- little weasel
- Length of string a_str = 13
- sizeof of a_str = 14
- sizeof of *a_str = 1
- sizeof of a_str[0] = 1
- žebenkštis
- Length of string u_str = 12
- sizeof of u_str = 13
- sizeof of *u_str = 1
- sizeof of u_str[0] = 1
- little weasel
- Length of string p_str = 13
- sizeof of p_str = 8
- sizeof of *p_str = 1
- sizeof of p_str[0] = 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement