根据C中的条件更改指针(星号1.4 cdr struct)

时间:2015-03-05 18:17:36

标签: c pointers

我该如何执行以下操作? full_agentstring需要指向cdr-> channel或cdr-> dstchannel:

....
char *cdr_channel = cdr->channel;
char *cdr_dstchannel = cdr->dstchannel;
char *full_agentstring = NULL;
if(strstr(cdr_channel, agent_pattern) != NULL) {
      *full_agentstring = cdr->channel;
} else if(strstr(cdr_dstchannel, agent_pattern) != NULL) {
      *full_agentstring = cdr->dstchannel;

我也试过full_agentstring = &cdr_dstchannel但仍然收到警告:

从不兼容的指针类型[默认启用]

进行分配

1 个答案:

答案 0 :(得分:1)

if(strstr(cdr_channel, agent_pattern) != NULL) {
      full_agentstring = cdr->channel;
} else if(strstr(cdr_dstchannel, agent_pattern) != NULL) {
      full_agentstring = cdr->dstchannel;

或者,如果我不正确地理解你的问题,那么这是第二种方法

char *cdr_channel = cdr->channel;
char *cdr_dstchannel = cdr->dstchannel;
char **full_agentstring = NULL;
if(strstr(cdr_channel, agent_pattern) != NULL) {
      full_agentstring = &cdr->channel;
} else if(strstr(cdr_dstchannel, agent_pattern) != NULL) {
      full_agentstring = &cdr->dstchannel;