Thread: Problem: Bubblesort - "strlen" in the function...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Adak View Post
    Just looked it up, and K&R says you're right.

    If the array[] had two strings in it, and the first string had an '\0' end of string marker, it would surely be goofed to use strlen(array) then, wouldn't it?

    e.g.:
    Code:
    "fisrt string\0 second string\0"
    In the same array element, of course.
    Each array element is a char. So they can't all be in the same array element?

    I suppose you could initialize a string to that quoted thing, but that would give you a string of "first string" and some extra, but unrelated, memory that would happen to make sense when interpreted as characters. (In other words, if you tried to use in a library function or whatever, you would never get the second part.)

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by tabstop View Post
    Each array element is a char. So they can't all be in the same array element?

    I suppose you could initialize a string to that quoted thing, but that would give you a string of "first string" and some extra, but unrelated, memory that would happen to make sense when interpreted as characters. (In other words, if you tried to use in a library function or whatever, you would never get the second part.)
    This is what I mean:

    Code:
    char array[] = { "the\0 candle" };
    So what can strnlen(array). tell us about the size of the array?

    I don't believe it can be accurate, but I'll try it in a bit, and see.

    Sorry about the same "element", clearly the wrong word, I meant in the same dimension.
    Last edited by Adak; 01-24-2008 at 07:24 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Adak View Post
    This is what I mean:

    Code:
    char array[] = { "the\0 candle" };
    So what can strnlen(array). tell us about the size of the array?

    I don't believe it can be accurate, but I'll try it in a bit, and see.

    Sorry about the same "element", clearly the wrong word, I meant in the same dimension.
    Well, strlen is not supposed to give you the size of the array, but the size of the string that the array contains. (Think of all those 80-character (or BUFSIZ-character, or whatever) arrays you've used -- you want strlen to report the size of the string in the buffer, not 80 (or BUFSIZ, or whatever). In your example, array points to the string "the". It is true that you've allocated 12 characters in memory, and put something in all of them, but in a string context, array and the string literal "the\0 candle" aren't the same thing.[1] You get strlen(array) == 3 and sizeof(array)==12.

    [1] Edit to add: I mean that in a string context, array does not represent the full string "the\0 candle", but only up to the terminating \0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM