Problems adding info to an MDA?

This is a discussion on Problems adding info to an MDA? within the C Programming forums, part of the General Programming Boards category; Hi all, Still not getting my head around multidimentional arrays and was wondering if anyone would be so curtious as ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    Problems adding info to an MDA?

    Hi all,

    Still not getting my head around multidimentional arrays and was wondering if anyone would be so curtious as to explain what I am doing wrong and why it's wrong. I am trying to read data out of a file and put the information into individual arrays to be displayed on the screen. I also want access to any of the array elements individually.

    The code I have thus far:

    Code:
    /*
      Reads a single record from the currently open file into an array of
      strings.
    
      A single record is in the format of:
      field1,field2,field3,field4
    
      Returns ntokens.
    */
    int fileio(char **ptr, int fp)
    {
      char buffer[BUFSIZ];		  // Temp storage for single field.
      char **ptr_to_ptr = ptr;	// A pointer to the pointer address
    				             // passed to the function.
      int i, ntokens = 0;
    
      fseek(file, fp, SEEK_SET);    // Seeks to a point in file assigned by fp.
    
      /* Reads the record from currently open file into the array */
      if(fgets(buffer, sizeof buffer, file) != NULL)
      {
        char *sep = strtok(buffer, DELIM); // First call to strtok.
    
        while(sep != NULL)
        {
          strcpy(*ptr_to_ptr, sep);
          sep = strtok(NULL, DELIM); // Second call to strtok using NULL.
    
          ntokens += *ptr_to_ptr[i];  // increments ntokes length of i array.
          *ptr_to_ptr += MAXLEN;	 // increments the pointer the size of array.
          i++; num_elements++;	 // increments local variable i, and global
                       				 // variable num_elements.
        }
      }
    
      return ntokens;
    }
    This code works fine when called like this

    Code:
      *ptr_array = &array[0][0];
      fileio(ptr_array, fp);
    and I can display the whole array

    Code:
      for(i = 0; i < count; i++)
      {
        cursor(row, indent);         // Positions cursor on the screen.
        printf("%s", array[x++]);    // Prints contents of the array on the screen.
        indent += 8;                // Moves the cursor a set distance on the screen.
      }
    or pick out what I want

    Code:
    printf("%s", array[27]);
    (There is also an annoying 'Null pointer assignment' message displayed that I can't get rid of)

    But when I try to recall it like this

    Code:
      *ptr_array = &array[1][0];
      fileio(ptr_array, fp);
    it goes downhill fast all that is displayed is random garbage.

    I hope I have given you enough information to work with. Thanks to those who reply.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,552
    Well if array is something like
    char array[100][50];
    then it's badly broken.

    Can't say much more than that unless you tell us what array really is.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    I've first used

    #define MAXWORD 80
    #define MAXLEN 20

    then in main

    char array[MAXWORD][MAXLEN];

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,552
    Then the prototype for the function would be that as well
    int fileio(char array[MAXWORD][MAXLEN], int fp)

    And you would need to do something like
    strcpy( array[pos], token );
    pos++;

    to copy a token into the array.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    I've changed my code to represent the suggestion made by Salem but it seems to be overwriting what's in the array with new data each time the function (fileio) is called instead of appending the new data to the end of what's already there.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    Sorry guys disregard the last message of course I've forgotten to increment pos. The simplest mistakes are the stupidest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  2. Replies: 3
    Last Post: 07-24-2002, 08:46 AM
  3. problems adding
    By Klinerr1 in forum C++ Programming
    Replies: 3
    Last Post: 07-13-2002, 05:54 PM
  4. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM
  5. Replies: 3
    Last Post: 12-06-2001, 04:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21