AM22 Tech
»
Technical Discussions
»
C/C++ Discussions
»
Sizeof array and string (in C language)?
 Rank: Advanced Member
Joined: 2/16/2010(UTC) Posts: 30
|
Sizeof array and string (in C language)? lets say i have the following program: String s1 = "Hello"; char[] s2 = {'H', 'e', 'l', 'l', 'o'}; and i try to find sizeof s1 and sizeof s2, what would be returned? will both get the same answer, 5? Edited by user Thursday, March 11, 2010 4:28:21 AM(UTC)
| Reason: Not specified
|
|
|
|
|
|
|
|
|
|
 Rank: Administration
Joined: 1/9/2010(UTC) Posts: 449  Location: India Was thanked: 1 time(s) in 1 post(s)
|
1) SizeOf(s1) will give you 6 2) SizeOf(s2) will give you 5.
1) Sizeof function will count the number of characters (5) + the null terminated character at the end of every string (1), hence answer is 6 . 2) As s2 is a character array and NOT a string, the sizeOf function will count the number of elements in the array and hence will return 5.
|
|
|
|
|
|
AM22 Tech
»
Technical Discussions
»
C/C++ Discussions
»
Sizeof array and string (in C language)?
Forum Jump
You can post new topics in this forum.
You can reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.