Using const char*


 
Thread Tools Search this Thread
Top Forums Programming Using const char*
# 1  
Old 08-09-2012
Using const char*

I am writing some code in C++ to print a message using fprintf

Here is an example

Code:
void pr_desc(
    FILE*  stream,
    int       shift,
    const char*  desc) {

  const char*  format="%*s\e[0;37m%s\e[0m\n";
  fprintf(stream,format,shift,"",desc); 
}

I call it using

Code:
const char*  desc;  

desc="display susage";
pr_desc(stream, msgShift, desc); 

desc="prints some examples";
pr_desc(stream, msgShift, desc); 

desc="display this usage information";
pr_desc(stream, msgShift, desc);

I want to know how it is that I can change desc when I declared desc as const.

Last edited by kristinu; 08-09-2012 at 08:51 AM..
# 2  
Old 08-09-2012
desc is not a constant pointer to char, but a pointer to a constant char.

If you want desc to be a constant pointer, write:
Code:
char * const desc;

If you want desc to be a constant pointer to a constant character, write:
Code:
 const char * const desc;

# 3  
Old 08-09-2012
I need to use

Code:
const char *

I am unsure what const char * means, because I can change desc.
# 4  
Old 08-09-2012
Quote:
Originally Posted by kristinu
I want to know how it is that I can change desc when I declared desc as const.
You are mistaken. You have not declared desc as const. desc is a memory address and it can change. However, the char that the memory address points to cannot be modified through desc.

Read hergp's response carefully.

Since desc is not constant, you can assign a new memory address to it, which is what's happening with desc="string". What you cannot do (even if the memory is dynamically allocated) is indirect through desc to modify a character, *desc='a'.

Regards,
Alister

Last edited by alister; 08-09-2012 at 10:02 AM..
This User Gave Thanks to alister For This Post:
# 5  
Old 08-09-2012
Got it Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Double to const char conversion

Dear all, I am using C and ROOT for programming. And I need to incorporate following in my code. char *fps=NULL; int dec=0,sign=0; float mean = h1->GetMean(1); //0.001298 fps= fcvt(mean,6 , &dec, &sign); I need to provide this mean as const char to some other function to get... (8 Replies)
Discussion started by: emily
8 Replies

2. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

3. Programming

Why this const variable is not changing even after applying const_cast?

Hi In the following code, why the variable 'i' still retains the value 10 instead of 11? Why it is not possible to alter this variable's value? int main() { const int i = 10; *(const_cast<int*>(&i)) = 11; cout << i << endl; // Ans: 10 } (6 Replies)
Discussion started by: royalibrahim
6 Replies

4. Programming

Questions about const

Hi all, I have some questions about functions. In a code I have (a .hpp file) there is this line which says: const Class_1_Name* Class_2_Name::MethodName(int ipart) const {return (ClassName*)_ref_to_method.At(ipart);} My questions are: What is the meaning of the two constants,... (2 Replies)
Discussion started by: eager2no
2 Replies

5. Programming

Optional non-const reference argument c++ ?

Is it possible to have a non-const reference variable as an OPTIONAL/DEFAULT parameter to c++ function ex void read(string &data,int &type=0 /*or something*/) ; so i will call read(data); //or int type; read(data,type); printf("Type =%d",type); I found one dirty workaround ... (2 Replies)
Discussion started by: johnbach
2 Replies

6. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

7. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

8. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

9. Programming

diff b/w char const in C/C++

hi, what is the difference b/w char in C and C++.and give me the examples. Thanks... sarwan (3 Replies)
Discussion started by: sarwan
3 Replies

10. Programming

Reference to a const

Can any one explain how the statement '2' in the following statements is a legal one. int & ref = 3; // Illegal statement - Compiler error. const int& ref=3 ; // Compile and executes properly. Thanks in Advance, Arun (1 Reply)
Discussion started by: arun.viswanath
1 Replies
Login or Register to Ask a Question

Featured Tech Videos