c - 如何遍历 char ** 变量
问题描述
我正在运行一个小型 rpc 程序/使用 rpc 框架,该框架char[]
从客户端文件中获取 a 并将其发送到枚举字符串中的整数的服务器。
我有一个文件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;
解决方案
就像@user3386109 说的:
command 不是指针。所以 &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);
}
推荐阅读
- azure - 无法利用 Azure 发布管道中上一步传递的变量
- ionic-framework - https //127.0.0.1:111001 意外关闭连接
- reactjs - 图形工具包反应组件不维护状态
- google-chrome - 虽然我使用的是 vpn,但网站如何知道我的时区
- r - 根据行条件替换列值
- javascript - Javascript Fetch 没有按预期工作?
- python - 基于日期范围的电子邮件限制无法使用 Python
- java - 在 Linux 终端上使用 LWJGL 运行程序
- python-3.x - 如何在python中的二维数组/列表上应用过滤函数
- amazon-web-services - 如何将目标跟踪策略与 SQS 队列一起使用?