473,396 Members | 1,998 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,396 software developers and data experts.

Usage of const char * const name

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?


Expand|Select|Wrap|Line Numbers
  1. void setValue(const int * const intVal, const char *  const cStringVal)
  2. {
  3.     int length=0;
  4.  
  5.     //*intVal = 50; //Error Can't change the value
  6.  
  7.  
  8.     //assign the another value
  9.     strcpy(cStringVal, "India");
  10.  
  11. }
Jul 12 '10 #1
3 2370
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. int aninta = 5;
  2. int anintb = 7;
  3.  
  4. /* Constant pointer to a constant integer
  5.    Pointer can not be altered
  6.    Value pointed at can not be altered
  7. */
  8. const int * const cpc = &aninta;
  9.  
  10. cpc = &anintb;  /* Error */
  11. *cpc = 10;      /* Error */
  12.  
  13. /* Pointer to a constant integer
  14.    Pointer can be altered
  15.    Value pointed at can not be altered
  16. */
  17. const int * cp = &aninta;
  18.  
  19. cp = &anintb;  /* OK    */
  20. *cp = 10;      /* Error */
  21.  
  22. /* Constant pointer to an integer
  23.    Pointer can not be altered
  24.    Value pointed at can be altered
  25. */
  26. int * const pc = &aninta;
  27.  
  28. pc = &anintb;  /* Error */
  29. *pc = 10;      /* OK    */
  30.  
  31. /* Pointer to an integer
  32.    Pointer can be altered
  33.    Value pointed at can be altered
  34. */
  35. int * p = &aninta;
  36.  
  37. p = &anintb;  /* OK    */
  38. *p = 10;      /* OK    */
  39.  
Note that the order of const and int can be interchanged

Expand|Select|Wrap|Line Numbers
  1. /* The 2 declarations in each of these two pairs have the same result */
  2. const int * const cpc1;
  3. int const * const cpc2;
  4.  
  5. const int *cp1;
  6. int const *cp2;
  7.  
Jul 12 '10 #2
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.
Jul 13 '10 #3
donbock
2,426 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. char *strcpy( char *destination, const char *source );
Jul 13 '10 #4

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

Similar topics

1
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 =...
13
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...
2
by: Pinnacle | last post by:
Can anyone explain what is "const char* const*" mean??
2
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 ...
24
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...
2
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() ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
Oralloy
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,...
0
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,...

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.