strlen

G

Gregc.

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
 
R

Richard Heathfield

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.
 
A

Abdo Haji-Ali

#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
 
M

Mark McIntyre

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
 
R

Richard Heathfield

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>?
 
M

Mark McIntyre

Mark McIntyre said:


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top