2

In bstrlib.c (bstring library), there are a few places where it puts parenthesis around function calls. What is the purpose?

Code snippet:

bstring bfromcstr (const char * str) {
bstring b;
int i;
size_t j;

  if (str == NULL) return NULL;
  j = (strlen) (str);
  i = snapUpSize ((int) (j + (2 - (j != 0))));
  if (i <= (int) j) return NULL;

  b = (bstring) bstr__alloc (sizeof (struct tagbstring));
  if (NULL == b) return NULL;
  b->slen = (int) j;
  if (NULL == (b->data = (unsigned char *) bstr__alloc (b->mlen = i))) {
    bstr__free (b);
    return NULL;
  }

  bstr__memcpy (b->data, str, j+1);
  return b;
}

I tried something myself:

int main(){
  char *s = {"hello"};
  int length = strlen(s);  
  //int length = (strlen)(s);   // this produce the same output as the above line
  printf("length = %d\n", length);
  return 0;
}
drdot
  • 3,215
  • 9
  • 46
  • 81

1 Answers1

1

strlen(s) and (strlen)(s) both are same.

Since strlen() is a function while calling it there is no need to put brackets around the function name.

There is something called function pointers where brackets are needed while declaring and calling them.

When there is a macro with the same name then in order to differentiate between them you can have brackets around.

Gopi
  • 19,784
  • 4
  • 24
  • 36