{"id":3475,"date":"2019-02-02T00:01:28","date_gmt":"2019-02-02T08:01:28","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3475"},"modified":"2025-08-23T11:59:35","modified_gmt":"2025-08-23T18:59:35","slug":"declaring-a-string-literal-as-a-pointer","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3475","title":{"rendered":"Declaring a String Literal as a Pointer"},"content":{"rendered":"<p>The best way to declare a string literal in your code is to use array notation, like this:<\/p>\n<p><code>char string[] = \"I am some sort of interesting string.\\n\";<\/code><br \/>\n<!--more--><br \/>\nThis type of declaration is 100 percent okey-doke. The compiler allocates proper storage for the string, including the null character at the end, and it assigns it to the array <code>string<\/code>. Nifty keen.<\/p>\n<p>You can also declare a string in this manner:<\/p>\n<p><code>char *string = \"I am some sort of interesting string.\\n\";<\/code><\/p>\n<p>Again, in the code you can use the <code>string<\/code> variable to reference the text &mdash; but that&#8217;s about it. Altering the string or passing it to a function renders heaps of trouble. So, while you effectively create a string, you pretty much doom your code not to do anything with the string. In fact, there&#8217;s no clear advantage to such a declaration other than it frightens C programmers who don&#8217;t like pointers.<\/p>\n<p>Recently, I researched this phenomenon, trying to discover if I could possibly find a way around the pointer-string declaration limitation. First, I concocted a program using array notation, but manipulating the string in a function as a pointer:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid change(char *s)\r\n{\r\n    *(s+1) = 'i';\r\n}\r\n\r\nvoid output(char *s)\r\n{\r\n    printf(\"You are %s\\n\",s);\r\n}\r\n\r\nint main()\r\n{\r\n    char string[] = \"fat\";\r\n\r\n    output(string);\r\n    change(string);\r\n    output(string);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the <em>change()<\/em> function, the middle character of <code>fat<\/code> is changed to an <code>i<\/code>. Here&#8217;s the output:<\/p>\n<p><code>You are fat<br \/>\nYou are fit<\/code><\/p>\n<p>If you change the declaration in the <em>main()<\/em> function to read <code>char *string = \"fat\";<\/code> the program won&#8217;t run. It compiles, but the output chokes after the first string is displayed.<\/p>\n<p>I tried everything to get the program to run, but failed. The proper address is passed to the <em>change()<\/em> function, however any attempt to modify the string causes the program to crash with a memory violation. This situation is frustrating, especially because you can pass the string to the <em>output()<\/em> function without a hitch.<\/p>\n<p>The only way I can get the <em>change()<\/em> function to modify the string pointer is when it&#8217;s not initialized. In the following code, the pointer is declared, allocated memory, then a string is assigned. After that, the program runs without a memory violation:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nvoid change(char *s)\r\n{\r\n    *(s+1) = 'i';\r\n}\r\n\r\nvoid output(char *s)\r\n{\r\n    printf(\"You are %s\\n\",s);\r\n}\r\n\r\nint main()\r\n{\r\n    char *string;\r\n\r\n    string= (char *)malloc( sizeof(char)*4 );\r\n    if( string == NULL )\r\n    {\r\n        puts(\"Unable to allocate string storage\");\r\n        exit(1);\r\n    }\r\n    strcpy(string,\"fat\");\r\n\r\n    output(string);\r\n    change(string);\r\n    output(string);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I would think that this code would run the same if I merely assigned <code>*string<\/code> to a literal, but it doesn&#8217;t. And, yes, I tried passing pointers-to-pointers and other tricks. The problem is that the memory allocated by a pointer-string declaration is saved on the stack as local storage. This value isn&#8217;t available outside the function no matter how hard you try. That&#8217;s perfectly okay with me: You have plenty of alternatives for allocating string literals. And using array notation to address the issue is an easy fix.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s a handy shortcut, but for manipulating strings it just doesn&#8217;t work. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3475\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3475","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3475"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3475\/revisions"}],"predecessor-version":[{"id":7146,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3475\/revisions\/7146"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}