Thread: Type Qualifier const

  1. #1
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72

    Type Qualifier const

    Hi, I have two questions:

    1) What is the difference (if any) between:
    Code:
    char const *str;
    
    char *const str;

    2) In following code:
    Code:
    int main (void)
    {
     char input [MAX_SIZE];
    
     ...
    
     fun (input);
    
     ...
    
    }
    
    void fun (char const input)
    {
    
     ...
    
    }
    what is the effect of type qualifier const?

    input is passed by value. There's no way to modify its value anyway. So why using const in parameter list in fun difinition?

    Thanks !

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char const *str;
    This causes the object being pointed to to be read-only

    >char *const str;
    This causes the pointer itself to be read-only. You can also do this to make both the pointer and the object being pointed to read-only:
    const char * const str;
    or
    char const * const str;

    >what is the effect of type qualifier const?
    The function is passed a copy of the variable, but the compiler will complain if you try to change the copy, for example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void fun ( char * const input ) /* Pointer is read-only */
    {
      char input2[BUFSIZ] = "World";
      puts ( input );
      /* Attempt to change the address the pointer points to
      ** which will cause an error
      */
      input = input2;
      puts ( input );
    }
    
    int main ( void )
    {
     char input[BUFSIZ] = "Hello";
     fun ( input );
     return 0;
    }
    >There's no way to modify its value anyway
    Not quite, everything is passed by value in C, but back to the question. You can modify the value that you pass to the function, but it will be a copy of the variable that you passed.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Thanks for help Prelude.

    I didn't know that const char *str is the same as char const *str.

    The use of const for guarding against modification of pointer works fine. It hinders pointer modification.

    However, how can I make the object pointed to read-only?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void fun (const char *input);
    
    int main (void)
    {
     char input [10] = "Hey";
    
     printf ("%s\n", input);
    
     fun (input);
    
     printf ("%s\n", input);
    
     return 0;
    }
    
    void fun (const char *input)
    {
     char *input2 = "Bye";
    
     strcpy (input, input2);
    
     return;
    }
    The object pointed to by input keeps getting modified by fun()

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >However, how can I make the object pointed to read-only?
    Your code is an example of how const isn't
    This is a case where you really should heed the warnings that your compiler gives you, when you use strcpy to transfer the data then you only get warnings but if you transfer each element manually then you'll get errors:
    Code:
    for ( i = 0; i < len; i++ )
      input[i] = input2[i]; 
      /* Error, Lvalue specifies const object */
    The const qualifier can be overridden by using casting and pointers to go around it. Hence, why strcpy only gave you warnings and not errors.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  2. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 03:46 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM