Home > Mobile >  Understanding parameters of strlen in C
Understanding parameters of strlen in C

Time:02-02

I am a bit confused on why my code is not iterating a string in C programming.

Essentially.

I have this function here

int atoi(const char *s[]){
    printf(" The length is  %d",strlen(s));

    int length = strlen(s);

    for(int i = 0; i< length;   i){
        printf("This is %s",*(s i));
    }
    return 0;
}

My first question

  1. When doing strlen(s) why does it work, but when doing strlen(*s) it does not work, isn't strlen supposed to be taking in the value not the pointer so the latter should work?

  2. How would I go about looping through the pointer that points to the string in memory?

Cheers

CodePudding user response:

  1. Do not use standard function names
  2. strlen takes a pointer to char and returns size_t (not int)
  3. Your print format is wrong.
  4. Your function takes an array of pointers not a pointer to char
int myfunc(const char *s)
{
    size_t length = strlen(s);

    printf(" The length is  %zu", length);
    for(size_t i = 0; i< length;   i){
        printf("This is '%c'\n",*(s i));
    }
    return 0;
}

CodePudding user response:

Question:

when doing strlen(s) why does it work, but when doing strlen(*s) it does not work?

Answer:

strlen() expects an argument of type const char *, not a const char **. You passed an array of pointers to char to the function, which decayed to a char **. From the man page:

The strlen() function calculates the length of the string pointed to by s, excluding the terminating null byte ('\0').

Question:

How would I go about looping through the pointer that points to the string in memory?

printf("This is %s",*(s i));

Answer:

Assuming you wanted to pass a string (an array of null-terminated bytes) to the function and print it char by char, change the function declaration to take a const char *, and use the correct format specifier in the call to printf() (The %s format specifier expects a pointer to char, not a char itself). Change the call to:

printf ("%c", *(s   i));

Note that *(s i) is equivalent to s[i], which is more readable. So the above call to printf() can be rewritten as:

printf ("%c", s[i]);

Aside: strlen() returns size_t, not an int. Do not mismatch numeric types.

CodePudding user response:

For starters you should not name your user-defined functions the same ways as standard C functions.

There is already standard C function with the name atoi declared in header <stdlib.h>. Using the same name can result in compiler or linker errors.

Your function atoi declared like

int atoi(const char *s[]){

that is the same as

int atoi(const char **s){

due to adjusting by the compiler a parameter having an array type to pointer to the array element type accepts a "double" pointer. So using the function strlen with the double pointer invokes undefined behavior

printf(" The length is  %d",strlen(s));

The function strlen expects an argument of the type char * or const char * but not an argument of the type const char **.

Moreover the return value of the function strlen has the type size_t. And using the conversion specifier %d in a call of printf with a corresponding argument expression of the type size_t again invokes undefined behavior. You have to use the conversion specifier %zu instead of %d.

This for loop

for(int i = 0; i< length;   i){
    printf("This is %s",*(s i));
}

does not make sense because the variable length actually does not contain the number of elements in the passed array.

You need either explicitly to pass the number of elements in the array or the array should have a sentinel value.

And the return type of the function int also does not make sense because the returned value 0 reports nothing useful to the caller of the function.

If the function is designed only to traverse a string in a for loop then it should be declared like for example

char * func( const char *s )
{
    for ( const char *p = s; *p != '\0';   p )
    {
        putchar( *p );
    }

    return ( char * )s;
}

If you want to pass to the function an array of strings then the function can be implemented for example the following way as shown in the demonstration program below provided that the array contains a sentinel value

#include <stdio.h>

void func( const char **s )
{
    for ( ; *s != NULL;   s )
    {
        printf( "%s ", *s );
    }
}

int main( void )
{
    const char * s[] = { "Hello", "World!", NULL );        

    func( s );
    putchar( '\n' );
}

The program output is

Hello World!
  • Related