Going to give an exam 6 hours later dont understand a lecture code

This is a discussion on Going to give an exam 6 hours later dont understand a lecture code within the C Programming forums, part of the General Programming Boards category; what does this mean? Code: char strg1[] = “Engineering Problem Solving:”; char strg2[] = “fundamental Concepts”, strg3[50]; strncpy(strg3, strg1, strlen(strg1)+1); ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    14

    Going to give an exam 6 hours later dont understand a lecture code

    what does this mean?
    Code:
    char strg1[] = “Engineering Problem Solving:”;
      char strg2[] = “fundamental Concepts”, strg3[50];
    
      strncpy(strg3, strg1, strlen(strg1)+1); 
    /*copy strlen+1 characters*/
       /*if enough room*/
      if (strlen(strg1)+strlen(strg2) < strlen(strg3)
        {
      strncat(strg3, strg2, strlen(strg2)+1);
      }
      char strg1[] = “Engineering Problem Solving:”;
      char strg2[] = “fundamental Concepts”, strg3[50];
    
      strncpy(strg3, strg1, strlen(strg1)+1);
    /*copy strlen+1 characters*/
       /*if enough room*/
      if (strlen(strg1)+strlen(strg2) < strlen(strg3)
        {
      strncat(strg3, strg2, strlen(strg2)+1);
      }

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Despite their names, strncpy() and strncat() were not designed to be used with strings.
    I suggest you do not use them.

    Also your string delimiters are not part of the Basic character set and are a syntax error.

    Your ifs are never going to be true.

    You cannot define the same identifier more than once.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by qny View Post
    Despite their names, strncpy() and strncat() were not designed to be used with strings.
    I suggest you do not use them.
    I don't know where you could possibly have got this impression from - these functions work with null-terminated char arrays. In C, this is a string.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,294
    All the code is trying to do is to copy strg1 into strg3, and append strg2.

    However, the way it does it will not work. Whoever wrote that code does not understand how C string functions work.

    The strlen(strg1)+1 or strlen(strg2)+1 as third argument to strncat() and strncpy() respectively is silliness, as both functions will stop (at the zero terminator), and that occurs before strlen()+1 characters are copied.

    The "if (strlen(strg1) + strlen(strg2) < strlen(strg3))" will always test false, since it comes immediately after the statement "strncpy(strg3, strg1, strlen(strg1)+1)" which will - as a side effect - ensure strlen(strg1) and strlen(strg3) are equal.

    What the code therefore actually does is copy the string strg1 to strg3. Nothing more, nothing less.

    (I'm assuming the code being repeated twice is a copy/paste error when posting).


    Quote Originally Posted by qny View Post
    Despite their names, strncpy() and strncat() were not designed to be used with strings.
    I suggest you do not use them.
    Smoking the whacky tabacky tonight, eh?
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    But strncpy does not always append a \0, so the result might not be a proper C-string, in the traditional sense.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by JohnGraham View Post
    I don't know where you could possibly have got this impression from - these functions work with null-terminated char arrays. In C, this is a string.
    Unlike "proper string functions" they also work with non null-terminated char arrays.

    Code:
    char a[12] = "not a string";
    char b[18] = "another non-string";
    char c[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; /* 42 X's; c is a string */
    strncpy(c + 3, a + 4, 5); /* c is "XXXa strXXX..." */
    strncat(c, b + 2, 3); /* c is "...XXXoth" */
    Last edited by qny; 12-07-2012 at 08:08 AM.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by qny View Post
    Unlike "proper string functions" they also work with non null-terminated char arrays.
    Well, the destination of strncat() must be null-terminated, meaning at least one of its parameters needs to be a string in order to use it properly.

    And anyway, just because a function works with things other than what it's intended to is a very strange reason indeed to prompt someone specifically not to use them for what it's intended. I have a wrench that can also be used to hit people on the head - would you suggest I not use it for its normal purpose?
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,294
    Quote Originally Posted by Salem View Post
    But strncpy does not always append a \0, so the result might not be a proper C-string, in the traditional sense.
    strncpy(x, y, len) always copies a '\0' from y when len exceeds strlen(y) - which is what the code shown by the OP does.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 10-05-2012, 01:50 AM
  2. I dont understand this code
    By Hyberboloid in forum C Programming
    Replies: 5
    Last Post: 12-21-2011, 11:44 AM
  3. Dont Understand some code
    By Davidreal in forum C Programming
    Replies: 3
    Last Post: 09-10-2011, 01:38 PM
  4. I dont understand a piece of this code
    By lilbo4231 in forum C Programming
    Replies: 25
    Last Post: 06-13-2011, 04:15 AM
  5. Replies: 2
    Last Post: 05-03-2011, 12:29 AM

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