-3

There are no compiler errors but when I run the program and write the two words and the position the program stops and writes this error:

: process returned 255

#include <stdio.h>
#include <stdlib.h>

char strins(char[],char[],int,int,int);

int main(void)
{
    char zk1[1000];
    char zk2[1000];
    int pos=0;
    int n1=0;
    int n2=0;

    printf("plz enter the first string : ");
    gets(zk1);
    n1=sizeof(zk1)/sizeof(int);
    printf("plz enter the second string : ");
    gets(zk2);
    n2=sizeof(zk2)/sizeof(int);
    printf("plz enter the position");
    scanf("%d",&pos);
    strins(zk1,zk2,pos,n1,n2);
    return 0;
}
char strins(char zk1[],char zk2[],int pos,int n1,int n2)
{
    char zk3[]={(char)malloc(sizeof(zk1)+sizeof(zk2))};
    int ctr=0;
    for(int i=0;i<pos;i++)
    {
        zk3[i]=zk1[i];
        ctr++;
    }
    for(int i=0;i<n2;i++)
    {
        zk3[pos]=zk2[i];
        ctr++;
    }
    for(int i=pos;i<n1;i++)
    {
        zk3[ctr]=zk1[i];
    }
    free(zk3);
    return *zk3;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
heroo
  • 21
  • 4
  • `malloc(sizeof(zk1)+sizeof(zk2))` : `sizeof(zk1)` and `sizeof(zk2)` are `sizeof(char*)`, not `sizeof(char[1000])`. – BLUEPIXY Jan 31 '16 at 03:34
  • ok i write this((char zk3[]={(char)malloc(sizeof(char*)+sizeof(char*))};)) and it gave me the same error could you explaine more please – heroo Jan 31 '16 at 03:47
  • try `char *zk3 = malloc(sizeof(char[1000])+sizeof(char[1000]));` – BLUEPIXY Jan 31 '16 at 03:52
  • or `char *zk3 = malloc(strlen(zk1) + strelen(zk2) +1);` – BLUEPIXY Jan 31 '16 at 03:59
  • Also `zk3[pos]=zk2[i];` : same position's char updated. – BLUEPIXY Jan 31 '16 at 04:01
  • Any other problems aside, you can't `free(zk3);` and then `return *zk3;`. When you free memory, your program no longer can use it. That means you can't do anything with `zk3` except assign a new value to it. –  Jan 31 '16 at 04:02
  • Also change to `char *strins(...` and `/* free(zk3); */ return zk3;` – BLUEPIXY Jan 31 '16 at 04:03
  • thank you for your time but even after that it gave me only the first 2 letters and some Symbols when i (printf) – heroo Jan 31 '16 at 04:17
  • `(char)malloc(sizeof(zk1)+sizeof(zk2))` is extremely wrong. One of the reasons that you [shouldn't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714)) – phuclv Jan 31 '16 at 05:44

1 Answers1

0

fix sample like this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *strins(char s1[], char s2[], int insert_pos, int sizeOfS1, int sizeOfS2);

int main(void){
    char zk1[1000];
    char zk2[1000];
    char *zk3;
    int pos=0;
    int n1, n2;

    printf("plz enter the first string : ");
    scanf("%999[^\n]%*c", zk1);//gets shouldn't use. (already obsolete)
    n1=sizeof(zk1);//strlen(zk1);
    printf("plz enter the second string : ");
    scanf("%999[^\n]%*c", zk2);
    n2=sizeof(zk2);//sizeof(int) --> sizeof(char) but it's 1 by standard
    printf("plz enter the position");
    scanf("%d", &pos);
    zk3 = strins(zk1, zk2, pos, n1, n2);
    puts(zk3);
    free(zk3);
    return 0;
}

char *strins(char zk1[], char zk2[], int pos, int n1, int n2){
    char *zk3 = malloc(n1 + n2);//+1 when use stelen for null-terminate
    int ctr=0;

    for(int i = 0; i < pos; i++){//pos isn't checking of valid
        zk3[ctr++] = zk1[i];
    }
    for(int i = 0; zk2[i]; i++){//loop Until the '\0'
        zk3[ctr++] = zk2[i];
    }
    for(int i = pos; zk1[i]; i++){
        zk3[ctr++] = zk1[i];
    }
    zk3[ctr] = '\0';

    return zk3;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70