Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

C-Programming

if S is a string what can you say about the expression(strlen(S+strlen(S))

a. it has a value of o
b. it has a value of 1
c. it will generate error

can anyone figure which is the best answer.

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : if S is a string what can you say about the
    : expression(strlen(S+strlen(S))
    :
    : a. it has a value of o
    : b. it has a value of 1
    : c. it will generate error
    :
    : can anyone figure which is the best answer.
    :

    I can... what do you think?

    Here's a structured approach:
    [code]
    strlen(S+strlen(S));

    // Is equivalent to
    ptr = S + strlen(S);
    strlen(ptr);
    [/code]
    So I guess the real problem is in the ptr=... line.

    This is how a basic zero terminated string is in memory:
    [code]
    char* S = "Hello" //= {'H', 'e', 'l', 'l', 'o', 0}
    [/code]
    What strlen(..) does is count the amount of characters till the zero-termination (0 character) is encountered.

    Now let's examine S. S holds the address of "Hello" in memory. More precisely, S points to the first charactacter of the string.
    So S points to 'H', S+1 points to 'e', etc.

    This should be all you need to answer your own question.


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • freelance starfreelance star Member Posts: 73
    : if S is a string what can you say about the
    : expression(strlen(S+strlen(S))
    :
    : a. it has a value of o
    : b. it has a value of 1
    : c. it will generate error
    :
    : can anyone figure which is the best answer.
    :
    0 or a program crash.
    [B][link=http://expert.myitcareer.org/]Freelance Programmer[/link]|[link=http://myitcareer.org/]Interview Question And Answer[/link][/B]
Sign In or Register to comment.