Thread: send strings into function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    2

    send strings into function

    hello everyone this is my first post here

    how i correctly make dynamic allocation to string and then make input from the user ?
    lets say that i dont know the length of the string.

    here is the code i wrote , when i input the strings i dont get an error when i allocate small amount of memory , how i make that correctly

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


    void stringMerge(char*, char*, int n);


    int main()
    {
    char *strA, *strB;
    int insert_pos;


    strA = (char *)malloc(3);
    strB = (char *)malloc(3);
    printf("enter the first string\n");
    gets(strA);
    printf("enter the second string\n");
    gets(strB);
    printf("enter the insert pos of the string\n"); //
    scanf("%d", &insert_pos);


    stringMerge(strA,strB,insert_pos);
    }


    void stringMerge(char *a,char *b ,int n)
    {

    int newString_lenth = strlen(a) + strlen(b) + 1;
    char* newString = (char*)malloc(newString_lenth);



    strncpy(newString, a, n);
    newString[n] = '\0';
    strcat(newString, b);
    strcat(newString, a + n);
    printf("%s\n", newString);


    getch();


    }

    **orginal problem sorted, i updated the qestion.
    Last edited by Vladi Pikovski; 01-01-2017 at 07:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to send a ppm to a function?
    By Cody Hutto in forum C Programming
    Replies: 5
    Last Post: 04-07-2015, 06:47 PM
  2. Send strings to port in Linux
    By Brownie in forum Linux Programming
    Replies: 6
    Last Post: 07-11-2009, 04:46 PM
  3. please send me the function name
    By mazhar in forum Tech Board
    Replies: 3
    Last Post: 04-29-2004, 11:14 AM
  4. one function to send them all
    By WebmasterMattD in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2002, 08:22 AM

Tags for this Thread