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
curs_printw(3X) 														   curs_printw(3X)

NAME
printw, wprintw, mvprintw, mvwprintw, vwprintw, vw_printw - print formatted output in curses windows SYNOPSIS
#include <curses.h> int printw(const char *fmt, ...); int wprintw(WINDOW *win, const char *fmt, ...); int mvprintw(int y, int x, const char *fmt, ...); int mvwprintw(WINDOW *win, int y, int x, const char *fmt, ...); int vwprintw(WINDOW *win, const char *fmt, va_list varglist); int vw_printw(WINDOW *win, const char *fmt, va_list varglist); DESCRIPTION
The printw, wprintw, mvprintw and mvwprintw routines are analogous to printf [see printf(3)]. In effect, the string that would be output by printf is output instead as though waddstr were used on the given window. The vwprintw and wv_printw routines are analogous to vprintf [see printf(3)] and perform a wprintw using a variable argument list. The third argument is a va_list, a pointer to a list of arguments, as defined in <stdarg.h>. RETURN VALUE
Routines that return an integer return ERR upon failure and OK (SVr4 only specifies "an integer value other than ERR") upon successful com- pletion. X/Open defines no error conditions. In this implementation, an error may be returned if it cannot allocate enough memory for the buffer used to format the results. It will return an error if the window pointer is null. PORTABILITY
The XSI Curses standard, Issue 4 describes these functions. The function vwprintw is marked TO BE WITHDRAWN, and is to be replaced by a function vw_printw using the <stdarg.h> interface. The Single Unix Specification, Version 2 states that vw_printw is preferred to vw- printw since the latter requires including <varargs.h>, which cannot be used in the same file as <stdarg.h>. This implementation uses <stdarg.h> for both, because that header is included in <curses.h>. SEE ALSO
curses(3X), printf(3), vprintf(3) curs_printw(3X)
All times are GMT -4. The time now is 01:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy