string size problem

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2004
Posts: 31
Reputation: Sukhbir is an unknown quantity at this point 
Solved Threads: 0
Sukhbir Sukhbir is offline Offline
Light Poster

string size problem

 
0
  #1
Sep 10th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: string size problem

 
0
  #2
Sep 10th, 2004
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC