如何在遍历字符串指针后从 void 函数中取回指针
How to get a pointer back from a void function after it iterates through a string pointer
我正在尝试编写一个代码,使用一个指针来解析给定的字符串。
我写的原始代码工作得很好,但它是……多余的,所以我试着把它变成一个函数调用,以使其更简洁。这是我拥有的:
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);
}
出于某种原因,它不会将指针数组返回到它从上一个函数调用中断的位置。这是为什么?它的行为就好像在调用它之后指针从头开始。
只需将您的 repeat
更改为:
void repeat(char **to) {
while (**to != '=') {
(*to)++;
}
}
并这样称呼它:
repeat(&tok);
并始终检查错误:
if (scanf("%299s", inputArray) != 1){
printf("incorrect input\n");
return 1;
}
和您的示例代码(并在 array
和 repeat
中添加错误检查以防止越界):
#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);
}
函数收到其参数的副本。原始参数不受影响。
回馈在 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
的指针,但为了清楚起见,建议仅在需要 return 时才使用此样式一个函数不止一件事。
我正在尝试编写一个代码,使用一个指针来解析给定的字符串。
我写的原始代码工作得很好,但它是……多余的,所以我试着把它变成一个函数调用,以使其更简洁。这是我拥有的:
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);
}
出于某种原因,它不会将指针数组返回到它从上一个函数调用中断的位置。这是为什么?它的行为就好像在调用它之后指针从头开始。
只需将您的 repeat
更改为:
void repeat(char **to) {
while (**to != '=') {
(*to)++;
}
}
并这样称呼它:
repeat(&tok);
并始终检查错误:
if (scanf("%299s", inputArray) != 1){
printf("incorrect input\n");
return 1;
}
和您的示例代码(并在 array
和 repeat
中添加错误检查以防止越界):
#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);
}
函数收到其参数的副本。原始参数不受影响。
回馈在 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
的指针,但为了清楚起见,建议仅在需要 return 时才使用此样式一个函数不止一件事。