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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:00:43+00:00 2026-06-16T08:00:43+00:00

Code: void w(char* c, int i) { char * t; sprintf(t, %d, i); perror(c);

  • 0

Code:

void w(char* c, int i)
{
    char * t;
    sprintf(t, "%d", i);
    perror(c);
    perror(t);
}

int main(void)
{
    w("qwe", 5);
    return 0;
}

causes segmentation fault, but

int main(void)
{
    perror("qwdasda");
    w("qwe", 5);
}

works. Why?
My question does not meet your quality standards.

  • 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-16T08:00:44+00:00Added an answer on June 16, 2026 at 8:00 am

    Here is your segmentation fault:

    char *t;
    sprintf(t,"%d",i);
    

    In this code, t is input to sprintf(). However, t is uninitialized so sprintf() will scribble on your memory. It is almost never advisable to use sprintf(), use snprintf() instead:

    char buf[32];
    snprintf(buf, sizeof(buf), "%d", i);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
The following bit of code... void foo(char* x) { int i; int len =
Consider the code: void foo(char a[]){ a++; // works fine, gets compiled //... }
Here's the code snippet: public static void main (String[]arg) { char ca = 'a'
Hello I have a bizarre problem with sprintf . Here's my code: void draw_number(int
Here is my code: void subroutine(const char *message) { printf(message); } And here is
I have the following C code: typedef void (*mycallback) (char buf[128]); void take_callback(mycallback cb)
Is the following C++ code well-formed? void consumer(char const* p) { std::printf("%s", p); }
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout

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.