传递“atoi”的参数 1;无需强制转换即可从整数生成指针......任何人都可以帮助我

发布于 2024-09-05 13:10:17 字数 727 浏览 4 评论 0原文

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

int main(){
    int n;
    int a,b,ans[10000];
    char *c,*d,*e;
    int i = 0;
    c = (char*)(malloc(20 * sizeof(char)));
    d = (char*)(malloc(20 * sizeof(char)));
    scanf("%d",&n);
    while(i < n){
            scanf("%d",&a);
            scanf("%d",&b);
            itoa(a,c,10);
            itoa(b,d,10);
            a = atoi(strrev(c)) + atoi(strrev(d));
            itoa(a,c,10);
            e = c;
            while(*e == '0')e++;
            ans[i] = atoi(strrev(e));
            i++;
            }
    i = 0;
    while(i < n){
            printf("%d\n",ans[i]);
            i++;
            }
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(){
    int n;
    int a,b,ans[10000];
    char *c,*d,*e;
    int i = 0;
    c = (char*)(malloc(20 * sizeof(char)));
    d = (char*)(malloc(20 * sizeof(char)));
    scanf("%d",&n);
    while(i < n){
            scanf("%d",&a);
            scanf("%d",&b);
            itoa(a,c,10);
            itoa(b,d,10);
            a = atoi(strrev(c)) + atoi(strrev(d));
            itoa(a,c,10);
            e = c;
            while(*e == '0')e++;
            ans[i] = atoi(strrev(e));
            i++;
            }
    i = 0;
    while(i < n){
            printf("%d\n",ans[i]);
            i++;
            }
}

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

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

发布评论

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

评论(2

甜心 2024-09-12 13:10:17

您的程序中没有声明 strrev 这样的函数。编译器假定它是某个返回 int 的未知函数。因此,诊断消息是这样的,因为 atoi 需要一个指针,而不是 int

什么是strrev?为什么你在不先声明的情况下尝试调用这个函数? C 标准库没有这样的函数,因此包含您已经包含的那些标准头文件是不够的(除非您假设一些扩展实现)。

There's no such function as strrev declared in your program. The compiler assumes that it's some unknown function that returns int. Hence the diagnostic message, since atoi expects a pointer, not an int.

What's strrev? And why are you attempting to call this function without declaring it first? C standard library has no such function, so including those standard headers that you have included already is not enough (unless you are assuming some extended implementation).

蒗幽 2024-09-12 13:10:17

除了 strrev 的问题不是标准的并且可以很容易实现,例如

char *strrev(char *s)
{
  size_t l = strlen(s), i;
  char *r = malloc(l + 1);
  if ( r != NULL ) {
    for(s += l-1, i=0; i < l; i++, s--) r[i] = *s;
    r[i] = '\0';
  }
  return r;
}

只是说(非就地恢复),您应该更喜欢使用 strtol 或 < code>strtoul 而不是 atoi 并实现 itoa 因为据我所知这也不是标准的(无论如何你都可以使用 sprintf 如果基数为 10)。

Apart the problem of strrev which is not standard and can be easily implemented e.g.

char *strrev(char *s)
{
  size_t l = strlen(s), i;
  char *r = malloc(l + 1);
  if ( r != NULL ) {
    for(s += l-1, i=0; i < l; i++, s--) r[i] = *s;
    r[i] = '\0';
  }
  return r;
}

just to say (not-in-place reversion), you should prefer the use of strtol or strtoul instead of atoi and implement also itoa since afaik that is not standard too (you could use sprintf anyway if the base is 10).

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