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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:30:15+00:00 2026-06-14T05:30:15+00:00

I started learning C, and I don’t understand what I’m doing wrong. Here is

  • 0

I started learning C, and I don’t understand what I’m doing wrong. Here is a simple code of a function that returns the pid+”.data”.

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

char * getfilename(){
    char name[60];
    sprintf(name,"%i.data",getpid());
    return name;
}

void main(){
    char* name = getfilename();
    printf("%s",name);
}

outputs: ��#�a.

So I guess that I’m doing something wrong.

  • 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-14T05:30:16+00:00Added an answer on June 14, 2026 at 5:30 am
    char * getfilename(){
        char name[60];
        sprintf(name,"%i.data",getpid());
        return name;
    }
    

    You cannot access name object after getfilename has returned. The lifetime of the automatic object name ends at the getfilename last }. Accessing it after the function returns is undefined behavior.

    As a temporary fix you can specify name as static and it will work. But what you should do is to have the getfilename function accepts a pointer argument where the filename will be written.

    EDIT:

    Why I don’t suggest to use strdup?

    • strdup is not a Standard C function. strdup lives in the POSIX world. For portability reasons whenever I can, I prefer to use Standard C functions.
    • strdup performs a hidden malloc call and you have not to forget to perform a free. This is contrary to all functions of the Standard C library which never call malloc (or actually that never appear to call malloc). strdup is a bad API design.
    • strdup is performing a copy of a string. Why do you need to perform an extra copy? Just write the string in a place you can retrieve it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning C but I don't understand this part of the code.
I started learning functional programming (OCaml), but I don't understand one important topic about
I started learning oop php and I don't understand how to make a method
I just started learning Lisp and I don't seem to understand the following piece
I've started learning Objective-C a few weeks ago and I still don't understand how
Started learning algorithms. I understand how to find theta-notation from a 'regular recurrence' like
Just started learning WP7 platform. Don't even know how to describe occurred error. It
hi I started learning google data api. I have one question What will be
I Just started learning ruby and I don't see the difference between an @instace_variable
I've just started learning linux and C, please don't judge me strictly. I'm trying

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.