Thread: strlen question?

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67

    strlen question?

    Hey there. I just have a really quick question that I am confused by. Given a string S, I am suppose to know what the expression

    Code:
    strlen(S+strlen(S))
    would yield. Would it have a value of 0, 1? Would it cause an error or does it really just depend on the contents of S? I'm just taking some guesses. I know my question doesn't really provide enough info, but it all I've got to go on. Can anyone shed some light on this?

    Thanks in advance for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Why are you guessing when it takes like 10 seconds to spin out a 1-line program and try it?

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    strlen(S) will give you the length of the string. It can also be interpreted as the index of where the null value is located.
    Therefore, S+strlen(S) will give you a pointer pointing to the null termination and since that pointer is thrown into another strlen() the result will be 0 because it found the null termination immediately.
    So the result is a 0 or a segfault if the string is missing said termination.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by OnionKnight
    strlen(S) will give you the length of the string. It can also be interpreted as the index of where the null value is located.
    Therefore, S+strlen(S) will give you a pointer pointing to the null termination and since that pointer is thrown into another strlen() the result will be 0 because it found the null termination immediately.
    So the result is a 0 or a segfault if the string is missing said termination.
    You really shouldn't have posted that up - assuming the OP hasn't already written the program himself as Salem suggested, he won't have learnt it by doing it himself.

    </rant>
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Compiling the program and testing it would answer his question of what the result would be, but it won't help him solve his initial problem "I am suppose to know what the expression would yield." so if he doesn't have a good insight of what strlen() does and how it's results can be interpreted he'll have a hard time figuring it out.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    You guys were both right. It helped that I wrote a sample program to see that it does in fact return a 0, but like onionknight said, I didn't know why it did, so thank you for the explanation on how strlen() works and why the expression returned what it did. I really appreciate all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  2. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM
  3. I'm not ask for ENTIRE program, only 1 Question !
    By Th3-SeA in forum C Programming
    Replies: 10
    Last Post: 10-01-2003, 12:33 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM