<pre><code>1167      ptr = (void*)getcwd(cwd, MAX_PATH_LENGTH-1);
(gdb) n
1168      if (!ptr) {
(gdb) print ptr
$1 = 0xbff2d96c "/media/MMC-SD/partition1/aaaaaaaaaaa"
(gdb) print &amp;cwd
$2 = (char (*)[3500]) 0xbff2d96c
(gdb) print strlen(cwd)
$3 = 36
(gdb) print "%s",cwd
$4 = "/media/MMC-SD/partition1/aaaaaaaaaaa", '\0' &lt;repeats 912 times&gt;, "��O�001\000\000\000\000��027\000\000\000�3����EL鷠3�000��027\000\000\000\000\000\000\000\027\000\000\000\000��/�027\000\000\000�3����N����\230���鷠3�000��027\000\000\000\000\000\000\000��000\000\000\000\001\000\000\000��M鷠3����\000\000\000\000.\231�027��w\005\b\001\000"...
(gdb) print "%s", ptr
$5 = 0xbff2d96c "/media/MMC-SD/partition1/aaaaaaaaaaa"
(gdb) Quit
</code></pre>

<p>Why is ptr printing the string correctly but cwd not; this also affects the program and it crashes if I try to use the cwd...</p>

<p>[edit: turns out that crash was caused by a stupid buffer overflow on this var... grr...not gdb, but the print question was still valid]</p>

[{'answer_id': 59509, 'author': 'oliver', 'author_id': 2148773, 'author_profile': 'https://Stackoverflow.com/users/2148773', 'pm_score': 1, 'selected': False, 'text': '<p>That <code>ptr</code> is displayed as nicely-formatted string and <code>cwd</code> as "byte buffer" is probably specific to gdb. In any case it shouldn\'t affect your application; according to <code>man 3 getcwd</code>, <code>ptr</code> should point to <code>cwd</code> (or it should be NULL if an error occurred).\nCan you use <code>ptr</code> without crashing the program?</p>\n'}, {'answer_id': 59514, 'author': 'Craig H', 'author_id': 2328, 'author_profile': 'https://Stackoverflow.com/users/2328', 'pm_score': 0, 'selected': False, 'text': "<p>What type is cwd?  The above code snippet doesn't tell us that.  It could be that ptr being a void* is treated differently by gdb.</p>\n"}, {'answer_id': 59517, 'author': 'mweerden', 'author_id': 4285, 'author_profile': 'https://Stackoverflow.com/users/4285', 'pm_score': 3, 'selected': True, 'text': "<p>The reason that <code>cwd</code> is printed differently in <code>gdb</code> is because <code>gdb</code> knows that <code>ptr</code> is a <code>char *</code> (I guess) and that <code>cwd</code> is an array of length <code>3500</code> (as shown in your output). So when printing <code>ptr</code> it prints the pointer value (and as a service also the string it points to) and when printing <code>cwd</code> it prints the <em>whole</em> array.</p>\n\n<p>I don't see any reason why using <code>cwd</code> instead of <code>ptr</code> would lead to problems, but I would need to see some code to be sure.</p>\n"}, {'answer_id': 59553, 'author': 'Eric Hansander', 'author_id': 5039, 'author_profile': 'https://Stackoverflow.com/users/5039', 'pm_score': 3, 'selected': False, 'text': '<p>I agree with mweerden. Trying something I believe is similar to your code, I get:</p>\n\n<pre><code>(gdb) print cwd\n$1 = "/media", \'\\0\' &lt;repeats 782 times&gt;, "\\016���" ...\n(gdb) print (char*) cwd\n$2 = 0xbfc8eb84 "/media"\n</code></pre>\n\n<p>from gdb, so it seems that since <code>cwd</code> was defined as <code>char cwd[3500]</code>, gdb prints the entire array, while if you tell gdb to interpret it as a <code>char*</code>, it will work as you expect. If your application crashes, I would assume it is because of something else.</p>\n'}]
['https://Stackoverflow.com/questions/59483', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5330/']