# Using const char\*

**URL:** <https://community.unix.com/t/using-const-char/314912>\
**Category:** Programming\
**Created:** [August 9, 2012, 7:43am UTC](https://community.unix.com/t/using-const-char/314912 "2012-08-09T07:43:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kristinu](https://community.unix.com/letter_avatar/kristinu/32/5_5575768a8748004e209b776fc1b2916d.png) [@kristinu](https://community.unix.com/u/kristinu)\
**Post date:** [August 9, 2012, 7:43am UTC](https://community.unix.com/t/using-const-char/314912/1 "2012-08-09T07:43:01Z")

</div>

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

Here is an example

```nohighlight
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

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![hergp](https://community.unix.com/letter_avatar/hergp/32/5_5575768a8748004e209b776fc1b2916d.png) [@hergp](https://community.unix.com/u/hergp)\
**Post date:** [August 9, 2012, 7:56am UTC](https://community.unix.com/t/using-const-char/314912/2 "2012-08-09T07:56:01Z")

</div>

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:

```nohighlight
char * const desc;

```

If you want desc to be a constant pointer to a constant character, write:

```nohighlight
 const char * const desc;
 

```

---

<div class="post-metadata">

**Author:** ![kristinu](https://community.unix.com/letter_avatar/kristinu/32/5_5575768a8748004e209b776fc1b2916d.png) [@kristinu](https://community.unix.com/u/kristinu)\
**Post date:** [August 9, 2012, 8:48am UTC](https://community.unix.com/t/using-const-char/314912/3 "2012-08-09T08:48:03Z")

</div>

I need to use

```nohighlight
const char * 

```

I am unsure what const char \* means, because I can change desc.

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/5_5575768a8748004e209b776fc1b2916d.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [August 9, 2012, 8:56am UTC](https://community.unix.com/t/using-const-char/314912/4 "2012-08-09T08:56:25Z")

</div>

> [@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

---

<div class="post-metadata">

**Author:** ![kristinu](https://community.unix.com/letter_avatar/kristinu/32/5_5575768a8748004e209b776fc1b2916d.png) [@kristinu](https://community.unix.com/u/kristinu)\
**Post date:** [August 9, 2012, 9:15am UTC](https://community.unix.com/t/using-const-char/314912/5 "2012-08-09T09:15:11Z")

</div>

Got it :b:
