数组指针函数
我一直在寻找解决这个问题的方法,但还没有找到。
我有一个函数可以进行一些字符串操作(简化):
void plr(char str, char *stro){
strcpy(*stro, str);
}
我的问题在于我无法从函数中得到结果:
int main(void){
//string and string out.
char str[25], stro[25];
printf("Something please: ");
scanf("%s", &str);
plr(str, &stro); // So basically stro would be the same as str.
printf("Copy succesfull, %s", stro);
return 0;
}
整个想法是我有函数pluralis,它将把pluralis附加到字符串中给定并将其输出到 stro.整个字符串操作已经过测试并且可以工作,如果它在 main() 内部,但我根本无法让它与函数和指针一起工作。显然我可以不去管它,但我能从中学到什么。
当我指向的数组而不是普通的值时,我需要考虑什么吗?
编辑:感谢大家的帮助,已经解决了。非常感谢大家!
I've been looking around for a solution to this, but haven't quite found one.
I've got a function which do some string manipulation (simplified):
void plr(char str, char *stro){
strcpy(*stro, str);
}
My issue lies in the fact that I cannot get my result out from the function:
int main(void){
//string and string out.
char str[25], stro[25];
printf("Something please: ");
scanf("%s", &str);
plr(str, &stro); // So basically stro would be the same as str.
printf("Copy succesfull, %s", stro);
return 0;
}
The whole idea is that I have the function pluralis, which would append pluralis to the string given and output it to stro. The whole string manipulation has been tested and works, if it's inside the main(), but I simply cannot get it to work with a function and the pointer. I could obviously leave it be, but what would I learn from that.
Is there something I need to consider when it's an array I point to, rather than a normal value of sorts.
Edit: Thanks for all the help, it has been solved. Greatly appreciated all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该这样做:
在使用指向数组或字符串的指针时请非常小心:
使用 scanf 从用户处获取字符串对于更多人来说确实是一件坏事:
You should be doing this:
Please be very careful when using a pointer to an array or a string for more :
Using scanf to get a string from the user is really a bad thing for more:
无需用指针搞乱;)
说明:
这是可行的,因为当您使用数组的名称时,它会分解为指向其第一个元素的指针。所以
foo(a, b)
实际上是foo(&a[0], &b[0])
。因此,即使a
和b
是数组,将它们传递给函数也会将它们“转换”为指针。引用自此处:
并且,下面几行:
总之,代码中的问题只是
plr
的原型。查找数组到指针衰减以获取有关此现象的更多信息。 :)
No need to mess up that way with pointers ;)
Explaination:
This works because when you use the name of an array it decays to a pointer to its first element. So
foo(a, b)
actually isfoo(&a[0], &b[0])
. So, even ifa
andb
are arrays, passing them to a function "converts" them to a pointer.Quoting from here:
and, a few lines below:
Concluding, the problem in your code is just
plr
's prototype.Look for array to pointer decay for more info about this phenomena. :)
也许您需要一个指向指针的指针,即声明您的函数
void plr(char str, char **stro)
Perhaps you need a pointer to a pointer, i.e. declare your function
void plr(char str, char **stro)
我相信你必须将代码更改为:
你想传入一个数组str,你必须使用一个指针char *str。
我认为您不能将数组直接传递给函数。
如果你声明 char str[25];编译器为数组保留内存,如果使用 char * 它将仅指向数组中的第一个元素。
我希望这能起作用!
先生
迪特尔
I believe that you have to change the code to:
You want to pass in an array str, you have to use a pointer char *str.
I don't think you can pass arrays directly to functions.
if you declare char str[25]; the compiler reserves memory for the array, if you use char * it will just point to the first element in the array.
I hope this works!
Gr,
Dieter