动态C字符串问题 [英] Dynamic C String Question

查看:111
本文介绍了动态C字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C编写程序,因此必须使用C字符串。我遇到的问题是我不知道如何为该字符串范围之外的C字符串重新分配空间

。例如:

int main(无效)

{

char * string1;

string1 = malloc(6 );

sprintf(string1," Hello");

foo(string1);


}


void foo(char * string)

{

string = realloc(string,12);

strcat (字符串,世界);

}

在这个例子中,我为

中另一个函数中声明的字符串重新分配空间函数foo。但是,在从函数foo返回main

之后,C字符串将只包含Hello。来自函数foo的

变化已经消失。我怀疑这是

,因为重新分配已超出范围 - 我不知道

如何在函数返回后保持更改。

任何帮助都将不胜感激。


--Sachin

I''m writing a program in C, and thus have to use C strings. The
problem that I am having is I don''t know how to reallocate the space
for a C string outside the scope of that string. For example:
int main(void)
{
char *string1;
string1 = malloc(6);
sprintf(string1, "Hello");
foo(string1);

}

void foo(char *string)
{
string = realloc(string, 12);
strcat(string, " World");
}
In this example, I reallocate the space for a string declacred in
another function in the function foo. But, after it returns to main
from the function foo, the C string will only contain "Hello". The
changes from the function foo have disappered. I suspect this is
because the reallocation has gone out of scope - and I have no idea how
to keep the changes after the function returns.
Any help would greatly be appreciated.

--Sachin

推荐答案

intercom5写道:
intercom5 wrote:
我正在用C编写程序,因此必须使用C字符串。我遇到的问题是我不知道如何为该字符串范围之外的C字符串重新分配空间。例如:

int main(void)
{* char * string1;
string1 = malloc(6);
sprintf(string1,"你好);
foo(string1);

}

void foo(char * string)
{
string = realloc( string,12);
strcat(字符串,World);
}

在这个例子中,我为
另一个声明的字符串重新分配空间函数foo中的函数。但是,在从函数foo返回到main
之后,C字符串将只包含Hello。函数foo的变化已经消失了。我怀疑这是
因为重新分配已超出范围 - 我不知道如何在函数返回后保持更改。
I''m writing a program in C, and thus have to use C strings. The
problem that I am having is I don''t know how to reallocate the space
for a C string outside the scope of that string. For example:
int main(void)
{
char *string1;
string1 = malloc(6);
sprintf(string1, "Hello");
foo(string1);

}

void foo(char *string)
{
string = realloc(string, 12);
strcat(string, " World");
}
In this example, I reallocate the space for a string declacred in
another function in the function foo. But, after it returns to main
from the function foo, the C string will only contain "Hello". The
changes from the function foo have disappered. I suspect this is
because the reallocation has gone out of scope - and I have no idea how
to keep the changes after the function returns.



$ b指向指针的$ b指针可能就是答案。


int main(无效)

{

char * string1;

string1 = malloc(6);

sprintf(string1," Hello");

foo(& string1);


}


void foo(char ** string)

{

* string = realloc (字符串,12);

strcat(* string," World");

}



pointer to pointer could be the answer.

int main(void)
{
char *string1;
string1 = malloc(6);
sprintf(string1, "Hello");
foo(&string1);

}

void foo(char **string)
{
*string = realloc(string, 12);
strcat(*string, " World");
}


intercom5写道:
intercom5 wrote:
我正在用C编写程序,因此必须使用C字符串。我遇到的问题是我不知道如何为该字符串范围之外的C字符串重新分配空间。例如:

int main(void)
{* char * string1;
string1 = malloc(6);
sprintf(string1,"你好);
foo(string1);

}

void foo(char * string)
{
string = realloc( string,12);
strcat(字符串,World);
}

在这个例子中,我为
另一个声明的字符串重新分配空间函数foo中的函数。但是,在从函数foo返回到main
之后,C字符串将只包含Hello。函数foo的变化已经消失了。我怀疑这是
因为重新分配已超出范围 - 我不知道如何在函数返回后保持更改。
I''m writing a program in C, and thus have to use C strings. The
problem that I am having is I don''t know how to reallocate the space
for a C string outside the scope of that string. For example:
int main(void)
{
char *string1;
string1 = malloc(6);
sprintf(string1, "Hello");
foo(string1);

}

void foo(char *string)
{
string = realloc(string, 12);
strcat(string, " World");
}
In this example, I reallocate the space for a string declacred in
another function in the function foo. But, after it returns to main
from the function foo, the C string will only contain "Hello". The
changes from the function foo have disappered. I suspect this is
because the reallocation has gone out of scope - and I have no idea how
to keep the changes after the function returns.




实际上,微妙的变化是因为,realloc已将所有内容移动到一个新地址。 (当然,这是允许这样做的)因为

所要求的新尺寸是>上一篇。尺寸。


OTOH,如果您要求的尺寸小于原始尺寸,

那么它可能无法移动它而只是重新分配内存。

在这种情况下,原始指针可能仍然有效

函数返回后。


realloc是一把多刃剑,的确:) 。请小心使用。



Actually the subtle change is because , realloc has moved everything
to a new address. (it is allowed to do that, of course ) because
the requested new size is > the prev. size.

OTOH, if you had requested less than the original size,
then it may not move it and just reallocate memory.
In that case, the original pointer might be still valid
after the function returns.

realloc is a multi-edged sword, indeed :) . Use it with care.




intercom5写道:


intercom5 wrote:
我正在写一个程序在C中,因此必须使用C字符串。我遇到的问题是我不知道如何为该字符串范围之外的C字符串重新分配空间。例如:

int main(void)
{* char * string1;
string1 = malloc(6);
sprintf(string1,"你好);
foo(string1);

}

void foo(char * string)
{
string = realloc( string,12); / *你在哪里得到这个魔法12?不是一个好习惯。 * /
strcat(string," World");
}
始终检查* alloc函数的返回值。

从foo返回字符串!

修改后的版本是:


char * foo(char * string)

{

string = realloc(string,12);

if(string)

strcat(string," World");


return string;

}

int main(无效)

{

char * string1;

string1 = malloc(6);

if(!string1)

返回0; / *返回前处理故障。 * /


sprintf(string1," Hello"); / *只需strcpy就可以了 -

strcpy(string1," Hello"); * /

string1 = foo(string1);

puts(string1);

返回0;

}


在这个例子中,我重新分配了函数foo中另一个函数声明的字符串的空间。但是,在从函数foo返回到main
之后,C字符串将只包含Hello。函数foo的变化已经消失了。我怀疑这是因为重新分配已超出范围 - 我不知道在函数返回后如何保持更改。
任何帮助都将非常感激。

--Sachin
I''m writing a program in C, and thus have to use C strings. The
problem that I am having is I don''t know how to reallocate the space
for a C string outside the scope of that string. For example:
int main(void)
{
char *string1;
string1 = malloc(6);
sprintf(string1, "Hello");
foo(string1);

}

void foo(char *string)
{
string = realloc(string, 12); /* Where did u get this magic 12 ? not a good practise. */
strcat(string, " World");
} Always check for return values of *alloc functions.
Return the string from foo !
Modified version is:

char *foo (char *string)
{
string = realloc(string, 12);
if (string)
strcat(string, " World");

return string;
}
int main(void)
{
char *string1;
string1 = malloc(6);
if (!string1)
return 0; /* Handle failures before returning. */

sprintf(string1, "Hello"); /* Just strcpy would do here -
strcpy(string1, "Hello"); */
string1 = foo(string1);
puts (string1);
return 0;
}


In this example, I reallocate the space for a string declacred in
another function in the function foo. But, after it returns to main
from the function foo, the C string will only contain "Hello". The
changes from the function foo have disappered. I suspect this is
because the reallocation has gone out of scope - and I have no idea how
to keep the changes after the function returns.
Any help would greatly be appreciated.

--Sachin






这篇关于动态C字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆