freelanceprogrammers.org Forum Index » C

REG SIZEOF


View user's profile Post To page top
win_truth Posted: Mon Aug 18, 2003 10:45 am


Joined: 25 Jul 2003

Posts: 4
REG SIZEOF
HI ALL,
 
in the below code
 
int main(){   char s[5];
   strcpy(s,"ABC");   printf("%d %d
",strlen(s),sizeof(s)); o/p 3 5}
int main()
 {
   char s="ABC";
 printf("%d %d
",strlen(s),sizeof(s)); o/p 3 4
}
 
 y different values is printed for sizeof() though the string is same
 
 
with reg
 ashwini
 
Win TVs, Bikes, DVD players and more!
Click on
Yahoo! India Promos
Reply with quote
Send private message
View user's profile Post To page top
shantanu_k06 Posted: Mon Aug 18, 2003 9:51 pm


Joined: 19 Jul 2003

Posts: 29
REG SIZEOF
--- suganthi rams <ashvinistar@...> wrote:
> HI ALL,
>
> in the below code
>
> int main()
> {
> char s[5];
> strcpy(s,"ABC");
> printf("%d %d
",strlen(s),sizeof(s)); o/p 3 5
> }
>
> int main()
> {
> char s="ABC";
> printf("%d %d
",strlen(s),sizeof(s)); o/p 3 4
> }
>
> y different values is printed for sizeof() though
> the string is same

Trying to obtain strlen(s) when s is a char (not
char[], mind you), should give you a runtime (pointer)
error. Maybe a segmentation fault.

strlen() is a function that returns you the length of
a string, i.e. the number of characters held in a
string.

On the other hand, sizeof() is a built-in, which
yields the total number of bytes occupied by the
variable itself, not its content. For instance, in the
first case, the strlen() is 3 which is fine, and
sizeof() is 5 which is fine too. However, in second
case, strlen should serve you a nice memory access
error, however sizeof() should feel comfortable giving
you a value of 1 (on systems where char is 1 byte
long, ofcourse).

HTH.

Regards,
Shantanu


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
Reply with quote
Send private message
View user's profile Post To page top
c_mallircr Posted: Tue Aug 19, 2003 11:05 am


Joined: 11 Jul 2003

Posts: 2
REG SIZEOF
Hi Ashwni,

int main(){   char s[5];                       // Allocates 5 bytes for s
   strcpy(s,"ABC");            // Copies only 4 bytes to s ( 3 chars and 1 ``   printf("%d %d
",strlen(s),sizeof(s)); o/p 3 5}
int main()
 {
   char s="ABC";     // it should be char *s="ABC";
 printf("%d %d
",strlen(s),sizeof(s)); o/p 3 4 // Here sizeof(s) is the sizeof char * but not sizeof s. so its 4 bytes. Any pointer variables will have 4 bytes in size. ex int *intPtr also 4 bytes in size.
}
 
Hope u understood
Regards,
Mallikarjunasuganthi rams <ashvinistar@...> wrote:


HI ALL,
 
in the below code
 
int main(){   char s[5];
   strcpy(s,"ABC");   printf("%d %d
",strlen(s),sizeof(s)); o/p 3 5}
int main()
 {
   char s="ABC";
 printf("%d %d
",strlen(s),sizeof(s)); o/p 3 4
}
 
 y different values is printed for sizeof() though the string is same
 
 
with reg
 ashwiniMallikarjuna CSoftware EngineerMindTree Consulting Pvt. Ltd.BangalorePh: 671 1777 / 671 2777 Extn 1514
Win TVs, Bikes, DVD players and more!
Click on
Yahoo! India Promos
Reply with quote
Send private message
View user's profile Post To page top
PankajP@... Posted: Tue Aug 19, 2003 5:41 pm


Joined: 25 Jul 2003

Posts: 3
REG SIZEOF
In first case it is taking the size of arrray which is 5,
whereas in second case it is taking the size of String
which always terminates with a "
" character at the end.
hence its 4.suganthi rams <ashvinistar@...> wrote:



HI ALL,
 
in the below code
 
int main(){   char s[5];
   strcpy(s,"ABC");   printf("%d %d
",strlen(s),sizeof(s)); o/p 3 5}
int main()
 {
   char s="ABC";
 printf("%d %d
",strlen(s),sizeof(s)); o/p 3 4
}
 
 y different values is printed for sizeof() though the string is same
 
 
with reg
 ashwini
 
Reply with quote
Send private message
View user's profile Post To page top
sflam108 Posted: Tue Aug 19, 2003 10:13 pm


Joined: 25 Jul 2003

Posts: 48
REG SIZEOF
See comments embedded…
 
-----Original Message-----
From: Pankaj Panchal , Gurgaon
[mailto:PankajP@...]
Sent: Tuesday, August
19, 2003 07:41
To: Programmers-Town@yahoogroups.com
Subject: RE:
*(&Programmers-Town&)* REG SIZEOF
 


In first case it
is taking the size of arrray which is 5,

 
That is correct.
 

whereas in second
case it is taking the size of String
which always
terminates with a "
" character at the end.
hence its 4.

 
Are you saying that the string literal (“ABC”)
has an embedded ‘
’ in it?  I wonder where did you get the
idea from…
 
To OP:  The second case would not
even compiled unless you change ‘s’ to char*:
   
char *s = "ABC";
 
Then, sizeof(s) is simply the size of the
pointer, which happens to be 4 in your environment.
 
HTH
Shyan
 

suganthi rams
<ashvinistar@...> wrote:






HI ALL,




 




in the below code




 




int main()
{
   char s[5];




  
strcpy(s,"ABC");
   printf("%d %d
",strlen(s),sizeof(s)); o/p 3 5
}




int main()




 {




   char
s="ABC";




 printf("%d
%d
",strlen(s),sizeof(s)); o/p 3 4




}




 




 y different values
is printed for sizeof() though the string is same




 




 




with reg




 ashwini




 
Reply with quote
Send private message
View user's profile Post To page top
ashok_nalla2002 Posted: Sat Aug 23, 2003 10:33 am


Joined: 23 Aug 2003

Posts: 1
REG SIZEOF
dear friends,
    hope all of u r fine and well...its so glad to form like a group from d c pattern of view.
    i want to suggest one thing...d group shud conduct some sessions towards some core concepts using C
    as C is not meant for application point of view(but one can use for application use) what i mean is d real use of C can b explored only in  System programming...like device drivers etc...
    so d group admins cud send some notes in mails to get awareness among so many c,c++ users...and abt OS also
thank u
ashok.nalla
 
 a.nalla
Win TVs, Bikes, DVD players and more!
Click on
Yahoo! India Promos
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help