{"id":4339,"date":"2014-03-30T09:55:02","date_gmt":"2014-03-30T09:55:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-null-terminated-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:55:02","modified_gmt":"2014-03-30T09:55:02","slug":"problem-about-null-terminated-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-null-terminated-collection-of-common-programming-errors\/","title":{"rendered":"problem about null-terminated-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5173fb53dde35ab721f52bc54e0e5df2?s=32&amp;d=identicon&amp;r=PG&amp;f=1\" \/><br \/>\nAlexander Maier<br \/>\nmasm null-terminated getmodulefilename<br \/>\nIn my program i call the GetModuleFileName function from the Windows Api. The function tells me the path of the running *.exe. On Windows XP machines the string (szSrc) is not null-terminated according to the MSDN. invoke GetModuleFileName,NULL,szSrc,255How can i null terminate it?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/1dba650bd7e0538186382d8b9b737017?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nDevEight<br \/>\nc multidimensional-array null-terminated<br \/>\nI&#8217;m trying to create an array of null-terminated string arrays. Everything in the array is known at compile time, except one element which is placed into it at runtime.This is how I want the array to look: [ [&#8220;command1&#8221;, &#8220;arg&#8221;, &#8220;arg&#8221;, 0], [&#8220;command2&#8221;, 0], argv, [&#8220;command3&#8221;, &#8220;arg&#8221;, 0] ]So here command1, command2 and their args are known at compile time, only argv is placed into it at runtime. The problem is that I can&#8217;t make the this work. Currently I am doing this to create an array with NUM_COM<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/a0a3a4200899708103f7430d87d56e24?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nTomasz Nurkiewicz<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/8b6050777f99af02c3d9fba73492403d?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJohn Mahoney<br \/>\nc++ arrays char null-terminated<br \/>\nI am a student learning C++, and I am trying to understand how null-terminated character arrays work. Suppose I define a char array like so:char* str1 = &#8220;hello world&#8221;;As expected, strlen(str1) is equal to 11, and it is null-terminated.Where does C++ put the null terminator, if all 11 elements of the above char array are filled with the characters &#8220;hello world&#8221;? Is it actually allocating an array of length 12 instead of 11, with the 12th character being &#8216;\\0&#8217;? CPlusPlus.com seems to suggest that o<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ee1048793d9fec6ef95a5a7c6790ab1b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nGreener<br \/>\nc++ arrays pointers char null-terminated<br \/>\nI&#8217;ve been reading C++ for dummies lately and either the title is a misnomer or they didn&#8217;t count on me. On a section about utilizing arrays of pointers with characters strings they show a function on which I&#8217;ve been completely stumped and don&#8217;t know where to turn. char* int2month(int nMonth) { \/\/check to see if value is in rang if ((nMonth &lt; 0) || (nMonth &gt; 12))return &#8220;invalid&#8221;;\/\/nMonth is valid &#8211; return the name of the month char* pszMonths[] = {&#8220;invalid&#8221;, &#8220;January&#8221;, &#8220;February&#8221;, &#8220;March&#8221;,<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/cae9f3dfd5cdaab0b22a1d4d4c7be29c?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nGlassGhost<br \/>\nc++ c string gcc null-terminated<br \/>\nUpdateturns out this is just another case of &#8220;c++ is not c blues&#8221;What I wantconst char hex[16] = &#8220;0123456789ABCDEF&#8221;;the only thing that workschar hex[16] = &#8220;0123456789ABCDE&#8221;; hex[16] = &#8220;F&#8221;;are there any compiler options or something I can do to make strings not null terminated in the gcc compiler. so that I can make a(n) constant array<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/216c1db53d752f87dd8176a6ba0c2190?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nKeith Thompson<br \/>\nc string null-terminated<br \/>\nAs I understand, the function char *strstr(const char *s1, const char *s2); searches for the null terminated string s2 in the null terminated string s1. Let&#8217;s say, I pass a null terminated string s2, but the buffer passed as s1 is not null terminated, what will happen in such a case if the string s2 is not present in s1? The function strstr will keep searching for s2 beginning from the start address of s1, till it finds some &#8216;\\0&#8217;. But, let&#8217;s say it doesn&#8217;t find a &#8216;\\0&#8217; at all? Will it access some<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7e0259537b3e7c6ee7d8a7475f0c6e81?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nWilliamKF<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/h5uEr.jpg?s=32&amp;g=1\" \/><br \/>\nLuchian Grigore<br \/>\nc++ memset null-terminated<br \/>\nWhat is the correct and safest way to memset the whole character array with the null terminating character? I can list a few usages:&#8230; char* buffer = new char [ARRAY_LENGTH];\/\/Option 1: memset( buffer, &#8216;\\0&#8217;, sizeof(buffer) ); \/\/Option 2 before edit: memset( buffer, &#8216;\\0&#8217;, sizeof(char*) * ARRAY_LENGTH ); \/\/Option 2 after edit: memset( buffer, &#8216;\\0&#8217;, sizeof(char) * ARRAY_LENGTH ); \/\/Option 3: memset( buffer, &#8216;\\0&#8217;, ARRAY_LENGTH ); &#8230;Does any of these have significant advant<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ae80d682d7445d79b8fc00dc13e47ebb?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser1329846<br \/>\nc arrays string null-terminated<br \/>\nProbably I&#8217;m just too dump for googling, but I always thought char arrays get only null terminated by an literal initialization (char x[]=&#8221;asdf&#8221;;) and got a bit surprised when I saw that this seems not to be the case.int main() {char x[2];printf(&#8220;%d&#8221;, x[2]);return 0; }Output: 0Shouldn&#8217;t an array declared as size=2*char actually get the size of 2 chars? Or am I doing something wrong here? I mean it isn&#8217;t uncommon to use a char array as a simple char array and not as a string, or is it?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/e9a46874cd35d5753507f10188ec83a8?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nusr55410<br \/>\nc c-strings null-terminated<br \/>\nI am at a total loss with this one. I can&#8217;t figure out why this isn&#8217;t working. Simple character array with a NULL terminator &#8211; except that when I output it, it doesn&#8217;t terminate!int file_create(const char *path) {\/\/trying to trap situations where the path starts with \/.goutputstreamchar path_left_15[16];strncpy(path_left_15, path, 15);printf(&#8220;%d\\n&#8221;, strlen(&#8220;\/.goutputstream&#8221;)+1);path_left_15[strlen(&#8220;\/.goutputstream&#8221;)+1] = &#8216;\\0&#8217;; printf(&#8220;%d\\n&#8221;, strlen(path_left_15));printf(&#8220;path_left_15: %s\\n&#8221;, pat<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Alexander Maier masm null-terminated getmodulefilename In my program i call the GetModuleFileName function from the Windows Api. The function tells me the path of the running *.exe. On Windows XP machines the string (szSrc) is not null-terminated according to the MSDN. invoke GetModuleFileName,NULL,szSrc,255How can i null terminate it? DevEight c multidimensional-array null-terminated I&#8217;m trying to [&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-4339","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4339","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=4339"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4339\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}