Need an example of const abuse

This is a discussion on Need an example of const abuse within the C Programming forums, part of the General Programming Boards category; I tried doing a followup to this on comp.lang.c. However, the regulars have managed to once again get into a ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    Need an example of const abuse

    I tried doing a followup to this on comp.lang.c. However, the regulars have managed to once again get into a long debate about the semantics of the language.

    And I quote:

    "> Not really.
    > If a pointer is const qualified, then you cannot mofdify what it points to.
    > This has a marginal use in tagging parameters as input rather than output.
    > However there are various weaknesses in C's implementation of const which
    > make in not especially useful. For instance members of a const-qulaified
    > structure can point to writeable memory, and const qualified parameters can
    > be aliased by writable pointers. "

    Can someone give me an example of this?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Maybe this?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct type
    {
       char *ptr;
    };
    
    char text[] = "hello world";
    
    int main( void )
    {
       const char more[] = "more text";
       char *ptr = more;
       const struct type object = {text};
       puts(object.ptr);
       strcpy(object.ptr, "bye");
       puts(object.ptr);
       puts(ptr);
       return 0;
    }
    
    /* my output
    hello world
    bye
    more text
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Okay, give this 24 hours to sink into my skull.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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