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 6627473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:01:08+00:00 2026-05-25T22:01:08+00:00

I was hoping someone could help me figure out why I am getting a

  • 0

I was hoping someone could help me figure out why I am getting a segmentation fault on my code below. My user has inputted a line of text, which is passed to the parse function. The parse function should initialize a 2D array (I would ideally like to dynamically allocate the array, but for now I am making it an array of size [25][25]).

Starting at the beginning of input strtok() is called. If strtok() sees a pipe symbol, it should increase the count of pipes and go to the next row of the matrix. For example, if the user inputted foo bar | foo1 | foo2 bar1 foo2, the 2D array would look like:

array[][] = { foo, barr;
              foo1;
              foo2, bar1, foo2; }

Eventually I would like to pass this array to another function. However, If I actually input the above into my program, this is the result:

/home/ad/Documents> foo bar | foo1 | foo2 bar1 foo2
test1
Segmentation fault
ad@ad-laptop:~/Documents$

Thus, given where I put these debug statements, the problem is with saving the tokens? This is the first time I have worked with a 2D array so I am sure it is something wrong with my pointer logic. What can I do to fix this segmentation fault? Thanks for your time.

Code:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>

int MAX_PATH_LENGTH = 1024; //Maximum path length to display.
int BUF_LENGTH = 1024; // Length of buffer to store user input
char * delims = "|"; // Delimiters for tokenizing user input.
const int PIPE_READ = 0;
const int PIPE_WRITE = 1;

void execute(char *args, int numPipes, int numArgs){
    int i;
    int j;
    for(i = 0; i <= numArgs; i++){
        for(j = 0; j < 25; j++){
            printf("args[%d][%d]", i, j);
        }
    }
}


void parse(char *input) { 
    char argArray[25][25];
    int numPipes = 0;
    int i = 0;
    int j = 0;
    char *tokenPtr = NULL;
    tokenPtr = strtok(input, delims);
    while(tokenPtr != NULL) {
        if(strcmp(tokenPtr, "|") == 0){ //is token a pipe?
            numPipes++;
            i++;
            j = 0;
        }
        else {
            argArray[i][j++] = *tokenPtr;
            printf("test1\n");
            tokenPtr = strtok(input, NULL);
            printf("test2\n");
        }
    }
    execute(*argArray, numPipes, i);
}

int main () {

    char path[MAX_PATH_LENGTH];
    char buf[BUF_LENGTH];
    char* strArray[BUF_LENGTH];

    while(1) {
      getcwd(path, MAX_PATH_LENGTH);
      printf("%s> ", path);
      fflush(stdout);
      fgets(buf, BUF_LENGTH, stdin);
      parse(buf);

      bzero(strArray, sizeof(strArray)); // clears array
   }
}
  • 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-05-25T22:01:09+00:00Added an answer on May 25, 2026 at 10:01 pm

    Only the first call to strtok should receive the input. Subsequent calls (while parsing the same string) should have NULL as their first argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was hoping someone could help me figure out what to do about a
I got a problem I was hoping someone could help me figure out! I
I was hoping someone could help me figure out how to change the setmessage
I was hoping someone could help me figure out why my application keeps timing
I'm new to ruby and was hoping someone could help me figure out how
hoping someone can help me out with this. I'm trying to figure out whether
I'm struggling on some coding and was hoping someone could help me out please,
Morning All, I was hoping someone could help or provide some sample code for
I was hoping someone could help me out with a small problem I am
I was hoping someone could help a noobie out. I am trying to create

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.