Thread: send strings into function

  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.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Be sure to use code tags, and that your code is neatly formatted and indented.

    Code:
    #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();
    }
    You're only allocating enough space for 3 characters, one of which must be the string-terminating null character '\0'. You should use a reasonably large size if you don't know what the length of the input would be.

    Additional comments:


  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Another potential problem is that the function could be called in such a way that the expected result is a concatenation, A + B, which would make doing the third step incorrect. It's a simple check.

    Code:
    // Assuming result is a zero'd out buffer of sufficient length
        if (result != NULL)
        {
            strncpy(result, a, n);
            strncat(result, b, lenB);
            if (n < lenA)
            {
                strncat(result, a + n, lenA - n);
            }
        }
    Last edited by whiteflags; 01-02-2017 at 01:17 AM.

  4. #4
    Registered User
    Join Date
    Jan 2017
    Posts
    2
    thank you for your answers .
    on my code example , it works even with space for 3 chars , i inputing a large string and the code works fine , why is that ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,526
    Quote Originally Posted by Vladi Pikovski View Post
    thank you for your answers .
    on my code example , it works even with space for 3 chars , i inputing a large string and the code works fine , why is that ?
    Pure dumb luck.

    If you overrun the bounds of where you know you can access, then the program is broken.
    When the problem shows up can be anywhere between microseconds or years.
    Where the problem shows up is usually in some unrelated part of the program (which makes debugging malloc related memory issues notoriously difficult).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    677
    Quote Originally Posted by Vladi Pikovski View Post
    thank you for your answers .
    on my code example , it works even with space for 3 chars , i inputing a large string and the code works fine , why is that ?
    Windows (Especially older versions) is notorious for not trapping for buffer overruns!

    You are probably using a compiler that uses the C89/90 Standard. The use of gets() would have flagged a warning in C99, (Depreciated) and would be an error in a C11 Standard compliant compiler. gets() has been removed completely from the C11 Standard Library!!!

    Please think about installing a newer compiler that is C99 / C11 compliant.

    Also, please turn on and turn up your warning level to the highest setting to see any errors and warnings the compiler can detect.

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