{"id":1102,"date":"2010-07-13T10:44:54","date_gmt":"2010-07-13T10:44:54","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1102"},"modified":"2010-07-13T10:44:54","modified_gmt":"2010-07-13T10:44:54","slug":"static-in-c-hide-that-function","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/07\/13\/static-in-c-hide-that-function\/","title":{"rendered":"Static in c &#8211; hide that function"},"content":{"rendered":"<p>In the c language there is no such thing as a class, private\/public setup as such, but if you have a allot of c files that may have allot of the same function names inside e.g. swap(void *data, void *data2).  So how do you just want to call your swap function and not another one from another c file.  Which in this setup it is similar to private method within a class.<\/p>\n<p>So instead of having access to the private keyword, you can define a function to be a static function as below in this header file that I have called static_test.h<\/p>\n<pre lang=\"cpp\">\r\n#ifndef STATIC_TEST_H\r\n#define STATIC_TEST_H\r\n\r\n#include <iostream>\r\nusing namespace std;\r\n\r\nstatic void PrivateAccessable(char *sting);\r\n\r\nvoid PublicAccessable(char *st);\r\n\r\n#endif \/\/ STATIC_TEST_H<\/pre>\n<p>the PublicAccessable function is what is *exposed* to other parts of the main program but the static void PrivateAccesable is not.<\/p>\n<p>So if you try and access it via the int main function like so<\/p>\n<pre lang=\"cpp\">\r\n\r\n#include <iostream>\r\n#include \"static_test.h\"\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n\r\n    PublicAccessable(\"hi there\");\r\n    \/\/ undefined reference to `PrivateAccessable(char*)'\r\n    PrivateAccessable(\"should not be able to access this!!\");\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p>The compiler will complain with something similar to the error above the PrivateAccessable line, or <\/p>\n<pre lang=\"bash\">\r\nundefined reference to `PrivateAccessable(char*)'\r\n<\/pre>\n<p>because the linker does not know where that function is, since it is &#8220;hidden&#8221; within the static_test namespace as such.<\/p>\n<p>Just to complete the code example here is the static_test.c<\/p>\n<pre lang=\"cpp\">\r\n#include \"static_test.h\"\r\n\r\nstatic void PrivateAccessable(char *sting)\r\n{\r\n    cout << sting << endl;\r\n}\r\n\r\nvoid PublicAccessable(char *st)\r\n{\r\n    PrivateAccessable(st);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the c language there is no such thing as a class, private\/public setup as such, but if you have a allot of c files that may have allot of the same function names inside e.g. swap(void *data, void *data2). So how do you just want to call your swap function and not another one &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/07\/13\/static-in-c-hide-that-function\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Static in c &#8211; hide that function<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[65,243],"class_list":["post-1102","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-function","tag-static"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1102"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1102\/revisions"}],"predecessor-version":[{"id":1860,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1102\/revisions\/1860"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}