2

I got a task from my college which is to find the length of a string using pointers in a function.Here is my code:

#include <stdio.h>
#include <stdlib.h>

#define max 100

int lengthchar(char *array1[])
{
    int a, x = 0;
    for (a = 0; *(array1++) != '\0'; a++)
    {
        x++;
    }
    return x;
}

int main()
{
    char arr1[max];
    int length;

    printf("Enter string\n");
    gets(arr1);

    length = lengthchar(arr1);

    printf("Length=%d", length);

    return 0;
}

But when I give input something bellow:

Enter string: av

Length=9

It shows the wrong length, what's my mistake?

7
  • 2
    When passing an array to a function the argument should be char *array1 or char array1[]. BTW, the variable a isn't doing anything useful and should be removed. Commented Dec 16, 2019 at 18:02
  • FYI: Why is the gets function so dangerous that it should not be used? Commented Dec 16, 2019 at 18:06
  • Listen to your compiler warnings. It will tell you you are using the wrong type when calling the function lengthchar1() Commented Dec 16, 2019 at 18:06
  • 2
    No, you can leave it empty like for(;*(array1++)!='\0';) Commented Dec 16, 2019 at 18:09
  • 3
    Or use a while loop: while (array[x]) x++; return x; Commented Dec 16, 2019 at 18:10

3 Answers 3

4

This is because of the way to pass a string pointer as an argument to the function.

Just change char *array1[] to char *array1 or array1[].

Have a look at the implementation below:

int lengthchar(char array1[])
{
    int a,x=0;
    for(a=0;*(array1++)!='\0';a++)
    {
        x++;
    }

   return x;
}

PS: variable a can be removed by using a while loop.

Sign up to request clarification or add additional context in comments.

4 Comments

Or... x can be removed and a returned.
@FiddlingBits, Correct!
Thanks, it helped
DV-ted for not showing OPs const correctness & correct types
1

In your function signature, you are telling the compiler that lenghtchar() expects a pointer to character strings, or **char in other words.

What you really want to do is to change your function from int lengthchar(char *array1[]) to int lengthchar(char array1[]) or int lengthchar(char *array1). This is a bit tricky since in C, you can address an array by using the address of its first element (aka, by using pointer to its first item).

Expert C Programming has a dedicated chapter on this topic.

Now, coming to your lengthchar() function, I would do some refactoring to eliminate the variable a and use a while loop instead. I have also included another alternative implementation that relies on pointer arithmetic (more fun to me :) )

Note also that I used fgets() instead of gets() which is considered deprecated since it does not do any bounds checking.

#include <stdio.h>
#include <stdlib.h>

#define max 100

/* 
 * returns the lenght of the string excluding the terminating 
 * NULL character
 */
int lengthchar(char *array1) {
    int x = 0;
    while (*array1++)
        x++;
    return x-1;
}

int lengthchar1(char *array1){
  char *p;
  for (p = array1; *p; p++)
  ;
  return p - array1 - 1; /* -1 for \0 */
}

int main() {
    char arr1[max];
    int length;

    printf("Enter string\n");
    fgets(arr1, max, stdin);

    length = lengthchar(arr1);

    printf("Length=%d", length);

    return 0;
}

Comments

0

On declaring or defining a function that shall take an array of type T as an argument, use the notation: T *array or T array[]; T may stand for any valid C data type such as char, int, float, double, etc...

As for your loop, the variable a seems to be redundant because it has no effect on any part of the program. The return value of the function lengthchar() is one more than the number of characters inputted.

Also you should avoid the function gets() because it is deemed dangerous and has been removed from the C standard; use fgets() instead.

Your code should look something like this:

#include <stdio.h>
#include <string.h>

#define max 100

int lengthchar(char *str)
{
    int len = 0;
    while (*str++)
    {
        len++;
    }
    return len;
}

int main()
{
    char str1[max];

    printf("Enter string:");

    fgets(str1, max, stdin);

    str1[strcspn(str1, "\n")] = 0; // remove new-line character

    int length = lengthchar(str1);

    printf("Length = %d\n", length);
    return 0;
}

4 Comments

@P__J__ There is no need to overwhelm the OP with new ideas. It is sufficient to point out the error and present the solution.
@P__J__ Not using const is not "not programming correctly" and int type is fine instead of size_t in toy programs.
@P__J__, I completely agree with machine_1. Also, the way to write comments is offensive.
@DeepakTatyajiAhire - I do not think that saying the truth is offensive. Const correctness and wrong types are one of the most common programming problems.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.