C语言中字符串指针的返回
我有一个带有函数的程序,我应该提供字符串作为参数,并返回字符串的一部分。这样我想返回一个指向调用函数的指针C语言中字符串指针的返回,c,pointers,C,Pointers,我有一个带有函数的程序,我应该提供字符串作为参数,并返回字符串的一部分。这样我想返回一个指向调用函数的指针 int test_func(char *dest, const char *src) { int i,n,len_in_msg,j; char *rem_string; n=strlen(src); for (i = 0; i < n && src[i] != '\n'; i++) { dest = s
int test_func(char *dest, const char *src)
{
int i,n,len_in_msg,j;
char *rem_string;
n=strlen(src);
for (i = 0; i < n && src[i] != '\n'; i++)
{
dest = src+i;
}
printf("value of i = %d ",i);
dest = dest+2;
printf("dest = %s", dest);
return 1;
}
也作为&测试,但没有运气。我怎样才能做到这一点
char *test;
/* ... */
msg_length = test_func(test,msg_full);
您正在传递test
指向test\u func
的未初始化指针。如果要修改传入参数的指针对象,请使用指向指针的指针:
int test_func(char **dest, const char *src)
{
/* change the body accordingly to new dest type */
}
以及:
由于要将test
的地址传递给test\u func
,因此在test\u func
中,必须将*dest
视为指针,而不是dest
。例如,通过参数char**dest
传递&test
后,必须为dest
的值分配空间,而不是dest
本身:
*dest = malloc (sizeof **dest * (len + 1));
这就是瓦赫所说的:
我认为你的代码应该在这个变化之后工作。如果您遇到其他问题,这里有一个稍微不同的方法,您可以从中得出:
#include <stdio.h>
#include <stdlib.h>
int test_func (char **dest, char *src, char delim);
int main (void)
{
int msg_length = 0;
char *test = NULL;
char *msg_full = "100000\nis hhhhhhhh";
msg_length = test_func (&test, msg_full, '\n');
printf ("\n value of test = '%s' (%d chars)\n\n", test, msg_length);
if (test) free (test); /* free allocated memory */
return 0;
}
/* copy characters in 's' following 'delim' to
newly allocated block of memory in 'str' */
int test_func (char **dest, char *src, char delim)
{
size_t len = 0;
char *p = src;
char *new = NULL;
/* set p at start of second string, save pointer
to start of second in new */
while (*p) if (*p++ == delim) break;
new = p;
while (*p++) len++; /* length of new */
*dest = malloc (sizeof **dest * (len + 1)); /* allocate */
p = new; /* set p to new */
new = *dest; /* set new to dest */
while (*p) { *new = *p++; new++; } /* copy to *dest */
return (int)len;
}
最后一点注意:(可能是2),请密切注意您的数据类型。(这很重要)。我在您的代码中将函数返回类型保留为int
以及msg\u length
,但它们实际上应该是unsigned int
(或者更恰当地说是size\u t
)。为什么?长度不能为负。养成将你的类型与你正在处理的信息相匹配的习惯。这将变得越来越重要,你在C的进一步进展
main
是一个特殊函数,具有必需的声明。它的类型是'int'
,这意味着它必须返回一个值(即使MS let’s you’s逃之夭夭)。应该使用所需的参数(int argc,char**argv)
来声明它,即使对于简短的代码片段,我并不总是这样做(上面的代码证明了这一点)。如果您不打算给出完整的声明,至少要明确地告诉编译器您在做什么(例如intmain(void)
)。虽然技术上不正确,但它比简单地提供空参数要干净得多。同样在test_func内部,更改dest=src+i;to*dest=src+i;我已经尝试过了,但它在调用函数中返回null
*dest = malloc (sizeof **dest * (len + 1));
/* change the body accordingly to new dest type */
#include <stdio.h>
#include <stdlib.h>
int test_func (char **dest, char *src, char delim);
int main (void)
{
int msg_length = 0;
char *test = NULL;
char *msg_full = "100000\nis hhhhhhhh";
msg_length = test_func (&test, msg_full, '\n');
printf ("\n value of test = '%s' (%d chars)\n\n", test, msg_length);
if (test) free (test); /* free allocated memory */
return 0;
}
/* copy characters in 's' following 'delim' to
newly allocated block of memory in 'str' */
int test_func (char **dest, char *src, char delim)
{
size_t len = 0;
char *p = src;
char *new = NULL;
/* set p at start of second string, save pointer
to start of second in new */
while (*p) if (*p++ == delim) break;
new = p;
while (*p++) len++; /* length of new */
*dest = malloc (sizeof **dest * (len + 1)); /* allocate */
p = new; /* set p to new */
new = *dest; /* set new to dest */
while (*p) { *new = *p++; new++; } /* copy to *dest */
return (int)len;
}
$ ./bin/splitintwo
value of test = 'is hhhhhhhh' (12 chars)