Sponsored Content
Full Discussion: constants in C/C++
Top Forums Programming constants in C/C++ Post 26695 by Shobhit on Thursday 22nd of August 2002 09:23:50 AM
Old 08-22-2002
constants in C/C++

Hi all
My question is related to following sample code which tries to change consant value by pointers.(I know it is wrong practice but i am surprised by mis-behaviour)
The code:

#include <stdio.h>

int main()
{
const int x = 10;
int *y;
const int * const z = &x;

y = (int *)&x;
*y=11;
printf('%p %d ', y, *y);
printf('%p %d ', &x, x);
printf('%p %d ', z, *z);
return 0;
}

The output:

0012FF7C 11
0012FF7C 10
0012FF7C 11

The above output is when the program was compiled as cpp file and got
11
11
11
when compiled as "c" file.
Also
const int x=10;
int arr[x];
works with c++ compiler and not C.

My question is why is this mis-behaviour.
 

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl: eval and constants

is it possible to use eval to create constants in perl? i cannot seem to get anything to work, and my searches are turning up little to nothing. an example of what i am trying to do is this: 2 arrays: array 1: 'FOOD','NUMBER','OS' array 2: 'pizza','two','unix' loop through the arrays and... (5 Replies)
Discussion started by: effigy
5 Replies

2. Programming

char constants vs. hard-coding

This might be a silly question, but I thought I'd ask anyway. If I'm writing in C, isn't it more efficient to, for instance, use constant character variable set to 'A' instead of hard-coding a character 'A'? Since it's only a single character instead of a string, it might not matter much. (10 Replies)
Discussion started by: cleopard
10 Replies

3. UNIX for Dummies Questions & Answers

Operation with real variables and constants

Hello there, I'd like to define a variable b equal to 0.5/a where a=0.001, so I wrote something like that: a=0.001; let 'b=0.5/$a'; but it doesn't work... maybe because the variable a has a real value??? Any help will be appreciated!!!:D (1 Reply)
Discussion started by: Giordano Bruno
1 Replies
APC_DEFINE_CONSTANTS(3) 						 1						   APC_DEFINE_CONSTANTS(3)

apc_define_constants - Defines a set of constants for retrieval and mass-definition

SYNOPSIS
bool apc_define_constants (string $key, array $constants, [bool $case_sensitive = true]) DESCRIPTION
define(3) is notoriously slow. Since the main benefit of APC is to increase the performance of scripts/applications, this mechanism is pro- vided to streamline the process of mass constant definition. However, this function does not perform as well as anticipated. For a better-performing solution, try the hidef extension from PECL. Note To remove a set of stored constants (without clearing the entire cache), an empty array may be passed as the $constants parameter, effectively clearing the stored value(s). PARAMETERS
o $key - The $key serves as the name of the constant set being stored. This $key is used to retrieve the stored constants in apc_load_constants(3). o $constants - An associative array of constant_name => value pairs. The constant_name must follow the normal constant naming rules. value must evaluate to a scalar value. o $case_sensitive - The default behaviour for constants is to be declared case-sensitive; i.e. CONSTANT and Constant represent different values. If this parameter evaluates to FALSE the constants will be declared as case-insensitive symbols. RETURN VALUES
Returns TRUE on success or FALSE on failure. EXAMPLES
Example #1 apc_define_constants(3) example <?php $constants = array( 'ONE' => 1, 'TWO' => 2, 'THREE' => 3, ); apc_define_constants('numbers', $constants); echo ONE, TWO, THREE; ?> The above example will output: 123 SEE ALSO
apc_load_constants(3), define(3), constant(3), Or the PHP constants reference. PHP Documentation Group APC_DEFINE_CONSTANTS(3)
All times are GMT -4. The time now is 02:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy