{"id":547,"date":"2009-12-25T15:35:54","date_gmt":"2009-12-25T20:35:54","guid":{"rendered":"http:\/\/vgable.com\/blog\/?p=547"},"modified":"2009-12-31T03:28:01","modified_gmt":"2009-12-31T08:28:01","slug":"a-c-puzzler","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2009\/12\/25\/a-c-puzzler\/","title":{"rendered":"A C &#038;Puzzler[]"},"content":{"rendered":"<p>Here&#8217;s a C-puzzler for you!<\/p>\n<p>given this function,<\/p>\n<pre>void foo(char* s){\n\tprintf(\"s is at: %p\\n s is: '%s'\\n\", s, s);\n}<\/pre>\n<p>and that<\/p>\n<pre>\nchar s[] = \"Joy!\";\nfoo(s);\n<\/pre>\n<p>prints out<\/p>\n<blockquote><p>\ns is at: <code>0xbffff46b<\/code><br \/>\ns is: &#8216;Joy!&#8217;\n<\/p><\/blockquote>\n<p>what will this next line print?<\/p>\n<pre><strong>foo(&s); \/\/WHAT WILL THIS DO?<\/strong>\n<\/pre>\n<p>Pick all that apply:<\/p>\n<ol>\n<li>Print &#8220;Joy!&#8221;<\/li>\n<li>Print garbage<\/li>\n<li>Print the same address for <code>s<\/code><\/li>\n<li>Print the a different address for <code>s<\/code><\/li>\n<li>Crash<\/li>\n<li>Go into an Infinite loop<\/li>\n<\/ol>\n<h3>Answer<\/h3>\n<p><small>Answer: one and three<\/small><\/p>\n<p>Yeah, it&#8217;s not what I expected either, especially since:<\/p>\n<pre>@encode(__typeof__(s)) = [5c]\n@encode(__typeof__(&s)) = ^[5c]<\/pre>\n<p>In fact, all of these are equvalent (modulo type warnings):<\/p>\n<pre>\nfoo(s);\nfoo(&s[0]);\nfoo(&(*s));\nfoo(&s);\n<\/pre>\n<p><a href=\"http:\/\/www.reddit.com\/r\/programming\/comments\/i6k5\/jeez_after_all_these_years_a_c_array_vs_pointer\">Explanation<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a C-puzzler for you! given this function, void foo(char* s){ printf(&#8220;s is at: %p\\n s is: &#8216;%s&#8217;\\n&#8221;, s, s); } and that char s[] = &#8220;Joy!&#8221;; foo(s); prints out s is at: 0xbffff46b s is: &#8216;Joy!&#8217; what will this next line print? foo(&#038;s); \/\/WHAT WILL THIS DO? Pick all that apply: Print &#8220;Joy!&#8221; Print [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,18,7,5,4],"tags":[553,613,76,554],"class_list":["post-547","post","type-post","status-publish","format-standard","hentry","category-announcement","category-bug-bite","category-c","category-objective-c","category-programming","tag-arrays","tag-c","tag-gcc","tag-pointers"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=547"}],"version-history":[{"count":7,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"predecessor-version":[{"id":554,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/547\/revisions\/554"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}