Thread: Const on left, not const on right. ¿Problem?

  1. #1
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82

    Const on left, not const on right. ¿Problem?

    Is there anything wrong with this?
    PHP Code:
    int get_valid_grade_counter();
    const 
    int GRADE_COUNTER get_valid_grade_counter(); 

    float grade[GRADE_COUNTER]; 
    My program works perfectly with this code, as far as I can tell. I'm asking because all the examples I've seen only have constants on the right hand side. For example:
    PHP Code:
    #define MAX_NUMBER_OF_GRADES 16

    float grade[MAX_NUMBER_OF_GRADES]; 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,484
    Well the former still counts as a variable length array, which is only fully supported in C99.
    All the const is doing is making sure you get a diagnostic if you try to modify GRADE_COUNTER after the initialisation (say GRADE_COUNTER++).
    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.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,673
    Code:
    #define MAX_NUMBER_OF_GRADES 16
    
    float grade[MAX_NUMBER_OF_GRADES];
    This works with all C Compilers.

    The other one does NOT work with all C Compilers.

    As already stated in this thread it works if you have variable length arrays.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  2. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  3. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  4. Replies: 7
    Last Post: 04-28-2008, 09:20 AM
  5. Mutable members in const member functions still const
    By ripper079 in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2002, 08:56 AM

Tags for this Thread