In the below mentioned code, my intension is only to use the values which are received as a arguments. But unfortunately, I am able to change the “cStringVal”. How to avoid this? - void setValue(const int * const intVal, const char * const cStringVal)
-
{
-
int length=0;
-
-
//*intVal = 50; //Error Can't change the value
-
-
-
//assign the another value
-
strcpy(cStringVal, "India");
-
-
}
3 2370 Banfa 9,065
Expert Mod 8TB -
int aninta = 5;
-
int anintb = 7;
-
-
/* Constant pointer to a constant integer
-
Pointer can not be altered
-
Value pointed at can not be altered
-
*/
-
const int * const cpc = &aninta;
-
-
cpc = &anintb; /* Error */
-
*cpc = 10; /* Error */
-
-
/* Pointer to a constant integer
-
Pointer can be altered
-
Value pointed at can not be altered
-
*/
-
const int * cp = &aninta;
-
-
cp = &anintb; /* OK */
-
*cp = 10; /* Error */
-
-
/* Constant pointer to an integer
-
Pointer can not be altered
-
Value pointed at can be altered
-
*/
-
int * const pc = &aninta;
-
-
pc = &anintb; /* Error */
-
*pc = 10; /* OK */
-
-
/* Pointer to an integer
-
Pointer can be altered
-
Value pointed at can be altered
-
*/
-
int * p = &aninta;
-
-
p = &anintb; /* OK */
-
*p = 10; /* OK */
-
Note that the order of const and int can be interchanged -
/* The 2 declarations in each of these two pairs have the same result */
-
const int * const cpc1;
-
int const * const cpc2;
-
-
const int *cp1;
-
int const *cp2;
-
Hi Banfa,
Thanks to the reply.
You are absolutely correct about integer. So that only already I have commented the second line of my program.
But, like that I should not able to change the character pointer value also. But I am able to change it. (Refer line number 3 of my program). My moto is I should not able to change the "cStringVal" value. How to do this?
Thanks in Advance,
Ferdinend.
Are you getting any errors or warnings from the compiler?
Are you including the header file (string.h) that declares strcpy?
Take a look in that header and check that the prototype is correct. It should be - char *strcpy( char *destination, const char *source );
Sign in to post your reply or Sign up for a free account.
Similar topics
by: electric sheep |
last post by:
Hi, can somebody explain the following syntax to me.
This is straight from a gnu info file:
int
main(void)
{
/* Hashed form of "GNU libc manual". */
const char *const pass =...
|
by: Vijay Kumar R. Zanvar |
last post by:
Hello,
I have few questions. They are:
1. Is "const char * const *p;" a valid construct?
2. How do I align a given structure, say, at 32-byte boundary?
3. Then, how do I assert that a given...
|
by: Pinnacle |
last post by:
Can anyone explain what is "const char* const*" mean??
|
by: s88 |
last post by:
Hi all:
I saw the code likes...
7 #include <stdio.h>
8 int main(void){
9 const char *const green = "\033[0;40;32m";
10 const char *const normal = "\033[0m";
11 ...
|
by: kevin.hall |
last post by:
Is char** (or char*) implicitly convertible to 'const char * const
*'?
I couldn't find anything about it in the standard. MSVS 8.0 allows
this. I'm curious if I'll run into trouble with other...
|
by: itsolution |
last post by:
Hi Guys,
When a function has following arg type,
update_data(const char *const * update_list,...)
exactly what object type update_list can take?
For example, I can call update_aaa() ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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,...
| |