-1

The exercise I'm doing asked to create an array of 50 characters, to put it in a structure and then use a subroutine to print the array on the screen and to say how many characters it has ( there's more but the problem that I have is here )

this is what I did

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

    int Anzahl (char []);

    int main ()

    {
    int x;
    char kette [50];
    printf ("Type down something\n");
    fgets (kette,50,stdin);

    struct hullo {
        char kette [50];
    };
    x=Anzahl (&kette[50]);
    printf ("the number of letters is : %d", x);
    }


    int Anzahl (char kette[50])
    {
    int x1;
    puts (&kette[50]);
    x1 = sizeof (kette[50]);
    return x1;
    }

`

but each time I type something, the number of characters is always 1 in the end. would be nice if someone could explain to me what I did wrong.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

3 Answers3

2

You are wrong, on two fronts.

Firstly, the function call

 x=Anzahl (&kette[50]);

is wrong (not syntactically, from the actual usage point of view), you need to pass the starring address, just like x=Anzahl (kette);

Secondly, arrays, when passed as function arguments, delays to pointer to the first elements. So, you cannot use sizeof to get the expected size from the parameter that received the input argumet.

Quoting C11, chapter §6.3.2.1

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

and, chapter §6.7.6.3

A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. [...]

Bonus:

  puts (&kette[50]);

is also wrong. It's off-by-one, as C arrays are 0-based indexed. In this case, you're passing an invalid pointer (and accessing the memory location therefor to read the content), you have undefined behavior.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    The first thing is not a type error. &kette[50] is a char*, and some compilers decay array types to pointers directly. Then indexing and the pointer pointing after the array will be a problem to the Runtime. – NoImaginationGuy Dec 14 '17 at 12:57
0

You can't call sizeof array on a function argument, since in C arrays decay to pointer. This means as soon as you call Anzahl(&kette); the "50" will just be discarded, and your function will look like int Anzahl(char *);. This is just one of your problems though, since you call sizeof on kette[50]. This will return the size of a char in bytes, which should always be 1. Instead you should be calling sizeof(kette), but this will not work because of the reasons I mentioned earlier. This could be fixed by wrapping the array in a struct like typedef struct arr { char[50]; } arr; and then using this struct instead of kette[50].

You should also be calling Anzahl(kette) instead of &kette[50], same goes with puts.

Arrrow
  • 542
  • 5
  • 21
0

Sourav Ghosh is right! The final version should be:

#include <stdio.h>
#include <string.h>
int Anzahl(char kette[50]);
int main (){
    int x;
    char kette[50];
    printf ("Type down something\n");
    fgets (kette,50,stdin);
    x=Anzahl(kette);
    printf ("the number of letters is : %d", x);
}


int Anzahl (char kette[50]){
    int x1;
    puts(kette);
    //this will print 8
    x1 = sizeof(kette);
    //if you need the string length you should use
    //but this will include the \n terminator...
    return strlen(kette);
}
gwerners
  • 66
  • 1
  • 6