我正在运行一个小型rpc程序,使用的是rpc框架,该框架从客户端文件中提取char[]
并将其发送到服务器,该服务器枚举字符串中的整数。
我有一个文件client.c
,该文件接受用户输入并将其传递到头文件中的外部函数。
#include <rpc/rpc.h>
#include "getNumInt.h"
int main(int argc, char **argv){
CLIENT *cli;
char *server;
server = argv[1];
cli = clnt_create(server, GETNUMINT, GNUMINT, "tcp"); //creates a client handle
/*does some check for whether the client connected*/
char command[256];
int *numInt;
fgets(command, 256, stdin);
numInt = enumints_1(&command, cli); //segfaults here according to backtrace
return(0);
}
函数enumints_1
在我的服务器存根server.c
中定义为:
int *enumints_1(msg, req)
char **msg; struct svc_req *req;{
printf(*msg);
static int numDigits = 0;
char msgcopy[256];
strcpy(msgcopy, *msg);
int i = 0;
for(i; i<strlen(msgcopy); i++){
if(msgcopy[i] >= '0' && msgcopy[i] <='9'){
numDigits++;
}
}
return(&numDigits);
}
我的主要问题是如何遍历char **msg
,因为这很可能导致我的程序出现段错误。
command
只是一个来自用户输入的字符串,然后通过引用传递给enumints_1
函数。
因为它是指向某个点的指针,所以我假设我可以strcpy
或memcpy
将该字符串复制到char数组,但这是行不通的。
也是我的.x
文件:
struct intStringPair{
int numInts;
char msg[256];
};
program GETNUMINT{
version GNUMINT{
int ENUMINTS(string) = 1; //string considered char * the rpc generated file makes it so enumints_1 then has to take char **
int WRITEMESSAGE(intStringPair) = 2;
} = 1;
}= 0x20000001;
答案 0 :(得分:3)
就像@ user3386109所说:
命令不是指针。因此,&command不是指向指针的指针
因此将&command分配给msg无效(而且我的编译器甚至都没有编译它)
当我编译这段代码时:
// my set up code
#include <stdio.h>
#include <string.h>
struct svc_req {};
typedef struct svc_req CLIENT;
struct svc_req *clnt_create(const char *, int, int, const char*) {return 0;}
int GETNUMINT=0, GNUMINT=0;
// your code verbatim
int *enumints_1(char **msg, struct svc_req *req){
printf(*msg);
static int numDigits = 0;
char msgcopy[256];
strcpy(msgcopy, *msg);
int i = 0;
for(i; i<strlen(msgcopy); i++){
if(msgcopy[i] >= '0' && msgcopy[i] <='9'){
numDigits++;
}
}
return(&numDigits);
}
int main(int argc, char **argv){
CLIENT *cli;
char *server;
server = argv[1];
cli = clnt_create(server, GETNUMINT, GNUMINT, "tcp"); //creates a client handle
/*does some check for whether the client connected*/
char command[256];
int *numInt;
fgets(command, 256, stdin);
numInt = enumints_1(&command, cli); //segfaults here according to backtrace
return(0);
}
编译器说:
<source>: In function 'int main(int, char**)':
<source>:34:25: error: cannot convert 'char (*)[256]' to 'char**'
numInt = enumints_1(&command, cli); //segfaults here according to backtrace
^~~~~~~~
<source>:10:24: note: initializing argument 1 of 'int* enumints_1(char**, svc_req*)'
int *enumints_1(char **msg, struct svc_req *req){
~~~~~~~^~~
Compiler returned: 1
相反,您可以做一个指向数组的指针,然后传递该数组的地址:
// my set up code
#include <stdio.h>
#include <string.h>
struct svc_req {};
typedef struct svc_req CLIENT;
struct svc_req *clnt_create(const char *, int, int, const char*) {return 0;}
int GETNUMINT=0, GNUMINT=0;
// your code verbatim
int *enumints_1(char **msg, struct svc_req *req){
printf(*msg);
static int numDigits = 0;
char msgcopy[256];
strcpy(msgcopy, *msg);
int i = 0;
for(i; i<strlen(msgcopy); i++){
if(msgcopy[i] >= '0' && msgcopy[i] <='9'){
numDigits++;
}
}
return(&numDigits);
}
int main(int argc, char **argv){
CLIENT *cli;
char *server;
server = argv[1];
cli = clnt_create(server, GETNUMINT, GNUMINT, "tcp"); //creates a client handle
/*does some check for whether the client connected*/
char command[256], *command_pointer=command;
int *numInt;
fgets(command, 256, stdin);
numInt = enumints_1(&command_pointer, cli); //segfaults here according to backtrace
return(0);
}