Note that there are some explanatory texts on larger screens.

plurals
  1. POif statement ambiguity
    primarykey
    data
    text
    <p>I'm trying to write my own code to walk the PATH to find an executable as a learning exercise in C programming. (after sucess I might replace it with someone else's code, but for now I want to understand my mistakes).</p> <p>The following section of code is not jumping to the else statement I expect ...</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #define EXECUTABLE S_IXOTH /* executable by others */ #define MAX_PATH_LEN 1024 void message (const char *msg) { fprintf(stdout, "INFO: %s\n", *msg); } int main (int argc, char *argv[], char *envp[]) { char *editor; struct stat editor_stat; char full_path[MAX_PATH_LEN]; int found_path; memset(full_path,0,MAX_PATH_LEN); strcpy(full_path,"/bin/ed"); found_path=stat(full_path,&amp;editor_stat); if (found_path!=0) { editor=NULL; message("The EDITOR specified is not found in the PATH. Using default editor"); } else { if (editor_stat.st_mode&amp;EXECUTABLE==0) { editor=NULL; message("The EDITOR specified must have world execute permission. using default editoe"); } else { editor=full_path; } } } </code></pre> <p>When I track it with gdb I see it jumps to the 2nd else instead of the first one, and doesn't execute the check for executable ...</p> <pre><code>(gdb) file /tmp/sample2 Reading symbols from /tmp/sample2...done. (gdb) b 28 Breakpoint 1 at 0x400688: file /home/ken/c/shorter_sample.c, line 28. (gdb) r Starting program: /tmp/sample2 Breakpoint 1, main (argc=1, argv=0x7fffffffe1f8, envp=0x7fffffffe208) at /home/ken/c/shorter_sample.c:28 28 if (found_path!=0) { Missing separate debuginfos, use: debuginfo-install glibc-2.13-2.x86_64 (gdb) p found_path $1 = 0 (gdb) s 36 editor=full_path; (gdb) </code></pre> <p>it should jump to line 32 not 36.</p> <p>I've tried searcing here for C ambiguity and I've read the sections from "C" by Kernighan &amp; Ritchie that are referenced in the index under C ambiguity, and stuck as many curly braces as I can in the code but the compiler isn't doing what I intend.</p> <p>FYI, I'm using gcc-4.5.1-4.fc14.x86_64 with kernel 2.6.35.14-106.fc14.x86_64 on Fedora 14.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload