【发布时间】: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”返回类型。
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。