C 指针和以下函数

标签 c

1)

int *ptr;
ptr = malloc(10 * sizeof (*ptr)); 

关于sizeof(*ptr)*ptr引用ptr - 它是ptr指向的值?那么sizeof(*ptr)是否等于ptr指向的总内存大小?

2)

void *print_message_function( void *ptr )
{ 
  char *message;
  message = (char *) ptr;
  printf("%s \n", message);
}

问题2,void* print_message_function 期望返回类型为void* 或其他类型,但下面的函数没有返回任何内容。

最佳答案

  1. 问问自己变量 *ptr 是什么?提示,它在第一行。

  2. 这实际上不是一个问题。但如果你问这是不是错了,那就错了。但是,如果您尝试使用返回值,您可能会遇到麻烦。

有趣的是,该标准仍然允许不从明确打算返回值的函数中返回显式值。 C116.8.6.4 return 语句/1 声明:

A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.

这仅说明return 语句的特定形式可能出现在哪里。它没有规定非 void 函数中必须存在显式 return 语句。

关于C 指针和以下函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16765051/

相关文章:

c - 删除链表头后出现垃圾打印

c - 我的链接列表没有从头打印出来

c++ - 从 C/C++ 构建 html 文档

c - 新手 : what is wrong with my makefile?

c - 带 lwip 的网络服务器(Cortex M3、Stellaris LM3S6965 评估板)

c - 定义整数(int);默认值是多少?

C 中的常量变量

c - libcurl:使用 WriteMemoryCallback 时 curl_easy_perform 上的段错误

c - 如何使用指针执行冒泡排序

C,将带有 GhashTable 键的结构体传递给函数