473,722 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regarding const qualifier in c

1 New Member
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 1606
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
3636
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 support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
4
1978
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 some typedef structure and offcourse x is a pointer to it. Needless to say my intention for writing such functions in C was to protect the accident write to x's content. So far so good. While porting to C++, I've mada DATA_TYPE as class and I want...
13
2491
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 example, if you only have this code ... void foo( const int n ) {}
17
2327
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 << endl;
10
2788
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1874
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, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
5
1987
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 function, and then figure that the function will not be modifying the list at all, so a const qualifier seems appropriate in the parameter. So essentially I have:
4
2225
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 say I have a function written in assembler/(some non-C language) which takes one pointer-to-const-char arg, the declaration being: extern unsigned int str_len(const char *s);
2
3260
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 objects. It will be good, if explained with example. Appriciate your help in this regard. Thanks,
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.