English 中文(简体)
C. C. C. C. C. C. C. C. C. C. C. C. C. C. C. A. C. C. C. C. C. C. A. C. C. C. C. C. C. C. C. C. C. C. C. A. C. C. C. C. C. C. C. C.
原标题:"Expected token ) got int" in C
  • 时间:2012-05-24 16:19:14
  •  标签:
  • c
  • qt-creator

我对下面的代码有一个小问题。 VS 2010 编译它, 运行并获得预言结果。 但是当我试图与 Qt 创造者编译这个代码时, 我每次得到警告 : “ 预想的象征 ” 都得到了 。 是的, 程序将由 Qt 创造者运行, 但程序会爆炸。 这个代码有什么问题 :

#include <stdio.h>
#include <stdarg.h>
#define ARR_SIZE 2

int **getAddresses(int amount, ...)
{
    static int *arr[ARR_SIZE] = {};
    va_list vl;

    if(amount > 0)
    {
        va_start(vl, amount);
        int i;
        for(i = 0; i < amount; i++)
        {
            *(arr + sizeof(int) * i) = va_arg(vl, int*);  //This one is highlighted by the Qt Creator.
        }
        va_end(vl);
        return 0;
    }
    else
    {
        return arr;
    }
}

int main(void)
{
    int a = 3, b = 5;

    getAddresses(ARR_SIZE, &a, &b);
    printf("BEFORE: %d, %d
", a, b);

    int **res = getAddresses(0), i;
    for(i = 0; i < ARR_SIZE; i++)
    {
        *(*(res + sizeof(int) * i)) += 5;
    }

    printf("AFTER: %d, %d
", a, b);

    return 0;
}

感谢您事先给出的答案

ADDED: Qt 创建者突出显示代码 * (arr + sizef(int) * i) = va_arg(vl, int* ) 的这一行;

此外,Dev++能够在没有任何警告和错误或压碎的情况下运行这个代码。

海合会能够按照Fedora Linux 14编集:

[Admin@localhost testerprog]$ gcc tester.c -o tester
[Admin@localhost testerprog]$ ls
tester  tester.c
[Admin@localhost testerprog]$ ./tester
BEFORE: 3, 5
AFTER: 8, 10
[Admin@localhost testerprog]$ 

海合金版本为4.5.1 20100924(红帽子4.5.1-4)

最佳回答

- 编辑,因为原始文章有点错误, 但同一行可能是原因...

由于错误具体提到找到一个内字而不是结束括号,我怀疑违规线是

        *(arr + sizeof(int) * i) = va_arg(vl, int*);

虽然你的代码是有效的 C/C+++, 我倒会怀疑 VS 编译器犯了一个错误, 但我也会怀疑 Qt 编译器

  1. Has a bug in it and is expecting a R value, not a type.
  2. Has wrapped va_arg references in some other qt_xxx style wrapper to allow proper compiling in the QT framework.

原职位如下:

int 是 C / C++ 中的关键关键字, 所以您无法安全地使用它作为变量。 这行似乎

        *(arr + sizeof(int) * i) = va_arg(vl, int*);

询问变量 int 所持有地址的值, 而不是int指针引用的值。 奇怪的是, VS 编译器错误检查不那么可靠, 导致对错误的沉默接受 。

问题回答
    va_start(vl, amount);
    int i;

不是C级的

定义必须在代码之前发生。

运行 C/C++ 模式的 VC 将不在意。 C 唯一的编译器将反对 。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签