simple cast question

This is a discussion on simple cast question within the C Programming forums, part of the General Programming Boards category; Hello, Code: ex1 ----------------------------------- char *name[] = "brian"; printf("Hello %s.\n", name); ex2 ----------------------------------- char *name[] = "brian"; printf("Hello %s.\n", (const ...

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    simple cast question

    Hello,
    Code:
    ex1
    -----------------------------------
    char *name[] = "brian";
    
    printf("Hello %s.\n", name);
    
    
    ex2
    -----------------------------------
    
    char *name[] = "brian";
    
    printf("Hello %s.\n", (const char*) name);
    
    ----------------------------------
    Since printf specifies that the char pointer passed to it is of type const char, is it
    a good idea to explicilty cast (I never see anyone do this) or am I correct in assuming
    that it is automagically cast to const when handed to printf()?

    Also, In alot of code I see this:
    Code:
    (void) printf("Hello\n");
    
    or
    
    (void) fprintf(stderr, "error\n");
    most people don't put the (void) in front of printf's, etc. What is the purpose of
    people writing code in this way? Does is make the program any safer?

    thanks
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    Well in your first two examples, both are wrong in that it should be char *name = "brian";
    And the cast is redundant.

    > What is the purpose of people writing code in this way?
    Some tools (like lint) will report that the return value is being ignored.
    One way round this is to litter the code with void casts.
    Another way would be to RTFM for the tool in question and figure out how to use it properly.

    I mean, if you're going to be that anal to cast all printf returns to void, why not go all the way and actually put checks in there?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Yeah, I forgot. printf() takes const char* argument for the %s conversion specifier and
    isn't name[0] a const char* ptr (meaning that name[0] ALWAYS points to name array)?
    So I'm casting name[0] (which is already a const char* pointer) to const char*. Am I right?

    and char *name[] was a typo, I meant char name[]. char *name[] would be an array
    of pointers.

    Thanks also on the (void). I was just wondering about it. I see it in alot of code I read.

    Thanks.
    Last edited by movl0x1; 07-24-2007 at 04:37 AM.
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    When a function is prototyped as "const char *", it doesn't mean that the thing being passed has to be const, it's a promise (on the part of the function) that the thing being passed will not be modified.

    It means that you can pass either const or non-const things into the function without any problems.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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