我在代码中使用功能,不再使用它

发布于 2025-01-22 19:22:47 字数 631 浏览 0 评论 0 原文

因此,我正在练习一些有关功能如何工作的编码,并且遇到了一个问题:
该代码旨在扭转一个数字。该算法效果很好,因此我对此没有任何问题。但是我想在我的代码中使用函数,因此我像下面一样对其进行了编辑,并且它不再起作用(没有错误,但是当我运行代码时,在我进入第一个SCANF之后,代码停止并保持像那没有回应)。有人可以帮助我解决这个问题吗? 代码:

#include <stdio.h>
#include <string.h>

int input(int *a) {
    scanf("%d", &*a);
}

int revint(int *a, int *b){
    int c;
    while(a != 0)
    {
        c = *a % 10;
        *b *= 10;
        b += c;
        *a /= 10;
    }
    return *b;
}

int output(int b) {
    printf("%d", b);
}

int main(){
    int a;
    int b = 0;
    input(&a);
    revint(&a, &b);
    output(b);
    
    return 0;
}

So I was practicing some coding about how functions work and I ran into a problem:
The code is meant to reverse a number. The algorithm works perfectly well so I don't have any problem with that. But I want to use functions in my code so I edited it like below and somehow it doesn't work anymore(there was no error, but when I run the code, after I entered in the first scanf, the code stopped and stays like that, no response). Can someone help me with this pls (This question may sound a little stupid but I'm just trying to be better at it :v)
Code:

#include <stdio.h>
#include <string.h>

int input(int *a) {
    scanf("%d", &*a);
}

int revint(int *a, int *b){
    int c;
    while(a != 0)
    {
        c = *a % 10;
        *b *= 10;
        b += c;
        *a /= 10;
    }
    return *b;
}

int output(int b) {
    printf("%d", b);
}

int main(){
    int a;
    int b = 0;
    input(&a);
    revint(&a, &b);
    output(b);
    
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不知所踪 2025-01-29 19:22:47

您的代码中有几个问题:

  1. input() output()没有 return 语句,应声明返回 void 在您的代码中。
  2. 对于 input()返回值而不是填充指针参数更有意义。因此,在我的版本中 input()确实返回了正确的 int 值。
  3. A b revint()的几次出现应为*a *b 代码>(因为 a b 是指示器,我们需要先推荐它)。

请参阅下面的代码:

int input(void) {
    int a;
    scanf("%d", &a);
    return a;
}

int revint(int *a, int *b)
{
    int c;
    *b = 0;
    while (*a != 0)
    {
        c = *a % 10;
        *b = *b * 10 + c;
        *a /= 10;
    }
    return *b;
}

void output(int b) {
    printf("%d", b);
}

int main(void)
{
    int a;
    int b = 0;
    a = input();
    revint(&a, &b); // passing the addresses
    output(b);
    return 0;
}

注意:为简单起见, input()函数不会验证 scanf()实际上成功了。在用户处理的真实代码中,应该更好地完成。

更好的 scanf()

if (scanf("%d", &a) != 1) 
{
    fprintf(stderr, "bad input\n");
    exit(1);
}

在实际代码中,用户可以考虑重试输入,只要它无效。

更新:
正如您在 @gerhard的答案中看到的那样, revint 可以重新编写,以避免完全避免使用指针语义。它确实会使代码变得更简单,对于此特定用例,我实际上认为它更好。
我的答案是为了尽可能地维护用户在问题代码中介绍的语义。因此,我建议您只有在通过指针确实有意义的情况下应用我的解决方案。

There are several issues in your code:

  1. input() and output() do not have a return statement and should be declared to return void in your code.
  2. It makes more sense for input() to return the value rather then fill a pointer parameter. Therefore in my version input() does return a proper int value.
  3. Several occurrences of a, b in revint() should be *a,*b (since a and b are pointers and we need to de-reference it first).

See the code below:

int input(void) {
    int a;
    scanf("%d", &a);
    return a;
}

int revint(int *a, int *b)
{
    int c;
    *b = 0;
    while (*a != 0)
    {
        c = *a % 10;
        *b = *b * 10 + c;
        *a /= 10;
    }
    return *b;
}

void output(int b) {
    printf("%d", b);
}

int main(void)
{
    int a;
    int b = 0;
    a = input();
    revint(&a, &b); // passing the addresses
    output(b);
    return 0;
}

NOTE: For simplicity the input() function doesn't validate that scanf() actually succeeded. In should better be done in the real code in which the user is handling.

Better scanf():

if (scanf("%d", &a) != 1) 
{
    fprintf(stderr, "bad input\n");
    exit(1);
}

In the real code the user can consider retrying getting the input as long as it is invalid.

Update:
As you can see in @Gerhard's answer, revint could be re-written to avoid the pointer semantics altogether. It will indeed make the code simpler and for this particular use case I actually think it is better.
My answer was written trying to maintain as much as possible the semantics that the user introduced in his question code. So I suggest you apply my solution only if it really makes sense for you to pass pointers.

辞旧 2025-01-29 19:22:47

输入更改为正确使用指针(&amp;*a )。

将指针删除到 revint ,因为它没有帮助,并且您的代码在那里存在点错误( b += c; 修改指针而不是值)。

#include <stdio.h>
#include <string.h>

void input(int *a) {
    scanf("%d", a);
}

int revint(int a){
    int b = 0;    
    int c;
    while(a != 0)
    {
        c = a % 10;
        b *= 10;
        b += c;
        a /= 10;
    }
    return b;
}

int output(int b) {
    printf("%d", b);
}

int main(){
    int a;
    int b = 0;
    input(&a);
    b = revint(a);
    output(b);
    
    return 0;
}

input changed to use the pointer correctly (&*a).

Removed the pointers to revint as it is not helping and your code had an point error there (b += c; modifies pointer not value).

#include <stdio.h>
#include <string.h>

void input(int *a) {
    scanf("%d", a);
}

int revint(int a){
    int b = 0;    
    int c;
    while(a != 0)
    {
        c = a % 10;
        b *= 10;
        b += c;
        a /= 10;
    }
    return b;
}

int output(int b) {
    printf("%d", b);
}

int main(){
    int a;
    int b = 0;
    input(&a);
    b = revint(a);
    output(b);
    
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文