Max
Max

Reputation: 37

c Function is not called

my written C function is not called, although I pass the two char to the function. I do not get the return value I would expect from the while loop. Anyone have an idea what the problem is?

int string_compare(char v[], char w[]);

int main (void) {
  char v[] = "N";
  char w[] = "M";
  int string_compare(char v[], char w[]);
  return 0;
}

int string_compare(char v[], char w[])
{
  int i = 0;
  while (1) {
    if (v[i] != w[i]) {
      return v[i] - w[i];
    }
    if (v[i] == '\0' || w[i] == '\0') {
      break;
    }
    ++i;
  }
  return 0;
}

Thanks!

Max

Upvotes: 0

Views: 65

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

For starters the function should be declared at least like

int string_compare( const char s1[], const char s2[] );

That is the parameters should have the qualifier const because the function shall not change passed to it strings.

The condition in this if statement

if (v[i] == '\0' || w[i] == '\0') {

can be simplified like

if ( v[i] == '\0' ) {

because when the control achieves this point v[i] and w[i] are equal each other. So it is enough to check only one character whether it is equal to zero.

The variable i used as an index

int i = 0;

shall have the type size_t because an object of the type int in general is unable to store all values of the type size_t (of string lengths) that is the return type of the function strlen or of the operator sizeof.

size_t i = 0;

However introducing this auxiliary variable is redundant.

In any case the definition of the function is too complicated.

At last this record in the function main

int string_compare(char v[], char w[]);

is a redundant function declaration. The function main does not call the function string_compare. It only declares it one more in its block scope.

Here is a demonstrative program that shows how the function can be declared, defined and called.

#include <stdio.h>

int string_compare( const char s1[], const char s2[] )
{
    while ( *s1 && *s1 == *s2 )
    {
        ++s1; ++s2;
    }
    
    return *s1 - *s2;
}

int main(void) 
{
    char *s1 = "N";
    char *s2 = "M";
  
    printf( "string_compare( \"%s\", \"%s\" ) = %d\n", s1, s2,
                             string_compare( s1, s2 ) );
  
    printf( "string_compare( \"%s\", \"%s\" ) = %d\n", s2, s1,
                             string_compare( s2, s1 ) );
                           
    s2 = s1;
    
    printf( "string_compare( \"%s\", \"%s\" ) = %d\n", s1, s2,
                             string_compare( s1, s2 ) );
  
                           
  
  return 0;
}

The program output is

string_compare( "N", "M" ) = 1
string_compare( "M", "N" ) = -1
string_compare( "N", "N" ) = 0

Upvotes: 0

John Mancini
John Mancini

Reputation: 53

There is a lot wrong here. To call the function string_compare you need to pass two character arrays to it. You then need to assign the return value to a variable.

So

int string_compare(char v[], char w[]);

int main (void) {
  char v[] = "N";
  char w[] = "M";
  int x = string_compare(v, w);
  printf("%d\n", x);
  return 0;
}

int string_compare(char v[], char w[])
{
  int i = 0;
  while (1) {
    if (v[i] != w[i]) {
      return v[i] - w[i];
    }
    if (v[i] == '\0' || w[i] == '\0') {
      break;
    }
    ++i;
  }
  return 0;
}

Upvotes: 2

ForceBru
ForceBru

Reputation: 44838

Quite simply, int string_compare(char v[], char w[]); is a function declaration, not a function call. A function call would look like int result = string_compare(v, w);.

Upvotes: 3

Related Questions