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

The Archive Base Latest Questions

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

As I understood the correct programming style tells that if you want to get

  • 0

As I understood the correct programming style tells that if you want to get string (char []) from another function is best to create char * by caller and pass it to string formating function together with created string length. In my case string formating function is “getss”.

void getss(char *ss, int& l)
{

    sprintf (ss,"aaaaaaaaaa%d",1);
    l=11;
}


int _tmain(int argc, _TCHAR* argv[])
{

    char *f = new char [1];
    int l =0;
    getss(f,l);

    cout<<f;
    char d[50] ;
    cin>> d;
    return 0;
}

“getss” formats string and returns it to ss*. I thought that getss is not allowed to got outside string length that was created by caller. By my understanding callers tells length by variable “l” and “getcc” returns back length in case buffer is not filled comleatly but it is not allowed go outside array range defined by caller.

But reality told me that really it is not so important what size of buffer was created by caller. It is ok, if you create size of 1, and getss fills with 11 characters long. In output I will get all characters that “getss” has filled.

So what is reason to pass length variable – you will always get string that is zero terminated and you will find the end according that.

What is the reason to create buffer with specified length if getss can expand it?

How it is done in real world – to get string from another function?

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

    Actually, the caller is the one that has allocated the buffer and knows the maximum size of the string that can fit inside. It passes that size to the function, and the function has to use it to avoid overflowing the passed buffer.

    In your example, it means calling snprintf() rather than sprintf():

    void getss(char *ss, int& l)
    {
        l = snprintf(ss, l, "aaaaaaaaaa%d", 1);
    }
    

    In C++, of course, you only have to return an instance of std::string, so that’s mostly a C paradigm. Since C does not support references, the function usually returns the length of the string:

    int getss(char *buffer, size_t bufsize)
    {
        return snprintf(buffer, bufsize, "aaaaaaaaaa%d", 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understood how to create events, like that: var evt=document.createEvent('Event'); evt.initEvent('foo',true,true); window.dispatch(evt); Then anyone
I'm not quite sure how indexes works. Here's how I understood it, please correct
I'm trying to understand what the correct approach for a constructor that accepts a
I understood that in order to use cookies inside of phonegap native app there
I understood that anonymous types are marked private by the compiler and the properties
I would like to get into programming with C# and Silverlight. I have read
This is an extract from Advanced Liniux Programming: Semaphores continue to exist even after
I have an assignment from my programming class, which is very poorly worded... The
I'm not entirely new to programming, but I'm not exactly experienced. I want to
I've been reading up on assembly. From what I understand about programming languages, it

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.