简体   繁体   English

C99-为什么我不能在Xcode 6中使用可变长度的char数组?

[英]C99 - Why can't I use a variable-length char array in Xcode 6?

Got a C Command-Line tool project going in Xcode 6, and everything works great except one little thing: I can't for the life of me figure out how to assign a value to a variable-length array! 在Xcode 6中得到了一个C命令行工具项目,除了一件小事情,其他所有东西都运行良好:我一生都无法弄清楚如何为可变长度数组赋值! For example, consider the following code: 例如,考虑以下代码:

#include <string.h>
int main()
{
    int len = 10;
    char str[len];
    strcpy(str, "hello");
    printf("%s", str);
}

This compiles fine but when I debug it, the array never gets assigned! 这样编译就可以了,但是当我调试它时,该数组永远不会被分配! If I set a breakpoint on line 7, hit the run button and hover over str , it just displays that name with an arrow beside it, which when expanded shows nothing! 如果我在第7行上设置了一个断点,请点击运行按钮,并将鼠标悬停在str ,它只会显示该名称,旁边带有一个箭头,展开时不会显示任何内容!

Correct me if I'm wrong but I believe this is perfectly-valid C99 code I'm writing here...so what gives?! 如果我错了,请指正我,但我相信这是我在这里写的完全有效的C99代码...那有什么用呢? I've tried both the GNU99 compiler (default) and C99 compiler, to no avail. 我已经尝试了GNU99编译器(默认)和C99编译器,都无济于事。

MTIA :-) MTIA :-)

EDIT: OK I seem to have confused and possibly even PO'ed a few people here (since I've earned at least 3 down votes on this question) so let me clarify things a bit. 编辑:好的,我似乎感到困惑,甚至可能在这里给几个人定罪(因为我在这个问题上至少获得3票赞成票),所以让我澄清一下。

I'm actually writing a libcurl app on Mac OS X Yosemite to upload files to a web server over HTTP. 我实际上是在Mac OS X Yosemite上编写一个libcurl应用程序,以通过HTTP将文件上传到Web服务器。 In the end I want to be able to type something like "upload [destination url] [file or directory 1] [file or directory 2] ... [file or directory N]" in Terminal, and have my program automatically upload those files and directories to [destination url]. 最后,我希望能够在终端中输入“ upload [目标url] [文件或目录1] [文件或目录2] ... [文件或目录N]”,然后让我的程序自动上传文件和目录到[目标url]。 The inputted paths can be relative to the CWD or absolute. 输入的路径可以相对于CWD或绝对路径。

The problem exists in my uploadDirectory function, as shown here: 该问题存在于我的uploadDirectory函数中,如下所示:

void uploadDirectory(CURL* hnd, struct curl_httppost* post,
    struct curl_httppost* postEnd, char* path, const char* url)
{
    DIR* dir = opendir(path);
    if (dir == NULL)
    {
        printf("\nopendir failed on path \"%s\"", path);
        perror(NULL);
    }
    else
    {
        struct dirent* file;
        while ((file = readdir(dir)) != NULL)
        {
            // skip the current directory and parent directory files
            if (!strcmp(file->d_name, ".") ||
                !strcmp(file->d_name, ".."))
                continue;

            if (file->d_type == DT_REG)
            {
                // file is an actual file; upload it
                // this is the offending code
                char filePath[strlen(path) + strlen(file->d_name) + 2];
                strcpy(filePath, path);
                strcat(filePath, "/");
                strcat(filePath, file->d_name);

                int res = uploadFile(hnd, post, postEnd, filePath, url);
                printf("%d", res);
            }
            if (file->d_type == DT_DIR)
            {
                // file is a directory; recurse over it
                // this section is omitted for brevity
            }
        }
        closedir(dir);
    }
}

I know I could define filePath with a huge constant size to fix the problem, but how big is too big? 我知道我可以用一个巨大的常量大小定义filePath来解决问题,但是太大太大了吗? What is the maximum length of a file path in OS X? OS X中文件路径的最大长度是多少? Etc, etc...so I'd prefer to make it exactly the right size. 等等,所以我更希望将其尺寸正确设置。

If you braved the extreme length of this post and got to here, thanks for being so patient! 如果您勇敢地发表这篇文章的篇幅太长了,请到这里,谢谢您的耐心等待! I originally tried to describe the problem as succinctly as I could but that obviously caused nothing but confusion, so I apologise for that :-) 我最初试图尽可能简洁地描述问题,但这显然引起了混乱,因此我对此表示歉意:-)

int N;
scanf("%d",&N);

char a[N];

a is a VLA. a是VLA。 The array shown in your example is not a VLA. 您的示例中显示的阵列不是VLA。

VLA's are supported in C99. C99支持VLA。 If you are not able to see the output try 如果您看不到输出,请尝试

printf("%s\n", test);

Apart from this your code looks good. 除此之外,您的代码看起来不错。

I ended up doing some research on maximum path lengths in OS X and came across this stackoverflow.com post which gave me the idea to use <sys/syslimits.h> 's PATH_MAX define. 我最终对OS X中的最大路径长度进行了一些研究,并遇到了这个stackoverflow.com帖子 ,这使我<sys/syslimits.h>使用<sys/syslimits.h>PATH_MAX定义的想法。 This is an acceptable solution since that define will always be updated when necessary. 这是可以接受的解决方案,因为该定义将始终在必要时进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM