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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:14:28+00:00 2026-05-17T19:14:28+00:00

Platform: Linux, OSX Compiler: GCC I’ve got a simple program which is currently confounding

  • 0

Platform: Linux, OSX
Compiler: GCC

I’ve got a simple program which is currently confounding me – I know that I’m messing with a couple different kinds of arrays/pointers to produce this problem – its intentional – I’m trying to understand it.

The code as listed will compile and run as expected, but changing data4 in the call to strsep(&data4, "e"); to data1 or data3 causes a segmentation fault. I would like to understand why.

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

int main(int c, char** v) {
    char* data1 = "hello\0";
    char* data2 = strdup(data1);
    size_t sz = strlen(data1);
    char data3[sz+1];
    char* data4;

    memset(data3, 0, sz+1);
    data4 = strncpy(data3, data1, sz);
    data4[sz] = '\0';

    char* found = strsep(&data4, "e");

    if (found == NULL) {
        printf("nothing found\n");
    } else {
        printf("found e\n");
    }

    return 0;
}
  • 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-17T19:14:29+00:00Added an answer on May 17, 2026 at 7:14 pm

    changing data4 in the call to strsep(&data4, “e”); to data1 or data3 causes a segmentation fault.

    In this case:

    char* found = strsep(&data1, "e");
    

    the string pointed to by data1 is a literal so it cannot be changed. When strsep() tries to place a ‘\0’ into it, segfault.

    in the other case:

    char* found = strsep(&data3, "e");
    

    data3 is an array, not a pointer (even though arrays easily evaluate to pointers, they are not actually pointers), so strsep() cannot update the value of the pointer which is what it tries to do once it finds the token. I get the following warning from gcc attempting to point this out:

    test.c:17: warning: passing argument 1 of 'strsep' from incompatible pointer type
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program on a Linux platform that is generating text files that
I have been asked to write a java program on linux platform. According to
I have a cross platform C++ project that targets and successfully compiles on Linux,
In Java I have code that works well on OSX but not in linux.
Why does the program listed below run on mac osx and linux but not
I'm developing cross-platform project that would support : Four C++ compilers - GCC, MSVC,
I'm just starting out on a cross-platform (Windows, Linux, OS X) C++ project, and
I develop some PHP project on Linux platform. Are there any disadvantages of putting
We are planning to implement ACL on our Linux platform. Only one particular group
Hi I'm working with C++ on Linux platform, i have to get the value

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.