63203

Why it's not safe to cast `char **` to `const char **`? [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/16390294/conversion-from-int-to-const-int" dir="ltr" rel="nofollow">Conversion from int** to const int**</a> <span class="question-originals-answer-count"> 3 answers </span> </li> </ul>

Compile this program:

#include <stdio.h> void main() { char *s = "helo"; char **sp = &s; const char **csp = sp; const char *cs = *csp; printf("%s\n", cs); }

get the warning:

cc.c: In function ‘main’: cc.c:6:24: warning: initialization from incompatible pointer type [enabled by default] const char **csp = sp;

Answer1:

char **sp

<blockquote>

sp is a pointer to pointer to char and sp, *sp, and **sp are all mutable

</blockquote>

const char **csp

<blockquote>

csp is a pointer to pointer to const char and, csp and *csp are mutable but **csp is const

</blockquote>

Now lets see why const char** csp = sp is not safe.

const char Imconst = 'A'; char* ImMutable; const char** ImConstPtr = &ImMutable; // This is illegal but if it is allowed *ImConstPtr = &Imconst; *ImMutable = '1'; // We are trying to assign to "Imconst"

Hope this clears the doubt.

Answer2:

The warning is because char ** and const char ** are not equivalent. To be correct, you could fix the prototype (callee), or fix the caller (const char *).

find fantastic article at <a href="http://c-faq.com/ansi/constmismatch.html" rel="nofollow">http://c-faq.com/ansi/constmismatch.html</a>

Recommend

  • javax.mail.MessagingException: 501 5.5.1 HELO/EHLO requires domain address
  • Need help putting the else into the while with a counter
  • PDFBox make text invisible
  • Antivirus for Azure App Services
  • json_decode() does not work with this valid JSON string
  • Can I protect native JavaScript functions
  • What needs to be changed for websocket security for iOS 10 compatibility
  • Firefox addon using AngularJS - “ng-src” not working
  • How to remove just the index name and not the content in Pandas multiindex data frame
  • InputDispatcher Error
  • Programmatically check if PHP is installed using Python
  • Hatch area using pcolormesh in Basemap
  • java.net.SocketTimeoutException on embedded tomcat with jersey
  • Why isn't \\\\ interpreted as a blackslash in this regex?
  • How to get google-services.json from Developer console?
  • How to check disabled jobs with Jenkins server?
  • How to protect an asp:textbox from user input?
  • NRefactory: How do I access unresolved Named Arguments on a Property Attribute?
  • Why cout is producing no output on Code Blocks?
  • How to print columns containing value
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Undefined references when compiling gSOAP client
  • Object and struct member access and address offset calculation
  • How to assign byte[] as a pointer in C#
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • C: Incompatible pointer type initializing
  • Can Jackson SerializationFeature be overridden per field or class?
  • SSO with signing and signature validation doesn't work
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Delete MySQLi record without showing the id in the URL
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Linker errors when using intrinsic function via function pointer
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?