-4

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]='\0';

How do I store the characters in a String variable ?

Any small help is highly appreciated, Thanks! :)

user58736
  • 9
  • 8
  • 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 '19 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 '19 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](https://devblogs.microsoft.com/oldnewthing/20140627-00/?p=633). – Konrad Rudolph Mar 25 '19 at 11:52
  • @KonradRudolph it is true, but then there should be an attempt at creating a [mcve] that would *ensure* that it is not a case of UB travelling backwards in time... – Antti Haapala -- Слава Україні Mar 25 '19 at 11:58
  • 1
    There are less lines in the code than there are *errors*. – Antti Haapala -- Слава Україні Mar 25 '19 at 12:00
  • What is you code supposed to do? Please [edit] your question and show an example of input and expected output and explain what the program is supposed to do. – Jabberwocky Mar 25 '19 at 12:17

2 Answers2

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.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
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.

user803422
  • 2,636
  • 2
  • 18
  • 36