LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2012, 02:31 PM   #1
mscoder
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Rep: Reputation: Disabled
sizeof() with strings


What would be the output of the following program?...it comes out as 5...i cant understand the reason.....isnt the name of array treated as a pointer??..even if not,why doesnt the caluculated size include that for the terminating null character?
Code:
#include <stdio.h>
main()
{
char str[]="S65AB";
printf("\n%d", sizeof(str));
}
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 06-06-2012, 02:40 PM   #2
d3adc0de
LQ Newbie
 
Registered: Apr 2012
Location: Cyberspace
Posts: 26

Rep: Reputation: Disabled
This function counts how many characters are in the string, and outputs the result.

Code:
#include <stdio.h>
main()
{
char str[]="S65AB"; #The string which you're counting how many characters it has (which is 5 characters)
printf("\n%d", sizeof(str)); #The printed out result (on your screen not to your printer) of the function.
}
The sizeof operator is built into many programming languages including PHP and C, C++


Last edited by d3adc0de; 06-06-2012 at 02:40 PM. Reason: emphasizing comments
 
Old 06-06-2012, 02:48 PM   #3
mscoder
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
## d3adc0de...i know the use of sizeof() operato.i meant to clarify that why wasnt the name of array (str in this case) treated as a pointer as it always is...n that the o/p(on my screen..not my printer) should have been 6 even if size was calculated due to the null character at the end of array..

Last edited by mscoder; 06-06-2012 at 02:49 PM.
 
Old 06-06-2012, 02:55 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by mscoder View Post
What would be the output of the following program?
After fixing the obvious bugs, 6.

Quote:
Originally Posted by mscoder View Post
...it comes out as 5...i cant understand the reason..
Any standards-compliant compiler will output 6. If your compiler produces code that outputs 5, then it observes that str is not used as a string anywhere, and instead treats the declaration of the array as if it was written as char str[5] = { 83, 54, 53, 65, 66 }; . You can make sure the compiler does not do that optimization by referring to str as a string, for example by printing it.

Quote:
Originally Posted by d3adc0de View Post
This function counts how many characters are in the string, and outputs the result.
That is utterly false. First, sizeof is not a function, it is an operator. Second, it yields the size of the type referred to, not the size allocated. When applied to a character array (which strings are in C), it yields the size of the array, not the string length.

You must be confusing sizeof and strlen().
 
2 members found this post helpful.
  


Reply

Tags
strings



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: replace strings in on file by the strings in another one cristalp Programming 5 10-28-2011 09:47 AM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 02:40 PM
C and sizeof exvor Programming 5 08-11-2007 11:27 PM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
sizeof() and C/C++ boku Programming 2 09-01-2002 07:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration