Thread: Array Copying

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    22

    Array Copying

    Hi all--

    This issue is completely perplexing me, the following is code that reads in lines from a file and is supposed to copy each line into an array:

    Code:
    235                                                                 char* buffer = malloc(sizeof(1024));
    236                                                                 char** fileStrings = malloc(sizeof(1024)*40);
    237                                                                 FILE* redirFile = file_open(argvec[j+1]);
    238 
    239                                                                 j=0;
    240                                                                 for(buffer = file_getline(buffer, redirFile); buffer != NULL;
    241                                                                                 buffer = file_getline(buffer,redirFile)) {
    242                                                                         buffer = chomp(buffer);
    243                                                                         fprintf(stderr, "Buffer:%s\n", buffer);
    244                                                                         fileStrings[j]=buffer;
    245                                                                         fprintf(stderr, "First element:%s\n", fileStrings[0]);
    246                                                                         fprintf(stderr, "Second element:%s\n", fileStrings[1]);
    247                                                                         j++;
    248                                                                 }
    The following is the output from this code snippet:

    Code:
    Buffer:1
    First element:1
    Second element:(null)
    Buffer:2
    First element:2
    Second element:2
    What the heck is going on here?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    sizeof(1024)
    This does not do what you expect. This takes the size of the type of the expression 1024. 1024 is an int, so this is effectively sizeof(int). If you want to allocate 1024 bytes, you just do malloc(1024).

    As for your pointer-to-pointer, you need to worry about the size of the pointed-to object. You have a char**, which means you're pointing to a char*. So if you want to allocate, say, 40 of these, you need to allocate 40 bytes * sizeof(char*). Generally, an allocation looks like:
    Code:
    char **p = malloc(number_of_items * sizeof *p);
    When you fix this, note that you're not actually copying arrays, but pointers. When you say fileStrings[j]=buffer; you're just saving the value of buffer (which is an address) into fileStrings[j].

    What you probably want to do is allocate a new char* each time, then issue a copy, such as:
    Code:
    fileStrings[j] = malloc(strlen(buffer) + 1);
    strcpy(fileStrings[j], buffer);
    In real code, of course, you want to check to make sure malloc() doesn't return NULL.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    Brilliant! Thanks cas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying content of file into a 2D array
    By trueman1991 in forum C Programming
    Replies: 10
    Last Post: 12-16-2009, 12:42 AM
  2. Copying Array to Vector Question
    By bengreenwood in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2009, 03:00 PM
  3. Character Array copying
    By Drainy in forum C Programming
    Replies: 4
    Last Post: 03-15-2005, 10:43 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Problem Copying char array
    By NullStyle in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2004, 08:06 AM