After modifying string (char*) in a function the value won't show unless printed inside function












0














#include <stdio.h>
#include <string.h>
void increase(char **tab)
{
int i=strlen(*tab);
int o=i;
while((*tab)[i]!='5' && i>=0)
{
i--;
}
if(i>=0)
{
int tt;
char array[o+1];
for(tt=0;tt<o;tt++)
{
if(tt!=i)
array[tt]=(*tab)[tt];
else
array[tt]='6';
}
array[o]='';
*tab=array;
// printf("n%s",*tab);
}
else
{
char array[o+2];
int tt;
for(tt=0;tt<=o;tt++)
{
array[tt]='5';
}
array[o+1]='';
*tab=array;
// printf("n%s",*tab);
}
}
int main()
{
int n;
char *test;
test="555";
increase(&test);
printf("n%s",test);

return 0;
}


Okay, so increase() is meant to replace number in test with the next number containing only 5 and 6.
What I wanted to do is to change the value of test directly by using pointer to char*. Everything seems to work just fine untill it comes to displaying changed value - it simply won't show unless was asked to do it inside the
increase() function. Once I add



printf("n%s",*tab);


inside any of conditions (commented), everything works just fine (excluding showing a double result).
What causes a problem here?



555 is just a testing value, actually any number made of 5s or 6s will do the work.










share|improve this question


















  • 1




    Local variables in a function stop existing when the function returns
    – M.M
    Nov 12 at 1:24










  • Okay, small correction - after adding printf in one of conditions, modified string won't be shown twice if the other condition was fulfilled
    – Igor
    Nov 12 at 1:26










  • suggest replacing: int i=strlen(*tab); int o=i; while((*tab)[i]!='5' && i>=0) { i--; } with a call to strrchr() as that function's purpose is to find a char, starting at the end of the supplied string
    – user3629249
    Nov 12 at 2:20










  • suggest defining array in main() and adding the array as a parameter to increase()
    – user3629249
    Nov 12 at 2:22
















0














#include <stdio.h>
#include <string.h>
void increase(char **tab)
{
int i=strlen(*tab);
int o=i;
while((*tab)[i]!='5' && i>=0)
{
i--;
}
if(i>=0)
{
int tt;
char array[o+1];
for(tt=0;tt<o;tt++)
{
if(tt!=i)
array[tt]=(*tab)[tt];
else
array[tt]='6';
}
array[o]='';
*tab=array;
// printf("n%s",*tab);
}
else
{
char array[o+2];
int tt;
for(tt=0;tt<=o;tt++)
{
array[tt]='5';
}
array[o+1]='';
*tab=array;
// printf("n%s",*tab);
}
}
int main()
{
int n;
char *test;
test="555";
increase(&test);
printf("n%s",test);

return 0;
}


Okay, so increase() is meant to replace number in test with the next number containing only 5 and 6.
What I wanted to do is to change the value of test directly by using pointer to char*. Everything seems to work just fine untill it comes to displaying changed value - it simply won't show unless was asked to do it inside the
increase() function. Once I add



printf("n%s",*tab);


inside any of conditions (commented), everything works just fine (excluding showing a double result).
What causes a problem here?



555 is just a testing value, actually any number made of 5s or 6s will do the work.










share|improve this question


















  • 1




    Local variables in a function stop existing when the function returns
    – M.M
    Nov 12 at 1:24










  • Okay, small correction - after adding printf in one of conditions, modified string won't be shown twice if the other condition was fulfilled
    – Igor
    Nov 12 at 1:26










  • suggest replacing: int i=strlen(*tab); int o=i; while((*tab)[i]!='5' && i>=0) { i--; } with a call to strrchr() as that function's purpose is to find a char, starting at the end of the supplied string
    – user3629249
    Nov 12 at 2:20










  • suggest defining array in main() and adding the array as a parameter to increase()
    – user3629249
    Nov 12 at 2:22














0












0








0







#include <stdio.h>
#include <string.h>
void increase(char **tab)
{
int i=strlen(*tab);
int o=i;
while((*tab)[i]!='5' && i>=0)
{
i--;
}
if(i>=0)
{
int tt;
char array[o+1];
for(tt=0;tt<o;tt++)
{
if(tt!=i)
array[tt]=(*tab)[tt];
else
array[tt]='6';
}
array[o]='';
*tab=array;
// printf("n%s",*tab);
}
else
{
char array[o+2];
int tt;
for(tt=0;tt<=o;tt++)
{
array[tt]='5';
}
array[o+1]='';
*tab=array;
// printf("n%s",*tab);
}
}
int main()
{
int n;
char *test;
test="555";
increase(&test);
printf("n%s",test);

return 0;
}


Okay, so increase() is meant to replace number in test with the next number containing only 5 and 6.
What I wanted to do is to change the value of test directly by using pointer to char*. Everything seems to work just fine untill it comes to displaying changed value - it simply won't show unless was asked to do it inside the
increase() function. Once I add



printf("n%s",*tab);


inside any of conditions (commented), everything works just fine (excluding showing a double result).
What causes a problem here?



555 is just a testing value, actually any number made of 5s or 6s will do the work.










share|improve this question













#include <stdio.h>
#include <string.h>
void increase(char **tab)
{
int i=strlen(*tab);
int o=i;
while((*tab)[i]!='5' && i>=0)
{
i--;
}
if(i>=0)
{
int tt;
char array[o+1];
for(tt=0;tt<o;tt++)
{
if(tt!=i)
array[tt]=(*tab)[tt];
else
array[tt]='6';
}
array[o]='';
*tab=array;
// printf("n%s",*tab);
}
else
{
char array[o+2];
int tt;
for(tt=0;tt<=o;tt++)
{
array[tt]='5';
}
array[o+1]='';
*tab=array;
// printf("n%s",*tab);
}
}
int main()
{
int n;
char *test;
test="555";
increase(&test);
printf("n%s",test);

return 0;
}


Okay, so increase() is meant to replace number in test with the next number containing only 5 and 6.
What I wanted to do is to change the value of test directly by using pointer to char*. Everything seems to work just fine untill it comes to displaying changed value - it simply won't show unless was asked to do it inside the
increase() function. Once I add



printf("n%s",*tab);


inside any of conditions (commented), everything works just fine (excluding showing a double result).
What causes a problem here?



555 is just a testing value, actually any number made of 5s or 6s will do the work.







c arrays string pointers char






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 1:20









Igor

193




193








  • 1




    Local variables in a function stop existing when the function returns
    – M.M
    Nov 12 at 1:24










  • Okay, small correction - after adding printf in one of conditions, modified string won't be shown twice if the other condition was fulfilled
    – Igor
    Nov 12 at 1:26










  • suggest replacing: int i=strlen(*tab); int o=i; while((*tab)[i]!='5' && i>=0) { i--; } with a call to strrchr() as that function's purpose is to find a char, starting at the end of the supplied string
    – user3629249
    Nov 12 at 2:20










  • suggest defining array in main() and adding the array as a parameter to increase()
    – user3629249
    Nov 12 at 2:22














  • 1




    Local variables in a function stop existing when the function returns
    – M.M
    Nov 12 at 1:24










  • Okay, small correction - after adding printf in one of conditions, modified string won't be shown twice if the other condition was fulfilled
    – Igor
    Nov 12 at 1:26










  • suggest replacing: int i=strlen(*tab); int o=i; while((*tab)[i]!='5' && i>=0) { i--; } with a call to strrchr() as that function's purpose is to find a char, starting at the end of the supplied string
    – user3629249
    Nov 12 at 2:20










  • suggest defining array in main() and adding the array as a parameter to increase()
    – user3629249
    Nov 12 at 2:22








1




1




Local variables in a function stop existing when the function returns
– M.M
Nov 12 at 1:24




Local variables in a function stop existing when the function returns
– M.M
Nov 12 at 1:24












Okay, small correction - after adding printf in one of conditions, modified string won't be shown twice if the other condition was fulfilled
– Igor
Nov 12 at 1:26




Okay, small correction - after adding printf in one of conditions, modified string won't be shown twice if the other condition was fulfilled
– Igor
Nov 12 at 1:26












suggest replacing: int i=strlen(*tab); int o=i; while((*tab)[i]!='5' && i>=0) { i--; } with a call to strrchr() as that function's purpose is to find a char, starting at the end of the supplied string
– user3629249
Nov 12 at 2:20




suggest replacing: int i=strlen(*tab); int o=i; while((*tab)[i]!='5' && i>=0) { i--; } with a call to strrchr() as that function's purpose is to find a char, starting at the end of the supplied string
– user3629249
Nov 12 at 2:20












suggest defining array in main() and adding the array as a parameter to increase()
– user3629249
Nov 12 at 2:22




suggest defining array in main() and adding the array as a parameter to increase()
– user3629249
Nov 12 at 2:22












1 Answer
1






active

oldest

votes


















2














Lines like *tab=array; cause a problem, given that array is a local variable that goes out of scope when its enclosing block ends. In main(), test now points to memory that's invalid, and trying to use it is undefined behavior.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254886%2fafter-modifying-string-char-in-a-function-the-value-wont-show-unless-printed%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Lines like *tab=array; cause a problem, given that array is a local variable that goes out of scope when its enclosing block ends. In main(), test now points to memory that's invalid, and trying to use it is undefined behavior.






    share|improve this answer


























      2














      Lines like *tab=array; cause a problem, given that array is a local variable that goes out of scope when its enclosing block ends. In main(), test now points to memory that's invalid, and trying to use it is undefined behavior.






      share|improve this answer
























        2












        2








        2






        Lines like *tab=array; cause a problem, given that array is a local variable that goes out of scope when its enclosing block ends. In main(), test now points to memory that's invalid, and trying to use it is undefined behavior.






        share|improve this answer












        Lines like *tab=array; cause a problem, given that array is a local variable that goes out of scope when its enclosing block ends. In main(), test now points to memory that's invalid, and trying to use it is undefined behavior.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 1:25









        Shawn

        3,4531613




        3,4531613






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254886%2fafter-modifying-string-char-in-a-function-the-value-wont-show-unless-printed%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues