数组指针函数

发布于 2024-12-12 20:32:04 字数 662 浏览 0 评论 0原文

我一直在寻找解决这个问题的方法,但还没有找到。

我有一个函数可以进行一些字符串操作(简化):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绿光 2024-12-19 20:32:05

您应该这样做:

void plr(char str[], char stro[])
{
     strcpy(stro, str);
}

int main(void)
{
    char str[25], stro[25];
    printf("Something please: ");
    scanf("%s", &str); //unsafe code

    plr(str, stro); 
    printf("Copy succesfull, %s", stro);
    return 0;
}

在使用指向数组或字符串的指针时请非常小心:

  1. http://pw1.netcom.com/~tjensen/ptr/pointers.htm

使用 scanf 从用户处获取字符串对于更多人来说确实是一件坏事:

  1. scanf 的缺点

You should be doing this:

void plr(char str[], char stro[])
{
     strcpy(stro, str);
}

int main(void)
{
    char str[25], stro[25];
    printf("Something please: ");
    scanf("%s", &str); //unsafe code

    plr(str, stro); 
    printf("Copy succesfull, %s", stro);
    return 0;
}

Please be very careful when using a pointer to an array or a string for more :

  1. http://pw1.netcom.com/~tjensen/ptr/pointers.htm

Using scanf to get a string from the user is really a bad thing for more:

  1. Disadvantages of scanf
冰火雁神 2024-12-19 20:32:05

无需用指针搞乱;)

blackbear@blackbear-laptop:~$ cat prova.c
#include <stdio.h>
#include <string.h>

void foo(char *strin, char *strout)
{
    strcpy(strout, strin);
}

int main(void)
{
    char a[100], b[100];

    printf("What's a? ");
    scanf("%s", a);

    printf("What's b? ");
    scanf("%s", b);

    foo(a, b);

    printf("a is \"%s\"\nb is \"%s\"\n", a, b);
}
blackbear@blackbear-laptop:~$ gcc prova.c
blackbear@blackbear-laptop:~$ ./a.out 
What's a? abc
What's b? def
a is "abc"
b is "abc"
blackbear@blackbear-laptop:~$ 

说明:
这是可行的,因为当您使用数组的名称时,它会分解为指向其第一个元素的指针。所以 foo(a, b) 实际上是 foo(&a[0], &b[0])。因此,即使 ab 是数组,将它们传递给函数也会将它们“转换”为指针。
引用自此处

当您将数组作为参数传递给函数时,您实际上传递了
指向数组第一个元素的指针,因为数组衰减为
指针。

并且,下面几行:

腐烂是一种隐含的&;数组 == &数组 == &数组[0]。用英语讲,
这些表达式读取“数组”、“指向数组的指针”和“指向数组的指针”
数组的第一个元素”(下标运算符 [] 具有更高的
优先于地址运算符)。但在 C 语言中,这三个
表达式的意思是相同的。

总之,代码中的问题只是 plr 的原型。

查找数组到指针衰减以获取有关此现象的更多信息。 :)

No need to mess up that way with pointers ;)

blackbear@blackbear-laptop:~$ cat prova.c
#include <stdio.h>
#include <string.h>

void foo(char *strin, char *strout)
{
    strcpy(strout, strin);
}

int main(void)
{
    char a[100], b[100];

    printf("What's a? ");
    scanf("%s", a);

    printf("What's b? ");
    scanf("%s", b);

    foo(a, b);

    printf("a is \"%s\"\nb is \"%s\"\n", a, b);
}
blackbear@blackbear-laptop:~$ gcc prova.c
blackbear@blackbear-laptop:~$ ./a.out 
What's a? abc
What's b? def
a is "abc"
b is "abc"
blackbear@blackbear-laptop:~$ 

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 is foo(&a[0], &b[0]). So, even if a and b are arrays, passing them to a function "converts" them to a pointer.
Quoting from here:

When you pass an array as an argument to a function, you really pass
a pointer to the array's first element, because the array decays to a
pointer.

and, a few lines below:

Decaying is an implicit &; array == &array == &array[0]. In English,
these expressions read “array”, “pointer to array”, and “pointer to
the first element of array” (the subscript operator, [], has higher
precedence than the address-of operator). But in C, all three
expressions mean the same thing.

Concluding, the problem in your code is just plr's prototype.

Look for array to pointer decay for more info about this phenomena. :)

娇柔作态 2024-12-19 20:32:05

也许您需要一个指向指针的指针,即声明您的函数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)

相权↑美人 2024-12-19 20:32:05

我相信你必须将代码更改为:

void plr(char* str, char *stro)

你想传入一个数组str,你必须使用一个指针char *str。

我认为您不能将数组直接传递给函数。

如果你声明 char str[25];编译器为数组保留内存,如果使用 char * 它将仅指向数组中的第一个元素。

我希望这能起作用!

先生

迪特尔

I believe that you have to change the code to:

void plr(char* str, char *stro)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文