-4
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *s1, *s2, *s3;
 int length, len1=0,len2=0, i, j;
  clrscr();
  s1=(char*)malloc(20* sizeof(s1));
  s2=(char*)malloc(20* sizeof(s2));
 printf("Enter first string\n");
   gets(s1);
  len1=strlen(s1);
   printf("Enter second string\n");
  gets(s2);
 len2=strlen(s2);
 length=len1+len2;
  s3= (char*)malloc((length+2)* sizeof(s3));
   for(i=0;i<len1;++i)
  *(s3+i) =*(s1+i);
  *(s3+i)=' '; /*leave a space at end of first string */
   ++i;
   for(j=0;j<len2;++j)
  { *(s3+i)=*(s2+j); /* copying 2nd string */
    ++i;
  }
  *(s3+i)='\0'; /* store '\0' at end to set 'end of string' */
  printf("Concatenated string is\n%s", s3);
  getch();
   }

Can you please point out the errors in this code, which is used to concatenate two strings... it is showing too many errors first its asking for a prototype for malloc function..

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 2
    Show what errors you get, *format the code* and include malloc.h. And do you know that you can use `a[i]` instead of `*(a+i)` ? – deviantfan Apr 04 '14 at 17:36
  • 1
    This question is off-topic because it belongs at http://codereview.stackexchange.com/ – nobody Apr 04 '14 at 17:36
  • 2
    `#include `, `char *var = malloc(size*sizeof(*var));` – BLUEPIXY Apr 04 '14 at 17:38
  • Once you have included all required header files your code kind of works. writing `malloc(20* sizeof(s1))` is pointless, `sizeof(s1)` being 4 (or 8 on a 64 bit computer). You could just write `malloc(80)`, but that's pointless too, it would be easier to declare `char s1[80] ; char s2[80] ;` and drop the 2 mallocs at the start. – Jabberwocky Apr 04 '14 at 17:46

1 Answers1

0

By looking at Your code i can say that you are using Turbo-c compiler. You need to add #include<alloc.h>in your code.Your Code is correct and worked fine on my gcc compiler(after some compiler specific adjustment).

But Some important points to learn(sorry if you already know them):-

1) *(s1+1)=s1[1] or *(s1+0)=s1[0] (you can use them interchangeably)

2) If you are learner its fine to use gets() as starter. But beware its not safe because in your Code you allocated 20 bytes of memory that means you can give 20 characters as input.But what if you give input greater than 20 characters ??? gets() function will store remaining characters beyond your allocated 20 bytes. This beyond memory is used by neighboring variables of program.In this Case neighboring variable is s2. Hence there is chance of changing values of other variables(without knowing).Try storing string of length greater than 20 in s1 and print both s1 and s2.(you will know what am saying.)(This is classic problem).Please Learn to use scanset to read strings as fast as you can.

3)After everything you forgot to free the memory.

4) Use a new Compiler(ex:- GCC),Because Turbo c is made to generate code for computers which are of 30 years old from now.Its Better to use new compilers,You can Find more help and support if you do.

Mysterious Jack
  • 621
  • 6
  • 18
  • 1
    It's **NEVER** fine to use `gets()`. – pmg Apr 05 '14 at 10:08
  • Yes i agree but as a starter these memory allocation concepts may be difficult to understand.Hence i feel we can use gets() (only in class rooms or home not in the field)till we are comfortable with those concepts. – Mysterious Jack Apr 05 '14 at 10:10