array of structs problem.

This is a discussion on array of structs problem. within the C Programming forums, part of the General Programming Boards category; Hi, this is a homework problem where main is being run by another program. We need to make a series ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    3

    array of structs problem.

    Hi, this is a homework problem where main is being run by another program. We need to make a series of functions for main to use. It takes information that a bank statement would have and basically generates that statement using a series of functions. In the following two functions, entries are added to an array of structs containing all the information, then the arrays are printed in the second function. The problem is that I'm not saving to memory. That or I'm not accessing it correctly. I'm lost on which it is and how to address it. Any help would be great, I'm very new to this and very lost! Thanks!


    here is my global information
    Code:
    #include <stdio.h>
    #include "banklog.h"
    #define ph 10
    #define max 1024
    
    typedef struct banklog{
    double amount;
    const char * category[ph];
    const char * date[ph];
    const char * description;
    } banklog;
    int at = 0;//placeholder for the current struct




    here is my function to add new entries
    Code:
    /*
     Add a record to the bank log with the specified information.
     Return value:
     0 -- successfully added
     -1 -- unable to allocate space for new entry
     */
    int BANKLOGadd(double amount, const char* category, const char* date, const char* description)
    {
    struct banklog banklog[max];
      if (amount!=0 && category != 0 && date != 0 && description != 0)
    {
    banklog[
            amount, 
            *category, 
            *date, 
            *description
            ];
    at++;    
    return 0;
    }
        return -1;
    }



    Here is the function to print the list.
    Code:
    void BANKLOGprint()
    {
    struct banklog banklog[ph];
    int i= 0;
    for (i=0; i<at; i++)
    {
     printf("%8.2lf ",banklog[i].amount);
     printf("%s ",banklog[i].category);
     printf("%.*s ",  banklog[i].date+2-1);
     printf("%.20s\n", banklog[i].description);
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    There is a lot wrong here:

    Code:
    typedef struct banklog{
    double amount;
    const char * category[ph];
    const char * date[ph];
    const char * description;
    } banklog;
    1. Here, category and date are 10 element arrays of pointers to char strings, which don't seem to be getting allocated in the code you present. All you have is an array of uninitialized pointers. I think you meant:

    [code]
    Code:
    typedef struct banklog{
    double amount;
    const char category[ph];
    const char date[ph];
    const char description[ph];
    } banklog;
    2. You're typedefing banklog to a data type named banklog, then later you declare structs of banklog named banklog. You might want to use different names so you can keep track of what is what.

    Code:
    int BANKLOGadd(double amount, const char* category, const char* date, const char* description)
    {
    struct banklog banklog[max];
      if (amount!=0 && category != 0 && date != 0 && description != 0)
    {
    banklog[
            amount,
            *category,
            *date,
            *description
            ];
    at++;   
    return 0;
    }
        return -1;
    }
    1. I'm not quite sure you understand how to index an array or perhaps how to copy data. The '[' and ']' brackets are element access only, they don't provide a means of copying/moving data, only access.

    Code:
    banklog[
            amount,
            *category,
            *date,
            *description
            ];
    This does not copy data and it should give you a compile error. You want to index like such:

    Code:
    banklog[ at ].amount = amount;
    strcpy( &banklog[ at ].category, category );
    
    etc, etc, etc.

    And again, you have pointers to char strings (category and date), but these currently point to unallocated memory.


    2. You're declaring your structs within the function, giving them very limited scope within your app(to that function only) . Once the function returns, you lose that data. You'll probably want to pass the address of the array element you want to change and use strcpy to copy string data.
    Last edited by Cynic; 04-15-2012 at 05:02 PM.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    3
    Thanks so much for the response!!! It's really appreciated!!
    Ok, unfortunately, I really don't know what I'm doing, this is my first semester of C and I've never done any other programming. So here are the changes that I got from that.

    First, global. I added the strings library, I took out the pointers.
    Code:
    #include <strings.h>
    #include <stdio.h>
    #include "banklog.h"
    #define ph 10
    #define max 1024
    
    typedef struct banklog{
    double amount;
    const char  category[ph];
    const char  date[ph];
    const char  description;
    } bl;
    int at = 0;//placeholder for the current struct




    Here is the add function, I switched out the brackets, it's not actually working, but is this closer to what I should use to get this stored to memory? This is what I had to begin with but couldn't figure out where I was going wrong and switched it out for the brackets.
    Code:
    /*
     Add a record to the bank log with the specified information.
     Return value:
     0 -- successfully added
     -1 -- unable to allocate space for new entry
     */
    int BANKLOGadd(double amount, const char* category, const char* date, const char* description)
    {
    struct banklog b[max];
      if (amount!=0 && category != 0 && date != 0 && description != 0)
    {
        b[at].amount=amount;
        strcpy (&b[at].category, category);
        strcpy (&b[at].date, date);
        strcpy(&b[at].description,description);
    
    
        printf("Implement BANKLOGadd: %.2lf %8s %8s  s\n",amount, category,  date,  description);
    at++;    
    return 0;
    }
        return -1;
    }


    print basically stays the same here.
    Code:
    void BANKLOGprint()
    {
    struct banklog log[max];
    //    printf("Implement BANKLOGprint!\n");
    int i=0;
    for (i=i; i<at; i++)
    {
        printf("%8.2lf ",log[i].amount);
        printf("%s ",log[i].category);
     printf("%.*s ",  log[i].date+2-1);
        printf("%.20s\n", log[i].description);
    }
    }

    I don't understand why, if it's a global, I can't access it directly in the functions. I know it's not working, but i don't totally get why.

    Code:
        bl[at].amount=amount;
        strcpy (bl[at].category, category);
        strcpy (bl[at].date, date);
        strcpy(bl[at].description,description);

    Thanks again for the help!

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by Jill Weinright View Post
    I don't understand why, if it's a global, I can't access it directly in the functions. I know it's not working, but i don't totally get why.
    I think you're getting tripped up on the difference between a definition and a declaration. The definition gives the layout of the structure, but does NOT create any variables of that definition. It's simply the structure of the structure. A declaration declares one or more variables of that definition. So, your structure at the top is a definition, while the structures within the functions are declarations.

    at is the only global declaration so far.

    Code:
    typedef struct banklog{
    double amount;
    const char  category[ph];
    const char  date[ph];
    const char  description;
    } bl;
    What is description? As is, it will hold only one character. If you need it to hold more, you'll need to declare an array, like so:

    const char description[32];

    Lastly, if you want a global array of banklog structures, you'll need to declare an array after your definition:

    Code:
    typedef struct banklog{
    double amount;
    const char  category[ph];
    const char  date[ph];
    const char  description;
    } bl;
    
    bl banklg[max];
    And remove the declarations within the functions.
    Last edited by Cynic; 04-15-2012 at 06:37 PM.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    3
    That is EXACTLY what I needed to understand!! You're awesome!! this is working now!! Thank you so much!!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-20-2011, 08:43 PM
  2. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  3. problem with printf in array of structs...
    By Huskar in forum C Programming
    Replies: 2
    Last Post: 03-29-2009, 10:13 PM
  4. Array of structs Problem
    By I BLcK I in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2008, 12:20 AM
  5. Problem with array of structs
    By Wiretron in forum C Programming
    Replies: 3
    Last Post: 09-03-2006, 02:07 PM

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