473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

regarding const qualifier in c

sir,
i am getting doubt on const qualifier .
Que:
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.           const int f(int );
  4.           int b = 4;
  5.           int  a = f(&b);
  6.           printf("%d", a); // o/p i am getting 4 is it ok  or compiler depent and compiled with warnning discarding const qualifier 
  7.  }
  8.  
  9. const int f(int *p)
  10. {     
  11.          *p = 10;
  12.         return  *p; 
  13. }       
compiler : gcc -Linux
Doubt 1: its returning const int and in the main setting in the int variable !
How these is happenning ?
Doubt 2 : int the main , I declared const int a[2] = { 1, 2}; sending f(&a[1])
o/p its reflecting in the main function why?
Oct 4 '07 #1
1 1584
weaknessforcats
9,208 Expert Mod 8TB
First: main()returns an int not a void. main() returning void was an old Microsoft goof-up that they have been trying to eradicate ever since.

Second: This code
void main()
{
const int f(int );
etc...
This is a function prototype:

const int f(int);

because the function returns a copy of an int. This const int can be assigned to either a const or non-const variable:

int x = f(3); //OK

const int y = f(4); //Also OK

so having the return be const is irrelevant. It is ignored by the compiler and also produces a warning.

In general terms, if the function returns a type then having the return be const is ignored and produces a warning.

The above is not true in the case of the function returning a pointer. There the const is important since it describes whether the value pointed at by the pointer is changeable or not. Returning a pointer is a separate case since a pointer is not a type. Rather, it is an address.

Third:

int a = f(&b);
won't compile becuse the function prototype shows an int argument and not an int*.

Fourth:

Doubt 2 : int the main , I declared const int a[2] = { 1, 2}; sending f(&a[1])
o/p its reflecting in the main function why?
You are passing the address of a variable to the function. The function is changing the variable at that address. Hence, you variable in main() got changed.

BTW: Where is const int a[2] = { 1, 2}; ???
Oct 4 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
4
by: Mahesh Tomar | last post by:
Dear Readers, I am porting my existing C code to C++. In my existing code there are numerous functions that has been defined with CONST qualifier. For eg. foo(const DATA_TYPE *x); DATA_TYPE is...
13
by: matthias_k | last post by:
Hi, I've never thought about this before, but since you are able to overload a function only by changing the const-ness of the formal parameters, some annoying side effects will arise. For...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
5
by: amvoiepd | last post by:
Hi, My question is about how to use const properly. I have two examples describing my problem. First, let's say I have a linked list and from it I want to find some special node. I write the...
4
by: Ben Petering | last post by:
Hi group, this is a 'best practice' type question (I want discussion of the issue - whys and why nots - similar to "casting the return value of malloc()", to cite an analogous case). Let's...
2
by: venkat | last post by:
Hi, i came across restrict qualifier while looking the code. I haven't able to understand what does this do?. Can some one help me how does this makes the things restrict to an specified...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.