How to append obtained char values in a loop into a String, to pass it to ShellExecute()? [closed]How do you pass a function as a parameter in C?How to initialize all members of an array to the same value?char** to cope with string?Using the scanf() functionIs there a way to print strings pointed to by array of char pointers in C?Why is my tokenize loop not properly populating array of char-array-pointersArray of chars from an array of pointer to strings in cGetting the values of a char array in a loop pointed by another pointer to the arrayWant to store “char values” in array (in C language)String prints after bounds

How often can a PC check with passive perception during a combat turn?

Do equal angles necessarily mean a polygon is regular?

First-year PhD giving a talk among well-established researchers in the field

Does image quality of the lens affect "focus and recompose" technique?

Does the Paladin's Aura of Protection affect only either her or ONE ally in range?

Are there any vegetarian astronauts?

Does anycast addressing add additional latency in any way?

Does ultrasonic bath cleaning damage laboratory volumetric glassware calibration?

Is my Rep in Stack-Exchange Form?

Is this one of the engines from the 9/11 aircraft?

Links to webpages in books

Why is C++ initial allocation so much larger than C's?

Why would people reject a god's purely beneficial blessing?

Is there a short way to compare many values mutually at same time without using multiple 'and's?

Cascading Repair Costs following Blown Head Gasket on a 2004 Subaru Outback

Intuitively, why does putting capacitors in series decrease the equivalent capacitance?

Using “sparkling” as a diminutive of “spark” in a poem

How to positively portray high and mighty characters?

Pull-up sequence accumulator counter

Calculating the partial sum of a expl3 sequence

Is adding a new player (or players) a DM decision, or a group decision?

Ending: accusative or not?

How to append a matrix element by element?

The use of "I" and "we" used in the same sentence and other questions



How to append obtained char values in a loop into a String, to pass it to ShellExecute()? [closed]


How do you pass a function as a parameter in C?How to initialize all members of an array to the same value?char** to cope with string?Using the scanf() functionIs there a way to print strings pointed to by array of char pointers in C?Why is my tokenize loop not properly populating array of char-array-pointersArray of chars from an array of pointer to strings in cGetting the values of a char array in a loop pointed by another pointer to the arrayWant to store “char values” in array (in C language)String prints after bounds






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-3















If the variable 'S' is inside a for loop, which stores the char value of the Binary value inputted by the user,
how to append all the characters to form a String which I can pass to a ShellExecute() fucntion.



char *u="https://google.com"; 
ShellExecute(NULL, "open",u, NULL, NULL, SW_SHOWNORMAL); //this will open a webpage


^^^ Binary values should be input in such a way that 'u' is a URL that opens in the browser. This will be taken care by the user. Just referenced it.



The following, is to convert Binary -> Dec-> Char:



for(i=0; i<21; i++)
scanf("%ld",&n[i]); //Binary

for(i=0; i<21;i++)

k=n[i], c=0, decimalNumber=0;
while(k!=0)

remainder = k%10;
k /= 10;
decimalNumber += remainder*pow(2,c); //Dec
++c;

char S=decimalNumber; //Char
.
. //how do I join all the Char I am getting here in one String
.



Using this way causes A LOT OF ISSUES, but this was the only way I figured after doing a ton of reading...



char *u1=NULL;
char *u=NULL; //just after void main()
.
.
. //after I store char in 'S'
u1=u; //'u1' stores the string as 'u' gets deallocated every loop
size_t len=strlen(u1);
u=malloc(len + 1 + 1 );
strcat(u,u1); //copies the value of 'u1' from previous iteration to 'u'
u[len]=t;
u[len+1]='';


How do I store the characters in a String variable ?



Any small help is highly appreciated, Thanks! :)










share|improve this question















closed as too broad by Antti Haapala, tripleee, AdrianHHH, M-M, double-beep Mar 25 at 18:41


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.













  • 2





    printf("%s",u); is printing garbage before ShellExecute is called, so it's unrelated to ShellExecute. And your code is not infected, it just contains bug. You need to debug this. And what is printf("%s",u); suposed to print and what do you ShellExecute expect to do?

    – Jabberwocky
    Mar 25 at 11:32












  • Oh I see it now, there are many other issues. You should read the chapter dealing with strings in your C text book.

    – Jabberwocky
    Mar 25 at 11:34












  • @Jabberwocky Just because the effect is before the cause doesn’t mean it’s unrelated. In fact, it’s a well-known hallmark of undefined behaviour that it can have effects “in the past”, so to speak. See Undefined behavior can result in time travel by Raymond Chen.

    – Konrad Rudolph
    Mar 25 at 11:52












  • @KonradRudolph it is true, but then there should be an attempt at creating a minimal reproducible example that would ensure that it is not a case of UB travelling backwards in time...

    – Antti Haapala
    Mar 25 at 11:58






  • 1





    There are less lines in the code than there are errors.

    – Antti Haapala
    Mar 25 at 12:00

















-3















If the variable 'S' is inside a for loop, which stores the char value of the Binary value inputted by the user,
how to append all the characters to form a String which I can pass to a ShellExecute() fucntion.



char *u="https://google.com"; 
ShellExecute(NULL, "open",u, NULL, NULL, SW_SHOWNORMAL); //this will open a webpage


^^^ Binary values should be input in such a way that 'u' is a URL that opens in the browser. This will be taken care by the user. Just referenced it.



The following, is to convert Binary -> Dec-> Char:



for(i=0; i<21; i++)
scanf("%ld",&n[i]); //Binary

for(i=0; i<21;i++)

k=n[i], c=0, decimalNumber=0;
while(k!=0)

remainder = k%10;
k /= 10;
decimalNumber += remainder*pow(2,c); //Dec
++c;

char S=decimalNumber; //Char
.
. //how do I join all the Char I am getting here in one String
.



Using this way causes A LOT OF ISSUES, but this was the only way I figured after doing a ton of reading...



char *u1=NULL;
char *u=NULL; //just after void main()
.
.
. //after I store char in 'S'
u1=u; //'u1' stores the string as 'u' gets deallocated every loop
size_t len=strlen(u1);
u=malloc(len + 1 + 1 );
strcat(u,u1); //copies the value of 'u1' from previous iteration to 'u'
u[len]=t;
u[len+1]='';


How do I store the characters in a String variable ?



Any small help is highly appreciated, Thanks! :)










share|improve this question















closed as too broad by Antti Haapala, tripleee, AdrianHHH, M-M, double-beep Mar 25 at 18:41


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.













  • 2





    printf("%s",u); is printing garbage before ShellExecute is called, so it's unrelated to ShellExecute. And your code is not infected, it just contains bug. You need to debug this. And what is printf("%s",u); suposed to print and what do you ShellExecute expect to do?

    – Jabberwocky
    Mar 25 at 11:32












  • Oh I see it now, there are many other issues. You should read the chapter dealing with strings in your C text book.

    – Jabberwocky
    Mar 25 at 11:34












  • @Jabberwocky Just because the effect is before the cause doesn’t mean it’s unrelated. In fact, it’s a well-known hallmark of undefined behaviour that it can have effects “in the past”, so to speak. See Undefined behavior can result in time travel by Raymond Chen.

    – Konrad Rudolph
    Mar 25 at 11:52












  • @KonradRudolph it is true, but then there should be an attempt at creating a minimal reproducible example that would ensure that it is not a case of UB travelling backwards in time...

    – Antti Haapala
    Mar 25 at 11:58






  • 1





    There are less lines in the code than there are errors.

    – Antti Haapala
    Mar 25 at 12:00













-3












-3








-3


1






If the variable 'S' is inside a for loop, which stores the char value of the Binary value inputted by the user,
how to append all the characters to form a String which I can pass to a ShellExecute() fucntion.



char *u="https://google.com"; 
ShellExecute(NULL, "open",u, NULL, NULL, SW_SHOWNORMAL); //this will open a webpage


^^^ Binary values should be input in such a way that 'u' is a URL that opens in the browser. This will be taken care by the user. Just referenced it.



The following, is to convert Binary -> Dec-> Char:



for(i=0; i<21; i++)
scanf("%ld",&n[i]); //Binary

for(i=0; i<21;i++)

k=n[i], c=0, decimalNumber=0;
while(k!=0)

remainder = k%10;
k /= 10;
decimalNumber += remainder*pow(2,c); //Dec
++c;

char S=decimalNumber; //Char
.
. //how do I join all the Char I am getting here in one String
.



Using this way causes A LOT OF ISSUES, but this was the only way I figured after doing a ton of reading...



char *u1=NULL;
char *u=NULL; //just after void main()
.
.
. //after I store char in 'S'
u1=u; //'u1' stores the string as 'u' gets deallocated every loop
size_t len=strlen(u1);
u=malloc(len + 1 + 1 );
strcat(u,u1); //copies the value of 'u1' from previous iteration to 'u'
u[len]=t;
u[len+1]='';


How do I store the characters in a String variable ?



Any small help is highly appreciated, Thanks! :)










share|improve this question
















If the variable 'S' is inside a for loop, which stores the char value of the Binary value inputted by the user,
how to append all the characters to form a String which I can pass to a ShellExecute() fucntion.



char *u="https://google.com"; 
ShellExecute(NULL, "open",u, NULL, NULL, SW_SHOWNORMAL); //this will open a webpage


^^^ Binary values should be input in such a way that 'u' is a URL that opens in the browser. This will be taken care by the user. Just referenced it.



The following, is to convert Binary -> Dec-> Char:



for(i=0; i<21; i++)
scanf("%ld",&n[i]); //Binary

for(i=0; i<21;i++)

k=n[i], c=0, decimalNumber=0;
while(k!=0)

remainder = k%10;
k /= 10;
decimalNumber += remainder*pow(2,c); //Dec
++c;

char S=decimalNumber; //Char
.
. //how do I join all the Char I am getting here in one String
.



Using this way causes A LOT OF ISSUES, but this was the only way I figured after doing a ton of reading...



char *u1=NULL;
char *u=NULL; //just after void main()
.
.
. //after I store char in 'S'
u1=u; //'u1' stores the string as 'u' gets deallocated every loop
size_t len=strlen(u1);
u=malloc(len + 1 + 1 );
strcat(u,u1); //copies the value of 'u1' from previous iteration to 'u'
u[len]=t;
u[len+1]='';


How do I store the characters in a String variable ?



Any small help is highly appreciated, Thanks! :)







c pointers visual-studio-code shellexecute






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 19:51







user58736

















asked Mar 25 at 11:15









user58736user58736

66 bronze badges




66 bronze badges




closed as too broad by Antti Haapala, tripleee, AdrianHHH, M-M, double-beep Mar 25 at 18:41


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as too broad by Antti Haapala, tripleee, AdrianHHH, M-M, double-beep Mar 25 at 18:41


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 2





    printf("%s",u); is printing garbage before ShellExecute is called, so it's unrelated to ShellExecute. And your code is not infected, it just contains bug. You need to debug this. And what is printf("%s",u); suposed to print and what do you ShellExecute expect to do?

    – Jabberwocky
    Mar 25 at 11:32












  • Oh I see it now, there are many other issues. You should read the chapter dealing with strings in your C text book.

    – Jabberwocky
    Mar 25 at 11:34












  • @Jabberwocky Just because the effect is before the cause doesn’t mean it’s unrelated. In fact, it’s a well-known hallmark of undefined behaviour that it can have effects “in the past”, so to speak. See Undefined behavior can result in time travel by Raymond Chen.

    – Konrad Rudolph
    Mar 25 at 11:52












  • @KonradRudolph it is true, but then there should be an attempt at creating a minimal reproducible example that would ensure that it is not a case of UB travelling backwards in time...

    – Antti Haapala
    Mar 25 at 11:58






  • 1





    There are less lines in the code than there are errors.

    – Antti Haapala
    Mar 25 at 12:00












  • 2





    printf("%s",u); is printing garbage before ShellExecute is called, so it's unrelated to ShellExecute. And your code is not infected, it just contains bug. You need to debug this. And what is printf("%s",u); suposed to print and what do you ShellExecute expect to do?

    – Jabberwocky
    Mar 25 at 11:32












  • Oh I see it now, there are many other issues. You should read the chapter dealing with strings in your C text book.

    – Jabberwocky
    Mar 25 at 11:34












  • @Jabberwocky Just because the effect is before the cause doesn’t mean it’s unrelated. In fact, it’s a well-known hallmark of undefined behaviour that it can have effects “in the past”, so to speak. See Undefined behavior can result in time travel by Raymond Chen.

    – Konrad Rudolph
    Mar 25 at 11:52












  • @KonradRudolph it is true, but then there should be an attempt at creating a minimal reproducible example that would ensure that it is not a case of UB travelling backwards in time...

    – Antti Haapala
    Mar 25 at 11:58






  • 1





    There are less lines in the code than there are errors.

    – Antti Haapala
    Mar 25 at 12:00







2




2





printf("%s",u); is printing garbage before ShellExecute is called, so it's unrelated to ShellExecute. And your code is not infected, it just contains bug. You need to debug this. And what is printf("%s",u); suposed to print and what do you ShellExecute expect to do?

– Jabberwocky
Mar 25 at 11:32






printf("%s",u); is printing garbage before ShellExecute is called, so it's unrelated to ShellExecute. And your code is not infected, it just contains bug. You need to debug this. And what is printf("%s",u); suposed to print and what do you ShellExecute expect to do?

– Jabberwocky
Mar 25 at 11:32














Oh I see it now, there are many other issues. You should read the chapter dealing with strings in your C text book.

– Jabberwocky
Mar 25 at 11:34






Oh I see it now, there are many other issues. You should read the chapter dealing with strings in your C text book.

– Jabberwocky
Mar 25 at 11:34














@Jabberwocky Just because the effect is before the cause doesn’t mean it’s unrelated. In fact, it’s a well-known hallmark of undefined behaviour that it can have effects “in the past”, so to speak. See Undefined behavior can result in time travel by Raymond Chen.

– Konrad Rudolph
Mar 25 at 11:52






@Jabberwocky Just because the effect is before the cause doesn’t mean it’s unrelated. In fact, it’s a well-known hallmark of undefined behaviour that it can have effects “in the past”, so to speak. See Undefined behavior can result in time travel by Raymond Chen.

– Konrad Rudolph
Mar 25 at 11:52














@KonradRudolph it is true, but then there should be an attempt at creating a minimal reproducible example that would ensure that it is not a case of UB travelling backwards in time...

– Antti Haapala
Mar 25 at 11:58





@KonradRudolph it is true, but then there should be an attempt at creating a minimal reproducible example that would ensure that it is not a case of UB travelling backwards in time...

– Antti Haapala
Mar 25 at 11:58




1




1





There are less lines in the code than there are errors.

– Antti Haapala
Mar 25 at 12:00





There are less lines in the code than there are errors.

– Antti Haapala
Mar 25 at 12:00












2 Answers
2






active

oldest

votes


















2














There are a number of issues in your code which may lead to memory corruption.



char *u1=malloc (sizeof 25);


I presume this is supposed to allocate 25 characters? it actually allocates sizeof(25) or sizeof(int) characters which is probably 4. You just need malloc(25).



char *u="";


This creates a read only empty string literal, you cannot modify its value.



u1=u;


This throws away the string you previously allocated and causes a memory leak.



strcat(u,u1)


The memory in u is uninitialised, you need to call memset(u, 0, len + 2) before you call this otherwise u1 will be appended after the first null character in u, u may well not contain any null characters so u1 could be written anywhere in memory.






share|improve this answer






























    1














    You have a mistake there, which might well explain the garbage:



     u=malloc(len + 1 + 1 );
    strcat(u,u1);


    On the malloc line (at the first iteration), you lose the u that was initialized by char *u="";.
    The malloc might return an uninitalized memory area (with garbage), and strcat copies u1 AFTER the garbage.






    share|improve this answer































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      There are a number of issues in your code which may lead to memory corruption.



      char *u1=malloc (sizeof 25);


      I presume this is supposed to allocate 25 characters? it actually allocates sizeof(25) or sizeof(int) characters which is probably 4. You just need malloc(25).



      char *u="";


      This creates a read only empty string literal, you cannot modify its value.



      u1=u;


      This throws away the string you previously allocated and causes a memory leak.



      strcat(u,u1)


      The memory in u is uninitialised, you need to call memset(u, 0, len + 2) before you call this otherwise u1 will be appended after the first null character in u, u may well not contain any null characters so u1 could be written anywhere in memory.






      share|improve this answer



























        2














        There are a number of issues in your code which may lead to memory corruption.



        char *u1=malloc (sizeof 25);


        I presume this is supposed to allocate 25 characters? it actually allocates sizeof(25) or sizeof(int) characters which is probably 4. You just need malloc(25).



        char *u="";


        This creates a read only empty string literal, you cannot modify its value.



        u1=u;


        This throws away the string you previously allocated and causes a memory leak.



        strcat(u,u1)


        The memory in u is uninitialised, you need to call memset(u, 0, len + 2) before you call this otherwise u1 will be appended after the first null character in u, u may well not contain any null characters so u1 could be written anywhere in memory.






        share|improve this answer

























          2












          2








          2







          There are a number of issues in your code which may lead to memory corruption.



          char *u1=malloc (sizeof 25);


          I presume this is supposed to allocate 25 characters? it actually allocates sizeof(25) or sizeof(int) characters which is probably 4. You just need malloc(25).



          char *u="";


          This creates a read only empty string literal, you cannot modify its value.



          u1=u;


          This throws away the string you previously allocated and causes a memory leak.



          strcat(u,u1)


          The memory in u is uninitialised, you need to call memset(u, 0, len + 2) before you call this otherwise u1 will be appended after the first null character in u, u may well not contain any null characters so u1 could be written anywhere in memory.






          share|improve this answer













          There are a number of issues in your code which may lead to memory corruption.



          char *u1=malloc (sizeof 25);


          I presume this is supposed to allocate 25 characters? it actually allocates sizeof(25) or sizeof(int) characters which is probably 4. You just need malloc(25).



          char *u="";


          This creates a read only empty string literal, you cannot modify its value.



          u1=u;


          This throws away the string you previously allocated and causes a memory leak.



          strcat(u,u1)


          The memory in u is uninitialised, you need to call memset(u, 0, len + 2) before you call this otherwise u1 will be appended after the first null character in u, u may well not contain any null characters so u1 could be written anywhere in memory.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 11:38









          Alan BirtlesAlan Birtles

          10.7k1 gold badge13 silver badges36 bronze badges




          10.7k1 gold badge13 silver badges36 bronze badges























              1














              You have a mistake there, which might well explain the garbage:



               u=malloc(len + 1 + 1 );
              strcat(u,u1);


              On the malloc line (at the first iteration), you lose the u that was initialized by char *u="";.
              The malloc might return an uninitalized memory area (with garbage), and strcat copies u1 AFTER the garbage.






              share|improve this answer



























                1














                You have a mistake there, which might well explain the garbage:



                 u=malloc(len + 1 + 1 );
                strcat(u,u1);


                On the malloc line (at the first iteration), you lose the u that was initialized by char *u="";.
                The malloc might return an uninitalized memory area (with garbage), and strcat copies u1 AFTER the garbage.






                share|improve this answer

























                  1












                  1








                  1







                  You have a mistake there, which might well explain the garbage:



                   u=malloc(len + 1 + 1 );
                  strcat(u,u1);


                  On the malloc line (at the first iteration), you lose the u that was initialized by char *u="";.
                  The malloc might return an uninitalized memory area (with garbage), and strcat copies u1 AFTER the garbage.






                  share|improve this answer













                  You have a mistake there, which might well explain the garbage:



                   u=malloc(len + 1 + 1 );
                  strcat(u,u1);


                  On the malloc line (at the first iteration), you lose the u that was initialized by char *u="";.
                  The malloc might return an uninitalized memory area (with garbage), and strcat copies u1 AFTER the garbage.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 25 at 11:34









                  user803422user803422

                  1,2202 gold badges9 silver badges25 bronze badges




                  1,2202 gold badges9 silver badges25 bronze badges













                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript