Thread: the length of my array is not what I expected

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    20

    the length of my array is not what I expected

    Hi All

    I just tried the following program
    Code:
    #include <stdio.h>
    #include <string.h>
    
    main() {
    	char c[5] ;
    	int x ;
    	for( x= 0; x < 5; x++ ) {
    			c[x] = 'c' ;
    	}
    	printf("\n%s l=%d\n", c, strlen(c)) ;
    
    }
    If I run this code it tells me the length of the array is 6!! Also when I let the loop go from 0 to 4 the length is still 6. What happened with the length of 5 ?

    The result of the following example did also surprise me:
    Code:
    	char d[5] ;
    	strcpy(d, "1234567890123" ) ;
    	printf("\n%s l=%d\n", d, strlen(d)) ;
    The length was 13, how is that possible. Interesting to say is that when I make that string 1 character longer the progrem executes, telling me the length is 14, but it ends with an segmentation fault

    How is all this possible ?

    Thnx a lot
    LuCa

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    strlen goes from the array pointer to the first NULL character. It has no way of knowing that d is defined to fit 5 characters. The fact that your first function gave you a length of six says that their happened to be a NULL after the array's memory. Both calls to strlen go out of the array bounds and could cause a segmentation fault.
    Sent from my iPad®

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    20
    So I should declare this array 1 element longer than I actually need, for this NULL character ?

    LuCa

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If you're planning on using string functions, yes.
    Sent from my iPad®

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by luca
    So I should declare this array 1 element longer than I actually need, for this NULL character ?

    LuCa
    Yes
    And you should put this null character there
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by vart
    Yes
    And you should put this null character there
    Though keep in mind that string functions such as strcpy and strcat put it in for you. Only if you were to assign to the string character by character would you have to put the null in yourself.
    Sent from my iPad®

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    However, functions like strncpy do not guarantee a null byte.


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

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    20
    thnx a lot!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. length of char array
    By taurus in forum C Programming
    Replies: 41
    Last Post: 09-22-2007, 05:33 PM
  2. Array length problem
    By Brewer in forum C Programming
    Replies: 10
    Last Post: 12-06-2006, 01:10 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM