program error :counting length of string

This is a discussion on program error :counting length of string within the C Programming forums, part of the General Programming Boards category; [edit] Originally Posted by dwks What's wrong with mine? Its not wrong. (I will begin eating crow very soon...) [/edit] ...

  1. #16
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    [edit]
    Quote Originally Posted by dwks
    What's wrong with mine?
    Its not wrong. (I will begin eating crow very soon...)
    [/edit]


    Well strchr returns NULL if it does not find the character you specified. So then, knowing that fgets will read in one less than size, it is quite possible that there will be no newline in the array. In that case you will get a NULL return from strchr, and in that case you will be dereferencing a NULL pointer, which is generally a Bad Thing.

    edit::

    You need the extra brackets with the != operator, otherwise you would have something like this:

    Code:
    if(p = (strchr(string_data, '\n') != NULL)) {
    thereby assigning either zero or non-zero to p.
    Last edited by kermit; 12-27-2005 at 04:04 PM.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well strchr returns NULL if it does not find the character you specified.
    Precisely. NULL is false. It's like doing this:
    Code:
    if(something != 0)
    versus this
    Code:
    if(something)
    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.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by kermit
    edit::

    You need the extra brackets with the != operator, otherwise you would have something like this:

    Code:
    if(p = (strchr(string_data, '\n') != NULL)) {
    thereby assigning either zero or non-zero to p.
    That's not what I meant at all. I was wondering if this is valid C:
    Code:
    if(x = function())
    or if you need extra brackets:
    Code:
    if((x = function()))
    I don't see why you would need them.
    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.

  4. #19
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Obviously its a matter of opinion, you have yours and I have mine, but for the sake of the OP, I think it would be clearer to have an explicit test, i.e., !NULL

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Let me break it down for you.
    Code:
    if((p = strchr(string_data, '\n'))!= NULL)
    if((p = strchr(string_data, '\n')))
    if(p = strchr(string_data, '\n'))
    [edit]
    Yes, your version is clearer. But my version isn't an error.
    [/edit]
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Help with string program
    By Duskan in forum C Programming
    Replies: 8
    Last Post: 04-02-2007, 09:27 AM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21