Thread: Problem with pointers

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    35

    Problem with pointers

    I have declared the following variable in a fuction:
    Code:
    char sFiles[MAX_NUMBER_OF_FILES][MAX_FILENAME];
    Then I have a function that retrieves the file listing of a given directory:
    Code:
    /****************************************************************************/
    DWORD DirListing
    (
        char*  sDir,
        char** sListing,
        int*   iLength
    )
    /****************************************************************************/
    {
        WIN32_FIND_DATA win32FindData;
        HANDLE          hFile;
        char            sDirListOfTxt[MAX_PATH];
        int             i;
        
        sprintf(sDirListOfTxt, "%s\\*.jar", sDir);
    
        i = 0;
        hFile = FindFirstFile(sDirListOfTxt, &win32FindData);
        if (hFile!=INVALID_HANDLE_VALUE) {
            strcpy(sListing[i], win32FindData.cFileName);
            i++;
            while (FindNextFile(hFile, &win32FindData)) {
                strcpy(sListing[i], win32FindData.cFileName);
                i++;
            }
            FindClose(hFile);
        }
        (*iLength) = i;
        return 0;
    }
    When I call this function I do:
    Code:
    DirListing(sDir, (char**)sFiles, &iFilesLength);
    But everytime I execute the program, it just exited before it even begans with a Windows error that doesn't tell anything about it.

    I guess it's a matter of pointers, but now I can't see where is the error. Any help?

    And another more question... If I don't want to declare sFiles that way, and I just want to make it char**, how can I allocate memory in order to store an array of strings.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    DWORD DirListing(char*  sDir,char** sListing,int*   iLength){
        WIN32_FIND_DATA win32FindData;
        HANDLE          hFile;
        char            sDirListOfTxt[MAX_PATH];
        int             i;
        
        sprintf(sDirListOfTxt, "%s\\*.jar", sDir);
    
        i = 0;
        hFile = FindFirstFile(sDirListOfTxt, &win32FindData);
        if (hFile!=INVALID_HANDLE_VALUE) {
           do{
                strcpy(sListing[i], win32FindData.cFileName);
                i++;
            }while (FindNextFile(hFile, &win32FindData)) ;
            FindClose(hFile);
        }
        (*iLength) = i;
        return 0;
    }
    A do...while can shorten your code

    The error
    char[][] is very diferent from char ** !!!!

    char [][] is an array of char[] which is stored sequentialy in memory
    This this
    Code:
    char a[2][2]={{'a','b'},{'c',0}};
    puts((char*)a);
    char* is an array of pointers to char. Each pointer holds a memory adress to wherelse in your heap, so you can't cast your var like that
    Code:
    char *a[2][2]={"abc","def"};
    printf("the adresses %x %x\n",a[0],a[1])
    puts(a[0]);puts(a[1]);
    possibles fixes:
    change the function prototipe
    Code:
    DWORD DirListing
    (
        char*  sDir,
        char sListing[][MAX_FILENAME],
        int*   iLength
    )
    /***
    redeclare your double array, and alocate memory
    Code:
    char *sFiles[MAX_NUMBER_OF_FILES];
    
    for(i=0;i<MAX_NUMBER_OF_FILES;i++){
        sFiles[i]=calloc(MAX_FILENAME,1);
    }
    or use static memory,
    Code:
    char sFilesBuffer[MAX_NUMBER_OF_FILES][MAX_FILENAME];
    
    char *sFiles[MAX_NUMBER_OF_FILES];
    
    for(i=0;i<MAX_NUMBER_OF_FILES;i++){
        sFiles[i]=sFilesBuffer[i];
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Given this
    char sFiles[MAX_NUMBER_OF_FILES][MAX_FILENAME];

    The prototype/declaration of the function is this
    DWORD DirListing
    (
    char* sDir,
    char sListing[MAX_NUMBER_OF_FILES][MAX_FILENAME]
    char** sListing,
    int* iLength
    )

    And the call is
    DirListing(sDir, sFiles, &iFilesLength);

    Casting is bad for you. If you cast simply to make the compiler shut up, then you're probably doing the wrong thing.

    > while (FindNextFile(hFile, &win32FindData))
    Better change this to
    while ( i < MAX_NUMBER_OF_FILES && FindNextFile(hFile, &win32FindData))
    You don't want to blow up just because you found a large number of directory entries...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  3. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM