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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:28:44+00:00 2026-06-03T01:28:44+00:00

I want to preface this by saying that I’ve done very little programming in

  • 0

I want to preface this by saying that I’ve done very little programming in C, so I’d prefer to know why a given solution works rather than just what it is.

I’m trying to write a function which will take a pathname, and return a pathname to a different file in the same directory.

  "/example/directory/with/image.png" => "/example/directory/with/thumbnail.png"

What I’ve tried after reading up on example uses of realpath and dirname (I’m working on Linux; if there’s a cross-platform equivalent, let me know) is:

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

char *chop_path(char *orig) {
  char buf[PATH_MAX + 1];
  char *res, *dname, *thumb;

  res = realpath(orig, buf);
  if (res) {
    dname = dirname(res);
    thumb = strcat(dname, "/thumbnail.png");
    return thumb;
  }
  return 0;
}

Compiling it seems to work, but running the program with

int main(void) {
  char *res = chop_path("original.png");
  if (res) {
    printf("Resulting pathname: %s", res);
  } 
  return 0;
}

gives me a segfault. Any hints?

  • 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-03T01:28:46+00:00Added an answer on June 3, 2026 at 1:28 am

    The only problem I see is the signature of your chop_path routine; it should be

    char *chop_path(char *orig) {
    

    Your version has a missing *. That makes an enormous difference actually; without the *, you’re effectively telling dirname and realpath to interpret the character code of the first character in your argument string as the numerical address (i.e., a pointer to) the path. That’s going to point into a location in low memory that you definitely have not allocated; trying to use it results in that “segmentation fault” error, which means, effectively, that you’re trying to touch memory you’re not allowed to.

    The other issue turned out to be that the dirname() function is declared in libgen.h, which you weren’t including. If you don’t include that header, the compiler assumes dirname() returns int instead of a pointer, and on a 64-bit architecture, the 64-bit return value from the function gets chopped down to 32 bits, a bad pointer is assigned to dname, and that’s going to cause your seg fault right there.

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

Sidebar

Related Questions

Ok so I want to preface this by saying I know very little about
I want to preface this by saying I'm a noob with WPF/.net programming. Here
I'll preface this with two notes: 1) I know very little about Java. 2)
I want to preface this by saying that this is a homework assignment. I
Let me preface this by saying that I'm a very raw iPhone developer. I'm
I want to preface this by saying I believe the Right™ Way to handle
I'll preface this by saying it's a homework assignment. I don't want code written
I want to preface this question by stating that I am fairly new to
I'll preface this with saying that I'm a crappy programmer, I'm sure that what
I must preface this by saying that I am a neophyte (learning), so please

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.