Thread: Size of a string

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    19

    Question Size of a string

    Hi everybody,

    I have a question on calculating the actual size of a string.

    Example:

    char *string = "String size";
    int size;

    The actual "String Size" is 11 charactres long.
    How can I get the size by using sizeof() routine.

    I am trying to use, but that does not work.

    size = sizeof(string);

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use strlen().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    I can't use any Standard Library C functions other than sizeof();

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah... in that case, you may find sizeof rather useless, methinks. You are dealing with a null terminated string here, so run a loop to count characters until you hit the first null character.

    I am going by my C++ background here, but I believe sizeof will just give you the size of the pointer to char in terms of number of bytes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    sizeof("String size")
    perhaps?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    I am trying to create a MACRO that will do this for a string of any size.
    If I have to go with a loop, I may as well just write a routine that does this.
    Thanks for your help.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you declare string like so
    Code:
    char string[] = "String size";
    then sizeof(string) works.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Well, since the size of a character is 1, you can indeed use sizeof to get the length of a FIXED string (doesn't work if the string is user-input into an array):

    Code:
    char string[] "some value here...";
    int sizeOfString = sizeof(string)/sizeof(*string) - 1;
    The -1 is because of the '\0' which usually isn't part of the string length. Note that this DOESN'T work if the user enters something:

    Code:
    int sizeOfString;
    char someString[100];
    fgets(someString, 100, stdin);
    sizeOfString = sizeof(string)/sizeof(*string) - 1;
    printf("Size of the string: %d\n", sizeOfString);
    This code will print 99, regardless of what the user enters. There is no "other" way except for looping until you find '\0'.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    char string[] = "String size";

    size = sizeof(string);

    This works!

    Thanks again!

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    If you declare string like so
    Code:
    char string[] = "String size";
    then sizeof(string) works.
    To elaborate on this, declaring it as a "char *" causes string to be a pointer to a string literal. Taking sizeof will give the size of that pointer (some small value), not the length of the string. Declaring it as "char []" causes it to be an array of chars. Taking sizeof() of an array always gives the size of the array. Since sizeof(char) is equal to 1, this is the same as taking the length of the string. Beware the null terminator; it is included in this count, unlike the return value of strlen().

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by brewbuck View Post
    To elaborate on this, declaring it as a "char *" causes string to be a pointer to a string literal. Taking sizeof will give the size of that pointer (some small value), not the length of the string. Declaring it as "char []" causes it to be an array of chars. Taking sizeof() of an array always gives the size of the array. Since sizeof(char) is equal to 1, this is the same as taking the length of the string. Beware the null terminator; it is included in this count, unlike the return value of strlen().
    Unless it's being passed to a function. This
    Code:
    void function(int array[]) {}
    is the same as
    Code:
    void function(int *array) {}
    You can't get a []-style array in a function.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    Unless it's being passed to a function. This
    Code:
    void function(int array[]) {}
    is the same as
    Code:
    void function(int *array) {}
    You can't get a []-style array in a function.
    You can if you tell it the size:

    Code:
    void function(int array[6][5]);
    It works fine.

    EDIT: Oh, I see what you mean. Yes, sizeof() won't return the real size in that case. By "[] style array" I assumed you mean with respect to indexing.

  13. #13
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    This raises the question (that I've heard asked before, but never succintly answered), why isn't there a library function that can return the size of a block of allocated memory? I mean, the OS must keep a table of allocated memory sizes somewhere, and such sizes, either directly or indirectly, are used in conjuction with a call to free() right? So why isn't there a function that can retreive the size of memory allocated given a pointer reference?

  14. #14
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    size_t _msize( void *memblock );
    Don't quote me on that... ...seriously

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly is that supposed to be? I'd be interested to see what specific compiler it's for, since it turns up zero hits on Ye Old Popular Search Engine.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM