-1

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! 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!

Correct me if I'm wrong but I believe this is perfectly-valid C99 code I'm writing here...so what gives?! I've tried both the GNU99 compiler (default) and C99 compiler, to no avail.

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.

I'm actually writing a libcurl app on Mac OS X Yosemite to upload files to a web server over HTTP. 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]. The inputted paths can be relative to the CWD or absolute.

The problem exists in my uploadDirectory function, as shown here:

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? What is the maximum length of a file path in 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 :-)

Kenny83
  • 769
  • 12
  • 38
  • 2
    look at the assembler output, your program can be optimized to `printf("hello");`. – mch Jan 22 '15 at 17:09
  • You imply this code doesn't print anything? – alk Jan 22 '15 at 17:09
  • The code works, I ran it and the output was: "hello". It would be best to add a "\n" to the `printf` function: `printf("output: '%s'\n", test);` – zaph Jan 22 '15 at 17:10
  • Did you `#include `? – Dmitri Jan 22 '15 at 17:27
  • Wow you all replied so quickly I didn't have the chance to fix my obvious mistakes LOL...I've now edited my question accordingly. And BTW, this is just example code, I'm not really trying to print "hello" to the console LOL...what I'm really trying to do is much more complicated and would take up 10 times more space and time to write it all out...but the gist of the problem area is captured here. – Kenny83 Jan 22 '15 at 17:30
  • The shown code should work and add `return 0` at the end on `main()` I ran it and got proper output – Gopi Jan 22 '15 at 17:37
  • @Gopi: I know it **should** work, and I'm sure it would if compiled with GCC etc, but it isn't working for me in Xcode, hence this post...did you use Xcode to test it? If so, what non-default options might I need to set in order for this to work? Thanks heaps :D – Kenny83 Jan 22 '15 at 17:44
  • You 1st snippet misses to prototype `printf()` so you shall include ``. – alk Jan 24 '15 at 10:20
  • Also the definition of `filePath` and its usage is perfectly valid. If the code as shown fails, the root cause most propably lies elsewhere. Memory corruptions are known to cause somewhat irrational behaviour of code, so you might like to want to debug your program using a memory checker like Valgrind (http://valgrind.org). – alk Jan 24 '15 at 10:22

2 Answers2

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

char a[N];

a is a VLA. The array shown in your example is not a VLA.

VLA's are supported in C99. If you are not able to see the output try

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

Apart from this your code looks good.

Gopi
  • 19,784
  • 4
  • 24
  • 36
1

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. This is an acceptable solution since that define will always be updated when necessary.

Community
  • 1
  • 1
Kenny83
  • 769
  • 12
  • 38