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

  • Home
  • SEARCH
  • 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 7821861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:46:20+00:00 2026-06-02T07:46:20+00:00

I have written the following program to resolve a path to several directory names

  • 0

I have written the following program to resolve a path to several directory names

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

char *
tokenizer(char *path, char **name){
  char s[300];
  char *buffer;
  memcpy(s, path, strlen(path)+1);
  printf("%s\n",s);    // PROBLEM
  int i=0;
  while(s[i] == '/'){
    i++;
  }
  if (i == strlen(path)){
    return NULL;
  }
  *name = strtok_r(s, "/", &buffer);
  return buffer;
}

int main(void){
  char str[300];
  char *token, *p;
  scanf("%s",str);
  p = tokenizer(str, &token);
  if (p != NULL)
    printf("%s\n",token);
  else
    printf("Nothing left\n");
  while((p=tokenizer(p, &token)) != NULL){
    printf("%s\n",token);
  }
}

Output of the above program

Input: a/b/c
Output: a/b/c
a/b/c
a
b/c
b
c
c

If I comment the line labelled PROBLEM

Input: a/b/c
Output: Some garbage value

Can somebody explain me the reason for this strange behavior?

Note:
I have realised that s is a stack allocated variable and it ceases to exist in function main() but why does the program works when I use printf() ?

  • 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-02T07:46:21+00:00Added an answer on June 2, 2026 at 7:46 am

    In addition to what geekasaur says:

    strtok_r‘s 3rd parameter is used incorrectly, in two ways:
    1. It should be initialized to NULL before the first call.
    2. It shouldn’t be used in any way (you return it to the caller). It should only be passed to another strtok_r call.

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

Sidebar

Related Questions

I have written the following C program. The output is 32. Why is this?
I am newbie to PHP I have written the following program: $address=array('abc@gmail.com','abc@hotmail.com','def@yahoo.com'); foreach($address as
I have written down the following program that uses the quicksort algorithm to sort
I have written the following program for automating scp command in perl. #!/usr/bin/expect spawn
I have written the following simple C++ program in order to learn how to
i have written the following code to copy a string hello world to another
I have written the following program which connects to two LDAP stores, compares the
I have written a program in linux bash and following are ways to start/stop
I am a newbie in Python. I have written following program and expecting all
i have written following vbScript file to make several programs autostart with delay: 'Delay

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.