【问题标题】:Scanf the numbers into function in C [closed]将数字扫描到C中的函数[关闭]
【发布时间】:2021-02-11 04:19:15
【问题描述】:

这是我写的代码。

#include <stdio.h>
#include <stdlib.h>

typedef struct note {
    int pitch;
    int velocity;
    int channel;  
} note;

int printnote(int pitch, int velocity, int channel);

int main() {
    int size = 100;
    note note;
    struct note *ptr = malloc(size * sizeof(int));

    printf("Input the values for the `pitch`, `velocity`, and `channel`\n");
    scanf("%d %d %d", &note.pitch, &note.velocity, &note.channel);
    printnote(note.pitch, note.velocity, note.channel);
    free(ptr);

    return 0;
}

int printnote(pitch, velocity, channel) {
    printf("The MIDI Note is:\n");
    printf("Pitch -> %d\n", pitch);
    printf("velocity -> %d\n", velocity);
    printf("channel -> %d\n", channel);
    return 0;
};

当我运行代码并输入数字时,它会显示错误的答案。 例如,我运行代码,它显示

输入“pitch”、“velocity”和“channel”的值 5 5 5 MIDI 音符是: 间距 -> 5 速度 -> 0 频道 -> -429762432

这三个数字应与输入的数字相同。 谁能帮帮我?

【问题讨论】:

  • %d with full-width 作为格式说明符无效。请改用%d。投票结束是错字。
  • 即使我得到了与@MikeCAT 相同的观察结果,第二个 %d 看起来与其他两个 scanf("%d %d %d", &amp;note.pitch, &amp;note.velocity, &amp;note.channel); 不同,还有一件事是为什么分配和释放内存,尽管 malloc 的大小错误
  • @sravs 为什么说分配大小不对? ptr 除了分配和释放之外不使用,所以我认为没有任何正确的大小可以在那里分配。 (或者也许零是正确的,因为它没有被使用)
  • @MikeCAT malloc(size * sizeof(int)); 应该是malloc(size * sizeof(struct note)); 如果他真的想用于存储多条记录
  • @sravs 啊,我忘了ptr 的类型是struct note *malloc(size * sizeof(*ptr)); 可能会更好。

标签: c function scanf


【解决方案1】:

您的scanf() 转换字符串中有一个细微的错字:

scanf("%d %d %d", &note.pitch, &note.velocity, &note.channel);

您使用了 unicode 全角百分号 (\uff05) 而不是 ASCII % 字符。 scanf 不将其识别为转换说明符,并尝试匹配用于编码 的字节序列(UTF-8 中为 0xEF 0xBC 0x85)并失败,因此仅将第一个输入转换为 note.pitch 并留下 @987654328 @ 和 note.channel 未初始化,返回 1。请注意 在用于代码的固定字体中与 % 看起来有何不同,但在用于此文本的字体中是相同的:% % % %

只需将 替换为正确的字符即可:

scanf("%d %d %d", &note.pitch, &note.velocity, &note.channel);

还要注意这些备注:

  • sizeptrmain()中没有使用,
  • 您应该检查scanf() 的返回值以检测无效输入。此检查将有助于发现错误,
  • printnote 定义中的原型不正确:缺少参数类型,
  • } 后面的; 没用,
  • 对变量使用相同的标识符note,其类型令人困惑。

这里是修改版:

#include <stdio.h>

typedef struct note {
    int pitch;
    int velocity;
    int channel;  
} note;

void printnote(int pitch, int velocity, int channel);

int main() {
    note note1;

    printf("Input the values for the `pitch`, `velocity`, and `channel`\n");
    if (scanf("%d %d %d", &note1.pitch, &note1.velocity, &note1.channel) != 3) {
        printf("invalid input\n");
        return 1;
    }
    printnote(note1.pitch, note1.velocity, note1.channel);
    return 0;
}

void printnote(int pitch, int velocity, int channel) {
    printf("The MIDI Note is:\n");
    printf("pitch -> %d\n", pitch);
    printf("velocity -> %d\n", velocity);
    printf("channel -> %d\n", channel);
}

【讨论】:

  • “忽略它,只转换 2 个输入”不正确。它将期望输入匹配,发现它不匹配并停在那里。数据只读取到note.pitchnote.velocitynote.channel 将保持未初始化状态。
  • @MikeCAT:很好的收获!我没有想清楚……是时候在宵禁前回家了!答案已修改。
猜你喜欢
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 2015-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多