strlen

Discussion in 'C Programming' started by Gregc., Apr 25, 2006.

  1. Gregc.

    Gregc. Guest

    Hello

    I am trying to get an understanding on strlen. From my understanding,
    it takes a look at a string or single character array and returns the
    length - the number of character in the string. When I try to apply it
    to the following code:

    #include <stdio.h>
    #include <string.h>
    const int MAX_FILENAME = 256;
    void checkFilename (char *filename, char *extension)
    {
    char length[]=filename[MAX_FILENAME];
    int size;
    size = strlen(length);
    return;
    int main () {
    char filename1 [MAX_FILENAME] = "testfile";
    char filename2 [MAX_FILENAME] = "testfile.txt";
    char filename3 [MAX_FILENAME] = "testfile.dat";
    char extension [] = ".txt";

    checkFilename (filename1, extension);
    checkFilename (filename2, extension);
    checkFilename (filename3, extension);

    printf("1: %s\n", filename1);
    printf("2: %s\n", filename2);
    printf("3: %s\n", filename3);
    }

    I get " initializer fails to determine size of `length'" Could someone
    direct me in the right direction. Please note, I'm not looking for the
    answer, but more interested in getting an understanding of what is
    going on.

    Greg
     
    Gregc., Apr 25, 2006
    #1
    1. Advertising

  2. Gregc. said:

    > Hello
    >
    > I am trying to get an understanding on strlen. From my understanding,
    > it takes a look at a string or single character array and returns the
    > length - the number of character in the string. When I try to apply it
    > to the following code:
    >
    > #include <stdio.h>
    > #include <string.h>
    > const int MAX_FILENAME = 256;
    > void checkFilename (char *filename, char *extension)
    > {
    > char length[]=filename[MAX_FILENAME];


    char length[MAX_FILENAME]; /* legal under C99 but not under C90 */
    strcpy(length, filename);

    > int size;
    > size = strlen(length);
    > return;


    Or simply use: size = strlen(filename); instead (thus cutting out the middle
    man).

    Note that strlen actually returns size_t, not int.

    Further note that FILENAME_MAX, which is defined in <stdio.h>, may suit your
    purposes better than rolling your own value.


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Apr 25, 2006
    #2
    1. Advertising


  3. > #include <stdio.h>
    > #include <string.h>
    > const int MAX_FILENAME = 256;
    > void checkFilename (char *filename, char *extension)
    > {
    > char length[]=filename[MAX_FILENAME];

    What are you trying to do here? Does this even compile? You're trying
    to initialize an array of charaters (a string) with one character
    (namely filename[MAX_FILENAME])...
    I think you meant something like
    char *length=filename;
    which basiclly just declares a (another) local copy of filename.

    > int size;
    > size = strlen(length);
    > return;
    > int main () {
    > char filename1 [MAX_FILENAME] = "testfile";
    > char filename2 [MAX_FILENAME] = "testfile.txt";
    > char filename3 [MAX_FILENAME] = "testfile.dat";
    > char extension [] = ".txt";
    >
    > checkFilename (filename1, extension);
    > checkFilename (filename2, extension);
    > checkFilename (filename3, extension);
    >
    > printf("1: %s\n", filename1);
    > printf("2: %s\n", filename2);
    > printf("3: %s\n", filename3);


    Where's the return statement?

    > I get " initializer fails to determine size of `length'" Could someone
    > direct me in the right direction. Please note, I'm not looking for the
    > answer, but more interested in getting an understanding of what is
    > going on.

    Basiclly what strlen does is count the elements of the character array
    until it reaches a character with a value of 0 or '/0', search for
    null-terminated strings

    Abdo Haji-Ali
    Programmer
    In|Framez
     
    Abdo Haji-Ali, Apr 25, 2006
    #3
  4. On 24 Apr 2006 16:26:33 -0700, in comp.lang.c , "Gregc."
    <> wrote:

    >Hello
    >
    >I am trying to get an understanding on strlen.


    Your problem is actually with the array declaration.

    >const int MAX_FILENAME = 256;


    Note that in C, unlike C++, this does NOT declare a constant. Instead
    it declares a variable whose value you can't change (subtle
    difference...)

    > char filename1 [MAX_FILENAME] = "testfile";


    in C, arrays must have a constant dimension. Since MAX_FILENAME is not
    a constant, you can't use it here. use

    #define MAX_FILENAME 256

    (C99, which is not widely implemented by compiler makers yet, allows
    arrays to be declared with non-constant dimensions, but not C89, which
    is the currently common version).


    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Apr 25, 2006
    #4
  5. Mark McIntyre said:

    > in C, arrays must have a constant dimension. Since MAX_FILENAME is not
    > a constant, you can't use it here. use
    >
    > #define MAX_FILENAME 256


    Just out of curiosity, what have you got against FILENAME_MAX from
    <stdio.h>?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Apr 26, 2006
    #5
  6. On Wed, 26 Apr 2006 00:38:25 +0000, in comp.lang.c , Richard
    Heathfield <> wrote:

    >Mark McIntyre said:
    >
    >> in C, arrays must have a constant dimension. Since MAX_FILENAME is not
    >> a constant, you can't use it here. use
    >>
    >> #define MAX_FILENAME 256

    >
    >Just out of curiosity, what have you got against FILENAME_MAX from
    ><stdio.h>?


    Pesonally I'm quite fond of it, I can't comment on the OP's
    preferences. :)

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Apr 27, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Imran

    strlen

    Imran, Aug 12, 2004, in forum: C++
    Replies:
    2
    Views:
    555
    Jerry Coffin
    Aug 12, 2004
  2. Matt Parkins

    strlen deprecated ?!?

    Matt Parkins, Jan 25, 2005, in forum: C++
    Replies:
    45
    Views:
    5,116
    Ivan Vecerina
    Jan 28, 2005
  3. chessc4c6
    Replies:
    9
    Views:
    507
    Old Wolf
    Mar 8, 2005
  4. Nollie

    strlen() speed test

    Nollie, May 11, 2005, in forum: C++
    Replies:
    12
    Views:
    3,377
    Peter Smithson
    May 14, 2005
  5. Tydr Schnubbis

    strlen() argument warning

    Tydr Schnubbis, Mar 7, 2004, in forum: C Programming
    Replies:
    6
    Views:
    474
    CBFalconer
    Mar 7, 2004
Loading...

Share This Page