| | |
string size problem
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
•
•
Originally Posted by Sukhbir
Can someone explain me
how these character or escape sequence are treated while finding string size(through sizeof() operator)
\0 \n \a \1 \2
for example char str[]="\1abc\0\0"
pls explain me in detailed
#include <stdio.h>
#include <string.h>
void foo(const char *s, size_t size)
{
size_t i;
printf("size = %d\n", (int)size);
printf("strlen(s) = %d\n", (int)strlen(s));
for(i = 0; i < size; ++i)
{
printf("s[%d] = %d\n", (int)i, s[i]);
}
}
int main(void)
{
char a[] = "\0\n\a\1\2"; /* equivalent to: {0,'\n','\a','\1','\2',0} */
char b[] = "\1abc\0\0"; /* equivalent to: {'\1','a','b','c',0,0,0} */
foo(a, sizeof a);
foo(b, sizeof b);
return 0;
}
/* my output
size = 6
strlen(s) = 0
s[0] = 0
s[1] = 10
s[2] = 7
s[3] = 1
s[4] = 2
s[5] = 0
size = 7
strlen(s) = 4
s[0] = 1
s[1] = 97
s[2] = 98
s[3] = 99
s[4] = 0
s[5] = 0
s[6] = 0
*/ Last edited by Dave Sinkula; Sep 10th, 2004 at 10:36 am. Reason: Added color.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Linked List: Mp3 Menu (String and Delete problem) (C++)
- string.size and accentuated words (C++)
- std::string::assign Problem (C++)
- string size problem (C)
Other Threads in the C Forum
- Previous Thread: Source File Too Big
- Next Thread: needed big time hw due and late
| Thread Tools | Search this Thread |
#include * adobe ansi append array arrays asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv research reversing scripting segmentationfault sequential single socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






