如何在迭代字符串指针

时间:2016-07-10 03:58:51

标签: c function parsing pointers pass-by-reference

我正在尝试使用指针解析它来编写一个遍历给定字符串的代码。

我写的原始代码工作正常,但它是多余的,所以我尝试将它变成函数调用,使其更简洁。这就是我所拥有的:

char inputArray[300];
char buffer[300];
char username[100];
char password[100];
char name[100];
int i=0;

void repeat(char *to)
{
   while(*to!='=')
   {
   to++;
   }
}

void array(char *mm,char *tt)
{
   i=0;
   while(*tt!='+')
   {
   mm[i]=*tt;
   tt++;
   i++;
   }
}

int main()
{
 printf("give me the shit in this fashion: username=?+password=?+real=?\n");
 scanf("%s",inputArray);
 strcpy(buffer,inputArray);
 char *tok=buffer;

 repeat(tok);
 tok++;

 array(username,tok);
 repeat(tok);

 tok++;

 array(password,tok);

 tok++;

 repeat(tok);
 tok++;

 array(name,tok);
 }

出于某种原因,它不会让我返回指针数组tok,它从前一个函数调用中退出。这是为什么?它就好像在调用它之后指针从头开始。

2 个答案:

答案 0 :(得分:1)

函数接收其参数的副本。原始论点不受影响。

回馈一些东西在C语言中有一个特殊的语法:return语句。因此

char* repeat (char *to) // <- this function gives back a char*
{
   while (*to != '=')
   {
     to++;
   }
   return to; // <- giving something back
}

这样称呼:

tok = repeat(tok);

以同样的方式对待array

注1,如果字符串不包含'=',则此函数将导致*未定义的行为。

注2,也可以将指针传递给tok,如同另一个答案所示,但为了清楚起见,只建议在需要从一个以上的东西返回时使用此样式。功能

答案 1 :(得分:0)

只需将您的repeat更改为:

void repeat(char **to) {
    while (**to != '=') {
        (*to)++;
    }
}

并将其称为:

repeat(&tok);

并始终检查错误:

if (scanf("%299s", inputArray) != 1){
    printf("incorrect input\n");
    return 1;
}

和您的示例代码(并添加arrayrepeat中的错误检查以避免超出范围):

#include <inttypes.h> 
#include <stdio.h>
#include <stdint.h>

char inputArray[300];
char buffer[300];
char username[300];
char password[300];
char name[300];
int i = 0;

void repeat(char **to) {
    while (**to != '=') {
        (*to)++;
    }
}
void array(char *mm, char *tt){
    i = 0;
    while (*tt != '+')  {
        mm[i] = *tt;
        tt++;
        i++;
    }
}
int main() {
    printf("give me the shit in this fashion: username=?+password=?+real=?\n");
    if (scanf("%299s", inputArray) != 1){
        printf("incorrect input\n");
        return 1;
    }
    inputArray[299] = 0;
    strcpy(buffer, inputArray);
    char *tok = buffer;

    repeat(&tok);
    tok++;

    array(username, tok);
    repeat(&tok);

    tok++;

    array(password, tok);

    tok++;

    repeat(&tok);
    tok++;

    array(name, tok);
}

你可以使用它来超出界限:

#include <inttypes.h> 
#include <stdio.h>
#include <stdint.h>


char* read_str(char *src, char *dst){
    char *p, *q;
    p = src;
    while (*p != 0 && *p != '=') p++;
    if (*p == 0) {
        *dst = 0;
        return NULL; // '=' not found
    }
    p++;

    q = p;
    while (*q != 0 && *q != '+') q++;
    //if (*q == 0)  return NULL;// '+' not found

    while (p <= q) *dst++ = *p++;
    dst--;
    *dst = 0;
    q++;
    return q;
}
#define MAX_LEN 100
int main() {
    char username[MAX_LEN];
    char password[MAX_LEN];
    char name[MAX_LEN];
    char  inputArray[MAX_LEN] = "username=Alex+password=123+real=Alex";
    char *p = inputArray;

    p = read_str(p, username);
    if (p == NULL)return 1; // error
    p = read_str(p, password);
    if (p == NULL)return 1; // error
    read_str(p, name);

    printf("username: %s \n", username);
    printf("password: %s \n", password);
    printf("    name: %s \n", name);
}