1. Distinguish String in C and C++
Almost of us know String in C and how to use some String functions in C. However, we easily confused about String between in C and in C++. This post will clarify some of the differences between String in C and String in C++ and location of String in address memory space.
Table 1. Distinguish String in C and C++
String in C
|
String in C++
|
|
declare
|
·
char s[10] = "hello w";
·
char s1[] = "hello w";
·
char s2[10]={'h','e','l',
'l','o','
','w'};
·
char *s3 = "hello w";
·
char *s4=(char*)malloc
(10*sizeof(int));
·
char s5[10][10] //array
·
char *s6[10] //array
|
·
same C
·
<string>
string
s1=”hello w”;
string
s2;
string
s3[10]; //array
|
stdin
|
·
char s[10];
__purge(stdin);
gets(s);
|
·
string s;
getline(cin,s)
|
function
|
<string.h>
· strlen(s)
·
strcpy(s1,s2)
·
strcat(s1,s2)
…
|
<string>
· s.length()
·
s1 = s2
·
s1 = s1 + s2;
…
|
address
|
char
s[10]= “hello w”
·
&s 0x7fff04217b20
·
&s[0] 0x7fff04217b20
·
&s[1] 0x7fff04217b21
·
&s[2] 0x7fff04217b22
char *s1
= “hello w”;
·
&s1[] 0x7fff69713258
·
&s1[0] 0x4008cc
·
&s1[1] 0x4008cd
·
&s1[2] 0x4008ce
char s2=
(char*)malloc(10*sizeof(char));
·
&s2
0x7fff0c79c6d0
·
&s2[0] 0x2050010
·
&s2[1] 0x2050011
·
&s2[2] 0x2050012
|
string
s = “hello w”;
·
&s 0x7fff27ba2db0
·
&s[0] 0x1b0b088
·
&s[1] 0x1b0b089
·
&s[2] 0x1b0b08a
|
Table 1 illustrates some important information which we need to understand about String.
This will be explained below
C supports "string.h" library, difference from "string" library in C++, String in C is char type array .There are two difference types of String declared in C
- Static char type array
char s[10] = "hello world"; char s1[] = "hello "; char s2[10]={'h','e','l','l','o',' ','w', 'o'}; char s4[]={'h','e','l','l','o',' ','w', 'o'};
- Dynamic char type array
char *s3 = "hello w"; //we can't change size of s3 char *s5; s5= (char*)malloc(10*sizeof(char)); sprintf(s5,"hello world");In this case, we use a pointer and the declared String is allocated in Heap partition of address memory space.
We can change the size of String by dynamic allocation so array size can not be known before declaring.
However, when we declare char *s3 = "hello w"; we can't change the size of s3 because it means pointer s3 points to a memory section with content "hello w", which is not dynamic memory allocation. So if we use some functions: strcat(s3, "hello") or strcpy(s, "hello world"), "segmentation fault" error will cause.
However, when we declare char *s3 = "hello w"; we can't change the size of s3 because it means pointer s3 points to a memory section with content "hello w", which is not dynamic memory allocation. So if we use some functions: strcat(s3, "hello") or strcpy(s, "hello world"), "segmentation fault" error will cause.
(You can see on table 1, C++ String is same as char *)
2. Some basic functions in handling String in C
size_t strlen(const char *s);this function returns length of s
int strcmp(const char *s1, const char *s2);
this function compares content of s1 and s2, returns:
- 0 if s1 = s2
- <0 if s1 < s2
- >0 if s1 >s2
int strncmp(const char *s1, const char *s2, size_t n);
this function compares the first n characters of s1 and s2
char *strcpy(char *dest, const char *src);
this function copy string pointed by src to the buffer pointed by dest
char *strcnpy(char *dest, const char *src, size_t n);
this function copy first n characters of src to to the buffer pointed by dest
char *strcat(char *dest, const char *src);
this function appends the src string to dest string, over-writing the terminal null byte ('\n') at the end of dest and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result
char *strncat(char *dest, const char *src, size_t n);
this function appends the first n characters of src to dest.
this function appends the src string to dest string, over-writing the terminal null byte ('\n') at the end of dest and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result
char *strncat(char *dest, const char *src, size_t n);
this function appends the first n characters of src to dest.
3. Array of Strings
Declare:
char *s[NUM] = {"hello", "frank", "humax"};
in this declaration, each of s is similar to char *s
if you want to change actual content of each element, we have to use dynamic memory allocation
or use below:
or use below:
char s[3][10] = {"hello", "frank", "humax"};
in this declaration, each of s is similar to char s[SIZE]. elements of array are allocated contiguous memory sections. We can see addresses of elements:
&s[0] 0x7fff0ffa7ab0 &s[1] 0x7fff0ffa7ac1 &s[2] 0x7fff0ffa7ad2we can see the size of address section of each element is larger than the size of it we declared (10 bytes), because in this system, memory is allocated by 16-byte blocks. So in this case, we can append elements some characters without memory error
No comments:
Post a Comment