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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:27:14+00:00 2026-05-16T08:27:14+00:00

Here is my function’s code: char * increment_b_string(char * b_string) { char * ret_string

  • 0

Here is my function’s code:

char * increment_b_string(char * b_string)
{
    char * ret_string = (char *) malloc(7);

    ret_string = "001aaa";

    if (*(b_string + 2) == '9')
    {
        *(ret_string +2) == '0';

        if (*(b_string + 1) == '9')
        {
            *(ret_string +1) = '0';
            *(ret_string) = *(b_string) + 1;
        } else {
            *(ret_string + 1) = *(b_string + 1) + 1;
        }
    } else {
        *(ret_string + 2) = *(b_string + 2) + 1;
    }

    return ret_string;
}

Any thoughts as to why it might be failing?

The general idea is that b_string will contain a value like “123aaa”. The “aaa” portion does not matter, and will never change. Only the first 3 numbers will. They need to increment as though they were integers. Leading zeroes need to be preserved in the beginning. So, if input is “004aaa”, output should be “005aaa”. This only needs to go up to “300aaa”, hence why I’m not considering anything more than that. It’s a school programming assignment, hence the very contrived nature of the problem.

Thanks.

Edit: I’ve changed my function. Here is what it is now.

void increment_b_string(char * b_string)
{   
    if (b_string[2] == '9')
    {
        b_string[2] == '0';

        if (b_string[1] == '9')
        {
            b_string[1] = '0';
            b_string[0] += 1;
        } else {
            b_string[1] += 1;
        }
    } else {
        b_string[2] += 1;
    }
}

Assuming that b_string is initially populated with…

strcpy(b_string, “001aaa”);

…would this be correct? My program is still exhibiting the same behavior, but this may be an error somewhere else in the code.

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

    You cannot increment or change a string literal – they are read only in C, which is what you’re doing.

    char * ret_string = (char *) malloc(7);
    ret_string = "001aaa";
    

    Does not do what you think it does, it does not copy the string into the malloced spaceret_string points to, it sets ret_string to point to the string literal “001aaa”. Use this instead:

     char * ret_string = (char *) malloc(7);
     strcpy(ret_string,"001aaa");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

simonn helped me to code an ordered integer partition function here. He posted two
From here : function highest(){ return makeArray(arguments).sort(function(a,b){ return b - a; }); } function
Here is my code: var Placemat = function(id,id2,id3) { this.bucket = ; this.result =
I am working on a code for a quiz test, here function run($id){ //Is
I have the following php code which I found here : function download_xml() {
var contextMenu = { isVisible : false, open : function() { //some code here
can anyone tell me why I get Return value ... might be undefined here:
I have enter code here function ajaxFunction(){ var ajaxRequest; // The variable that makes
The following code is taken from here : function +vi-git-st() { local ahead behind
I have two functions here function Preloader() {} Preloader.prototype = { init:function() { //

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.