Thread: **char problems in C

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    7

    **char problems in C

    Hello,
    I need to write a function as follows:

    //return a list of all the names d has
    char** ls(directory* dir);

    struct directory{
    char* name;
    int num_of_files;
    struct directory *next;
    }


    My problem is that name could be of any size and d can be of any size and I can't get the dynamic allocation to work for **char. Here is an attempt:

    ret = (char**)malloc(sizeof(dir->num_of_files)*sizeof(char*));
    for(i=0; i<dir->num_of_files; i++){
    ret[i] = malloc(sizeof(char)*sizeof(dir->name));
    ret[i] = dir->name;
    dir->files = dir->files->next;
    }

    Hope someone can straighten this out for me and write out how this should be done. Send an email to [email protected]

    Thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> ret = (char**)malloc(sizeof(dir->num_of_files)*sizeof(char*));

    dir->num_of_files is an int, so sizeof() will return the value of 4 (usually). Just omit that then.

    >> ret[i] = malloc(sizeof(char)*sizeof(dir->name));

    since dir->name is a pointer, sizeof() will return the size of a pointer, which is 4 (usually). Use strlen(dir->name)+1.

    >> ret[i] = dir->name;

    That line effectively loses track of the memory you allocated in the previous line of code. Perhaps you meant to use strcpy()?

    >> dir->files = dir->files->next;

    I didn't see a data member 'files' there, hmmm. Anyhow, be careful when playing with pointers belonging to other data structures. Best bet is to declare a separate 'iterating' pointer, and move through the data that way....
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    7
    Thanks a lot, I'm able to get it two work now that I use the string copy function. it's off putting because you often see things like

    x = "blah";

    where x is of type char*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM