Thread: const var.

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    9

    const var.

    Hello,
    can anyone tell me what will happen if const. var are changed through pointers and directly.
    Iam getting a strange behaviour from 2 compilers.

    1. with gcc on solaris-
    const int i=9;
    i=8;
    printf("%d",i);
    // it printf 8 but gives the compile time warning that writing on read only mem.

    the same thing with cc on HP unix gives error.

    but with use of pointers like this.
    const int i=9;
    int *p=&i;
    *p=8;
    works properly.
    if const variables can be changed like this then whats the use of const. var.



  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    18
    I dunno how you got those results.
    I ran them on gcc on Linux and the results are as follows.

    In the first instance my complier gives me a error stating that l-value specifies a constant object.

    In the second case when using a pointer it again gives me a error stating that there is a wrong assignment to a int * from a const int *.

    Anyway as i know the purpose of const variables is to keep the value in them constant and even accidentally not changing the values stored in them.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    9

    const. var

    no ...its not the same in gcc in solaris.
    its allowing to change ,but its giving compile time warning.
    but on HP unix it is giving error.
    that means is it compiler dependant?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    9

    const. var

    in file p.c
    main()
    leftbr
    const int i=9;
    i=8;
    printf("%d",i);
    rt br

    /home/gs001705/ex>gcc p.c
    p.c:4: warning: assignment of read-only variable `i'
    /home/gs001705/ex>a.out
    8

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Salem
    And what's that left br and right br rubbish in your code?
    Some attempt to get past the boards code-formatting rules.
    Probably something like:
    Code:
    #define leftbr {
    #define rt return 0;
    #define br }


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

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    9

    const. var

    First of all if everything is right and as per the definition why would i post it in the board?
    Acc. to const. var. defn. it should give error and not warning. but i found this is untrue with gcc compiler on solaris.
    And C quests are like that. i just want to know with this problematic code(by me) why is that the compiler is just giving warning and not giving any errors?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: const. var

    Originally posted by gsuresh18
    First of all if everything is right and as per the definition why would i post it in the board?
    Acc. to const. var. defn. it should give error and not warning. but i found this is untrue with gcc compiler on solaris.
    And C quests are like that. i just want to know with this problematic code(by me) why is that the compiler is just giving warning and not giving any errors?
    Who cares? Stop trying to modify a constant. Who cares why one particular compiler in on specific instance gives a warning instead of an error? Deal with it and move on. Don't modify constants. It's pretty simple. Stop wasting board space with obvious questioins.

    You've just learned a lesson: All compilers are not equal.

    Acknowledge and move on.

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

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, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01: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