{"id":63,"date":"2006-11-22T15:33:33","date_gmt":"2006-11-22T15:33:33","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=63"},"modified":"2006-11-22T15:33:33","modified_gmt":"2006-11-22T15:33:33","slug":"i-can-be-a-c-numptie","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=63","title":{"rendered":"I can be a C-numptie"},"content":{"rendered":"<p>This might not be earth-shatteringly new to those who have seen my code before, but I was a little surprised. \ud83d\ude42<\/p>\n<p>A C program that I wrote a little while ago (and has previously worked fine on Windows, Linux, Solaris and HP) failed horribly on <a href=\"http:\/\/www.ibm.com\/aix\" title=\"AIX - an IBM UNIX operating system\" target=\"_blank\">AIX<\/a>. A little digging showed that I had written:<\/p>\n<p><code>char* thisIsSupposedToBeAString[256];<\/code><\/p>\n<p>I guess that at the time I wrote it (presumably early in the morning, and caffeine-fuelled) I had a string (<code>char thisIsSupposedToBeAString[256];<\/code>) and wanted a pointer to it, so went back and stuck a * on it without thinking.<\/p>\n<p><!--more-->The problem is that strings in C are icky. You don&#8217;t really have a string object &#8211; your char variable is a pointer to the start of the string, and the string functions just keep going until they hit something that tells them they&#8217;ve reached the end. So <code>thisIsSupposedToBeAString<\/code> was already a pointer, before I went and messed about with it. In fact, by sticking a * on it I created a pointer to a pointer. <\/p>\n<p>Instead of having a pointer to a 256-character string like I guess I thought I had, I&#8217;ve ended up with an array of 256 strings&#8230; or, an array of 256 pointers to pointers to a character. Or something like that. Erg&#8230;. sometimes I really don&#8217;t like C \ud83d\ude42<\/p>\n<p>In fact, while I am admitting to supreme uselessness, I guess in an attempt to get the compiler to stop complaining about my gibberish code, I used this:<\/p>\n<p><code>char* thisIsSupposedToBeAString[256];<br \/>\n...<br \/>\naFunctionThatUsesMyString((char*)&thisIsSupposedToBeAString);<\/code><\/p>\n<p>to pass the string to another function. So, in my attempt to pass a pointer to my string, I was casting an address-of-a-pointer-to-a-pointer-to-a-char, to a pointer-to-a-char. Clever(!) And AIX is strict enough that it doesn&#8217;t like it. Although now I look at it, I&#8217;m amazed the other platforms all let me get away with it&#8230; <\/p>\n<p>And for those who are now thoroughly lost, the code now looks like:<\/p>\n<p><code>char thisIsActuallyAString[256];<br \/>\naFunctionWhichWorks(thisIsActuallyAString);<\/code><\/p>\n<p>Maybe I should stick to high-level languages? \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This might not be earth-shatteringly new to those who have seen my code before, but I was a little surprised. \ud83d\ude42 A C program that I wrote a little while ago (and has previously worked fine on Windows, Linux, Solaris and HP) failed horribly on AIX. A little digging showed that I had written: char* [&hellip;]<\/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":[],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=63"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/63\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}