{"id":4739,"date":"2014-03-30T15:02:39","date_gmt":"2014-03-30T15:02:39","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-initialize-static-pointer-with-malloc-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:02:39","modified_gmt":"2014-03-30T15:02:39","slug":"how-to-initialize-static-pointer-with-malloc-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-initialize-static-pointer-with-malloc-in-c-collection-of-common-programming-errors\/","title":{"rendered":"How to initialize static pointer with malloc in C?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to initiate a static variable (inside a function) with malloc in C, but I&#8217;m getting the &#8220;initializer not constant error&#8221;. I know that I can&#8217;t initiate a static with non constants in C, but can anyone think of a solution? I need the code to have the same effect as this:<\/p>\n<pre><code>static int *p = (int *)malloc(sizeof(int));\n<\/code><\/pre>\n<p>Is there a trick\/workaround?<\/p>\n<p>EDIT: I have a function that is called every time a flag goes high. In this function, I&#8217;m creating and starting a new thread. I declare a pointer to a struct and use malloc to allocate memory then pass this pointer to the thread. Then the function returns control. When I re-enter the function, the thread that I opened initially will still be running and I want to be able to access the memory region that I originally passed to the thread. That&#8217;s why I need a static so that I can malloc on the first call and then use the same address on subsequent calls. This way I can get info from the thread. All this to avoid using global variables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to initiate a static variable (inside a function) with malloc in C, but I&#8217;m getting the &#8220;initializer not constant error&#8221;. I know that I can&#8217;t initiate a static with non constants in C, but can anyone think of a solution? I need the code to have the same effect as this: static int [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4739","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4739"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4739\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}