【问题标题】:C program forcing me to have return type but I don't need it.!C 程序强迫我有返回类型,但我不需要它。!
【发布时间】:2019-05-12 09:44:38
【问题描述】:

我有一个 C 语言的 TCP/IP 应用程序。我有 1 个用于处理 TCP/IP 相关事物的头文件和 1 个用于调用所有函数的主文件。由于问题发生在客户端,我只发布我的客户端代码。

这里是program.c

#include "tcpHeader.h"
#include <pthread.h>
#include <stdio.h>

int main(int argc, char **argv){

    // all code regarding connecting to server on TCP on another thread.
    // Join here after completing the thread execution
    // It works completely fine upto the next line.
    testFunctionFromMyHeader();

    // after executing below line, program do not continue with 'void' return type of the function. 
    // But it works if I change return type to 'int'
    waitForAMessageFromServerAndSendConfirmation(12345678901LL);

    // program expects me to return an integer on last function call 
    // & all other calls from now.
    // even if I don't need integer return type.
    testFunctionFromMyHeader();

    return 0;
}

这里是 tcpHeader.h

#ifndef CLIENT_TCP
#define CLIENT_TCP

// all required headers.
// all global variables

void testFunctionFromMyHeader(){
    printf("Test Function");
}

void waitForAMessageFromServerAndSendConfirmation(unsigned long long args){

    // wait until receiving data from server in recv() function.
    // received 'args' is used in processing the data.
    // process the data received
    // send the confirmation message to server in send() function.
    // Server also received this confirmation without any problem on server side.

    // all code in this function also worked properly.
    // Even the next printf line is also executed.
    printf("All operation completed.");

    // with 'void' return type, next line does not make any difference.
    // But if I return an 'int' then it works.
    return NULL;
}

#endif

它在 program.c 中的 waitForAMessageFromServerAndSendConfirmation(unsigned long long) 处停止

但如果我在 program.c 中这样做,那么它就可以工作。(来自 tcpHeader.h 的函数的返回类型也更改为适当的类型)

int confirmation = waitForAMessageFromServerAndSendConfirmation(12345678901LL);
if( confirmation == 0 ){ // 0 or any int value
    int testFunctionReturnedValue = testFunctionFromMyHeader();
        .
        .
        .
        // keep going like this with int return type FOR ALL FUNCTIONS
}

帮助我找出问题所在。我不想到处使用 int 返回类型。

- 为什么它强制我在头文件中的 waitForAMessageFromServerAndSendConfirmation(unsigned long long) 函数上具有“int”返回类型

- 为什么它强迫我在所有调用的函数上都使用“int”返回类型。

【问题讨论】:

标签: c tcp pthreads posix


【解决方案1】:

当函数被声明为void 时,意味着该函数不会返回任何内容。

如果您在此类函数中使用带有值的 return 语句,例如return 1;,你试图让它返回一些东西。但它被定义为返回一些东西。

将函数声明为void 有两个目的:

  1. 告诉函数的用户不要期望返回值。

  2. 告诉函数的作者不要返回任何东西。如果作者想要传达一些东西,他应该使用其他方式来做到这一点(例如,有一个指向调用者变量的指针参数,用于放置结果值,或者调整函数签名以返回一些东西除了void)。

例如:

void myFunc1(int*result) {
    *result= 1;  // put value 1 in callers variable
    // there is no return statement that returns a value
}
void myFunc2(int*result) {
    *result= 1;  // put value 1 in callers variable
     return;     // leave the function. There is no value to return.
}

或:

int myFunc(int in) {
    return 2*in;  // leave the function and return a value.
}


回到你的问题:
int confirmation = waitForAMessageFromServerAndSendConfirmation(12345678901LL);

上面说期望函数返回一些东西并将其分配给变量conformation。然而,

void waitForAMessageFromServerAndSendConfirmation(unsigned long long args) {
    //....
    return NULL;

说函数将返回任何东西,但你试着让它返回一些东西。

由于这些矛盾,您的编译器会抱怨。

【讨论】:

  • 正如我在我的 cmets 中所写的那样,无论我写 returnreturn NULL 还是 do,该行实际上都没有任何区别什么都不写.
  • 当您想从函数的末尾以外的地方返回时,可以在 void 函数中使用 return 语句。
  • @BhavinPanara,我看到你的 .h 文件中有 code。 .h 文件应仅包含定义(原型),然后具有包含函数代码的文件 tcp.c。此外,请遵循我上面的建议:要么定义返回值的函数,要么不定义返回值,但不能同时定义两者。
  • @BhavinPanara,还要确保打开编译器的警告。如果您正确退货/不退货,它会给您反馈。
  • @BhavinPanara,那么您没有启用警告。它应该抱怨你在做什么,如果警告被打开它抱怨。检查编译器的文档。
猜你喜欢
  • 1970-01-01
  • 2022-09-27
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 2017-08-05
  • 2011-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多