strlen和strlen之间有什么区别吗? [英] Is there any differences between strlen(s) and (strlen)(s)?

查看:60
本文介绍了strlen和strlen之间有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在bstrlib.c(bstring库)中,在一些地方将括号放在函数调用周围.目的是什么?

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

代码段:

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;
}

推荐答案

strlen(s)(strlen)(s)都相同.

由于 strlen()是调用时的函数,因此无需在函数名称前后加上方括号.

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.

这篇关于strlen和strlen之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆