Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:36:55+00:00 2026-06-15T16:36:55+00:00

This is the output of the following code, but I expect it to be

  • 0

This is the output of the following code, but I expect it to be correct paths.
What is wrong in the output of odd indexes such as pathsarray[1] = ls/ls ?

---/usr/lib/lightdm/lightdm/ls
---ls/ls
---/usr/local/bin/ls
---ls/ls
---/usr/bin/ls
---ls/ls
---/bin/ls
---ls/ls

char *pathsarray[MAXARGS];//path
char *path=getenv("PATH");
tokenizeEnvPaths(path);

int i;

for(i=0;i<MAXARGS;i++)
{
    if(pathsarray[i] != NULL)
    {
        strcat(pathsarray[i],"/");
        strcat(pathsarray[i],"ls");
        fprintf(stderr,"---%s\n",pathsarray[i]);
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:36:56+00:00Added an answer on June 15, 2026 at 4:36 pm

    Here’s an SSCCE (Short, Self-Contained, Correct Example).

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    enum { MAXARGS = 32 };
    enum { MAXPATHLEN = 1024 };
    
    char *pathsarray[MAXARGS];
    
    static void add_path(char **dir, const char *begin, const char *end)
    {
        if (end == begin)
        {
            begin = ".";
            end = begin + 1;
        }
        size_t len = end - begin;
        *dir = malloc(len + 1);
        memmove(*dir, begin, len);
        (*dir)[len] = '\0';
        printf("Found: %s\n", *dir); // Debug
    }
    
    /* Split path into components - ignore empty elements */
    static size_t tokenizeEnvPaths(const char *path, char **dirs, size_t max_dirs)
    {
        const char *begin = path;
        const char *end;
        size_t num_dirs = 0;
    
        while (num_dirs < max_dirs && (end = strchr(begin, ':')) != 0)
        {
            add_path(&dirs[num_dirs++], begin, end);
            begin = end + 1;
        }
        if (num_dirs < max_dirs && *begin != '\0')
            add_path(&dirs[num_dirs++], begin, begin + strlen(begin));
        return num_dirs;
    }
    
    int main(void)
    {
        const char *path = getenv("PATH");
        size_t ndirs = tokenizeEnvPaths(path, pathsarray, MAXARGS);
    
        for (size_t i = 0; i < ndirs; i++)
        {
            if (pathsarray[i] != NULL)
            {
                char buffer[MAXPATHLEN];
                snprintf(buffer, sizeof(buffer), "%s/%s", pathsarray[i], "ls");
                fprintf(stderr, "---%s\n", pathsarray[i]);
            }
        }
        return(0);
    }
    

    Example output:

    $ PATH=:$PATH ./sp
    Found: .
    Found: /Users/jleffler/bin
    Found: /usr/informix/11.70.FC6/bin
    Found: /usr/gcc/v4.7.1/bin
    Found: /usr/informix/11.70.FC6
    Found: /Users/jleffler/perl/v5.16.0/bin
    Found: /usr/local/bin
    Found: /usr/bin
    Found: /bin
    Found: /usr/gnu/bin
    Found: /usr/sbin
    Found: /sbin
    ---./ls
    ---/Users/jleffler/bin/ls
    ---/usr/informix/11.70.FC6/bin/ls
    ---/usr/gcc/v4.7.1/bin/ls
    ---/usr/informix/11.70.FC6/ls
    ---/Users/jleffler/perl/v5.16.0/bin/ls
    ---/usr/local/bin/ls
    ---/usr/bin/ls
    ---/bin/ls
    ---/usr/gnu/bin/ls
    ---/usr/sbin/ls
    ---/sbin/ls
    $
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using following code but cannot return data from MySQL. This is the output:
From the following code, I expect to get this output from the corresponding input:
In the following code, I'd expect the output to be B C But frustratingly
The following Common Lisp code does not produce the output I would expect it
I have managed to bring the following output with this query SELECT DISTINCT IF(purchaseproduct_customerid=0,contactperson,vendorname)
This is what we type for Absolute path:- echo $_SERVER['DOCUMENT_ROOT']; Output is the following:-
I have written the following C program. The output is 32. Why is this?
any idea how i can get the code below to produce this output? 1
I am trying to use the following code to test set_unexpected(). I expect the
Can anyone tell me why the following code doesn't work the way I expect?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.