views:

78

answers:

5
int isEmpty(char x [1000]){
    int i = 0;

    while( x[i] == " " || x[i] == "/t" || x[i] == ""){
        i++;
    }

    if (i != 999)
        return 1;
    }

    return 0;
}

The errors I recieve:

warning: comparison between pointer and integer

warning: comparison with string literal results in unspecified behavior

I realise my code reads far too much like Java, I haven't fully grapled with the C syntax yet.

+2  A: 

Since you are comparing with char constants, you need to use single quotes and not double quotes as:

x[i] == ' '

Double quotes are used for string constants like "foo". So "\n" is a string literal with a single char and '\n' is a char constant.

codaddict
Many thanks codaddict :)
Kay
And if you were to be comparing strings, the == would be comparing the address that the string starts at, not the contents of the string, which is almost certainly not what you intended.
JamesMLV
A: 
  1. Characters should be enclosed in ' '(single quotes).
  2. It's \t & not /t for the tab character.
  3. Remove the case with a blank string "" (it's equivalent to a NULL).
Kedar Soparkar
+5  A: 

The problem is you're expressing char values by using string literals. Instead of surrounding them with double quotes " use single quotes '

while( x[i] == ' ' || x[i] == '\t' ) {

Couple of other issues.

  • You probably mean \t and not /t. The former is a tab character while the latter is two characters
  • Not sure what you're trying to get at with "". Perhaps \0
JaredPar
caf
A: 

In addition to the previous answers, I think

  1. Should the checking continue if a NULL (\0) is found in x? I think that answer might be: NO.
  2. It is better to separate the detection logic of empty character from the logic of empty string
  3. The (i != 999) part is unclear. Won't it be (i == 1000)?
  4. There are unmatched parentheses in the OP's code.

Here is my version:

/* return 1 when c is considered empty, 0 otherwise */
int isEmptyChar( const char c ) {
    return c == ' ' || c == '\t';
}

enum { N = 1000 };

/* return 1 when x is considered empty, 0 otherwise */
int isEmptyString( const char x[ N ] ) {
    for( int i = 0; i < N; i++ ) {
        if( x[ i ] == '\0' ) {
            break;      /* stop, if NUL termination is found */
        }
        if( ! isEmptyChar( x[ i ] ) ) {
            return 0;   /* early return if non-empty char is found */
        }
    }
    return 1;
}
ArunSaha
A: 
int isEmpty(const char *x)
{
  return EOF==sscanf(x,"%*s");
}

should also do what you want, is C89 and test whitespaces for you.