jiux1
jiux1

Reputation: 3

C Code: Pass string from one function to another function

The main problem is: as soon as I send a string from one function to another, this second function doesn't really get the string as a parameter.

In detailled: I have a function void myfunc() contains a word. This word should be send to another function, so it can count the length of it. That's what I've written so far:

void myfunc(int (*countlength)(char ch)){

    char word[10] = "Hello\n";

    int result = countlength(&word);

    printf("Length of word: %d\n", result);
}

The word is being send to this function countlength(char* word):

int countlength(char* word) {
    int length = strlen(word);
    return length;
}

However the function countlength() can't count it's length and I don't know why...

The thing is, it works when the word is in the main function. Does anybody know why my Code doesn't work?

Upvotes: 0

Views: 1246

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

This parameter declaration of a function pointer

int (*countlength)(char ch)

does not correspond to the function declaration used as an argument for this parameter

int countlength(char* word)

So you need to declare the parameter like

int (*countlength)(char *ch)

In fact the identifier ch is redundant. You could just write

int (*countlength)(char *)

That is the declaration of the function myfunc will look like

void myfunc(int (*countlength)(char *));

You declared a character array within the function like

char word[10] = "Hello\n";

So the expression used as an argument in this call

countlength(&word)

has the type char ( * )[10] instead of the expected type char *.

There is no need to use the address of operator. The array designator used as an argument in this call

countlength( word )

is implicitly converted to a pointer to the first element of the array and has the type char *.

This function

int countlength(char* word) {
    int length = strlen(word);
    return length;
}

does not change its argument. So it should be declared at least like

int countlength( const char* word) {
    int length = strlen(word);
    return length;
}

The used standard C string function strlen has the return type size_t. In general an object of the type int can be not enough large to store possible lengths of strings.

So the function should be declared like

size_t countlength( const char* word) {
    return strlen(word);
}

Thus returning to the function myfunc it should look like

void myfunc( size_t ( *countlength )( const char * ) )
{
    char word[10] = "Hello\n";

    size_t result = countlength( word );

    printf( "Length of word: %zu\n", result );
}

Upvotes: 1

Well; if you use the code like this, it's working just fine When you declare an array, its name has a type of a pointer so here word has a type of char* and it's the pointer of the array's first element

#include <stdio.h>
#include <string.h>
int countlength(char* word) {
    int length = strlen(word);
    return length;
}
void myfunc(){
    char word[10] = "Hello\n";
    int result = countlength(word);
    printf("Length of word: %d\n", result);
}
main(){
    myfunc();
}

Length of word: 6

Upvotes: 0

Captain Trojan
Captain Trojan

Reputation: 2920

Two mistakes:

void myfunc(int (*countlength)(char ch)){

should be

void myfunc(int (*countlength)(char* ch)){

instead, as the function accepts char pointers.

Secondly,

int result = countlength(&word);

should be

int result = countlength(word);

as word is already a char*.

Upvotes: 1

dbush
dbush

Reputation: 225737

What you're passing to the function doesn't match what it's expecting.

&word has type char (*)[10], i.e. a pointer to an array of size 10. The function expects a char *, so just pass word. Arrays are converted to a pointer to their first element when passed to a function, so the types will match.

Upvotes: 1

Related Questions