Problems with strncat

This is a discussion on Problems with strncat within the C Programming forums, part of the General Programming Boards category; Hi there, I am a newbie to c programming, in fact I started yesterday. I am facing a problem which ...

  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
    21,313
    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().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,313
    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");
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21