Thread: Problems with strncat

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Problems with strncat

    Hi there,

    I am a newbie to c programming, in fact I started yesterday. I am facing a problem which I can't understand.

    Given the following code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    	char* buffer = (char *)malloc(sizeof(char)*6);
    	
    	strncat(buffer, "ab", 2);
    	strncat(buffer, "de", 2);
    	
    	printf(buffer);
    	
    	free(buffer);
    	
    	return 1;
    }
    Output:
    # gcc -Wall -ansi test.c -o test
    # ./test
    abde

    However, if I change the two strncat lines to...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    	char* buffer = (char *)malloc(sizeof(char)*6);
    	
    	strncat(buffer, "%s", 2);
    	strncat(buffer, "%s", 2);
    	
    	printf(buffer);
    	
    	free(buffer);
    	
    	return 1;
    }
    # gcc -Wall -ansi test.c -o test
    # ./test
    Segmentation fault

    Question: Why segmentation fault? What's so special about %s that causes this error?

    Thanks in advance.

    Regards,
    CashCow01

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, one problem with the second example is that you are missing two arguments to printf that should correspond to the "%s%s" format string.

    One problem with both examples is that you do not append a null character after using strncat().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Well, one problem with the second example is that you are missing two arguments to printf that should correspond to the "%s%s" format string.

    One problem with both examples is that you do not append a null character after using strncat().
    Well, I believe the two arguments are not compulsory. Suppose I want to print the literal string "%s%s" without any format. How do I do that?

    Thanks for pointing out the null character though.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    	char* buffer = (char *)malloc(sizeof(char)*6);
    	
    	strncat(buffer, "%s%s\0", 4);
    	
    	printf(buffer);
    	
    	free(buffer);
    	
    	return 1;
    }
    # gcc -Wall -ansi test.c -o test
    # ./test
    Segmentation fault

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Count with me:
    % - 1
    s - 2
    % - 3
    s - 4

    Oh and you still have the problem with you using % specifiers in printf and not actually providing arguments for those.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CashCow01
    Well, I believe the two arguments are not compulsory.
    They are not compulsory, but leaving them out results in undefined behaviour. So, they are compulsory unless you want a potential bug.

    Quote Originally Posted by CashCow01
    Suppose I want to print the literal string "%s%s" without any format. How do I do that?
    With printf, you would do this:
    Code:
    printf("%%s%%s");
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Quote Originally Posted by laserlight View Post
    They are not compulsory, but leaving them out results in undefined behaviour. So, they are compulsory unless you want a potential bug.


    With printf, you would do this:
    Code:
    printf("%%s%%s");
    Thanks laserlight . Didn't know that % is an escape character by itself.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CashCow01 View Post
    Thanks laserlight . Didn't know that % is an escape character by itself.
    It only is with the *printf/*scanf functions. It's not in normal strings or as a normal character.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by CashCow01 View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    	char* buffer = (char *)malloc(sizeof(char)*6);
    	
    	strncat(buffer, "%s%s\0", 4);
    	
    	printf(buffer);
    	
    	free(buffer);
    	
    	return 1;
    }
    # gcc -Wall -ansi test.c -o test
    # ./test
    Segmentation fault
    Your code is still bad/flawed even after the proposed fixes.
    First of all, after allocation buffer can contain anything. Specifically, the first character doesn't need to be 0, meaning that the first strncat won't actually work.
    Also, don't cast malloc in C.
    Finally, sizeof(char) is defined to be 1, so you don't need to multiply 6 by sizeof(char).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strncat and Realloc - pointer issues
    By bhenderson in forum C Programming
    Replies: 6
    Last Post: 08-16-2009, 01:59 PM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

Tags for this Thread