Thread: Incompatibilities with C for Dummies?

  1. #1
    Registered User Wokzombie's Avatar
    Join Date
    Feb 2010
    Location
    Netherlands
    Posts
    14

    Incompatibilities with C for Dummies?

    The book C for Dummies 2nd Edition is has been pretty good for me for the first three chapters, but now I got to a point where I would swear that the book has it wrong, hence me posting this.

    The book is written in 2004, which is pretty new in C terms. It also recommends using GCC, which is no problem whatsoever since I'm a GNU/Linux user, so I would guess the code explained, including the examples, would be correct.
    Still I can't see what I would have mistyped in this simple example:

    PHP Code:
    #include <stdio.h>

    int main()
    {
        
    char me[20];
        
        
    printf("What is your name?");
        
    scanf("%s",&me);
        
    printf("Darn glad to meet you, %s!\n",me);
        
        return(
    0);

    As far as I can see I copied it character for character, still when I try to compile it through the "gcc who.c -o who" command in GCC 4.4.1 I get this error message:
    who.c: In function ‘main’:
    who.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’
    Is this the book giving me wrong code, or did I really miss something there?

  2. #2
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Remove the & operator. Probably a typo in the book.

    "me" itself is a pointer to the first array element of the array. Dereferencing it cannot be done since its just a pointer, not a ptr to ptr.

    who.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’

    The error says that formated string in printf expects a char pointer, and not a pointer to an array with 20 elements.
    Last edited by Tool; 02-17-2010 at 04:49 PM.

  3. #3
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Code:
    [flexo@localhost ~]$ gcc who.c -o who
    [flexo@localhost ~]$ ./who 
    What is your name?flexo!
    Darn glad to meet you, flexo!!
    [flexo@localhost ~]$ gcc --version
    gcc (GCC) 4.4.2 20091222 (Red Hat 4.4.2-20)
    Copyright (C) 2009 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    I have no idea!

  4. #4
    Registered User Wokzombie's Avatar
    Join Date
    Feb 2010
    Location
    Netherlands
    Posts
    14
    Thanks for the advice Tool, removing the & operator shut my compiler right up.
    In a few months I will come back and know what the rest is you said

    Strange that it did work for you Flexo..

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    flexo87
    gcc -Wall -pedantic *.c
     
    Join Date: Jan 2009
    Location: London
    Posts: 55
    Unlike his name-comment, he didn't actually compile with either -Wall or -pedantic, or he too would have had the same issue.


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

  6. #6
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Quote Originally Posted by quzah View Post
    Code:
    flexo87
    gcc -Wall -pedantic *.c
     
    Join Date: Jan 2009
    Location: London
    Posts: 55
    Unlike his name-comment, he didn't actually compile with either -Wall or -pedantic, or he too would have had the same issue.


    Quzah.
    Yeah you're right!!... but in any case I would have got warnings not compiling errors!! (if we have to be pedantic!)

  7. #7
    Registered User Wokzombie's Avatar
    Join Date
    Feb 2010
    Location
    Netherlands
    Posts
    14
    Strange, a few paged later the author goes a bit further into scanf() and after going through a few aspects of the scanf() function (is that the correct term?) I see this halfway through:

    Code:
    scanf(“%s”,&name);
    There is the ampersand in the same place again. The author's explanation is this:
    "The ampersand is required here to help scanf() find the location of the string variable in memory."
    Last edited by Wokzombie; 02-19-2010 at 09:32 AM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I don't think the author could believe this is correct, perhaps it is a product of cut and pasting the same mistake several times when editing.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User Wokzombie's Avatar
    Join Date
    Feb 2010
    Location
    Netherlands
    Posts
    14
    I don't know if you where writing the reply before I edited in the quote, but the author literaly says
    "The ampersand is required here to help scanf() find the location of the string variable in memory."

    That points towards an intentional act, but Tool pointed out it is incorrect and my compiler seems to agree with him.

    I compiled the code in my original post with the -std=c89,c99,gnu89 and gnu99 flags. All of them give the same result.
    Last edited by Wokzombie; 02-18-2010 at 10:37 AM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Books are no more bug-free than anything else.

    Even the best book on C - K&R is not without a tidy number of issues.

    Expecting some cheaply produced mass market "pulp" with "dummies" in the title to be perfect is asking way too much.

    > "The ampersand is required here to help scanf() find the location of the string variable in memory."
    The author is clueless.
    I think you've already out-grown the book and you need to find something better to learn from.
    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.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Wokzombie
    I don't know if you where writing the reply before I edited in the quote, but the author literaly says
    "The ampersand is required here to help scanf() find the location of the string variable in memory."
    Yes, then the author is dead wrong. Even more remarkably, this is not mentioned in the errata.
    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

  12. #12
    Registered User Wokzombie's Avatar
    Join Date
    Feb 2010
    Location
    Netherlands
    Posts
    14
    Hrm, I think I'll just have to dump C For Dummies.
    Another book I'm interested in is Beginning C, by Ivor Horton

Popular pages Recent additions subscribe to a feed