Thread: Scope of a data stucture

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    1

    Scope of a data stucture

    I'm just learning C and I can't work out how to solve this undeclared error when compiling this code. I assume it has something to do with scope. Any help / explanation would be much appreciated

    C code - 47 lines - codepad


    Code:
    #include <stdio.h>
    
    
    int main(void) {
    
    
            struct bank_account {
                    int accountNumber;
                    char * firstName;
                    char * lastName;
                    float balance;
                    char * password;
            };
    
    
    
    
    
    
            struct bank_account account_123;
            account_123.accountNumber = 123;
            account_123.firstName = "Oliver";
            account_123.lastName = "Hough";
            account_123.balance = 345.69;
            account_123.password = "f4kep455w0rd";
    
    
    
    
    
    
    
    
            display_account();
    
    
    
    
            printf("\n");
            return 0;
    }
    
    
    
    
    
    
    int display_account(void) {
    
    
            printf("--------------------------------------------------\n");
            printf("                Bank Account Details              \n");
            printf("--------------------------------------------------\n");
            printf(" Account Number: %d\n", account_123.accountNumber);
            printf(" Initials: %c %c\n", account_123.firstName[0], account_123.lastName[0]);
            printf(" First Name: %s\n", account_123.firstName);
            printf(" Last Name: %s\n", account_123.lastName);
            printf(" Balance: €%.2f\n", account_123.balance);
            printf(" Second character of Password: %c\n", account_123.password[1]);
            printf(" Fourth character of Password: %c\n", account_123.password[3]);
            printf(" Seventh character of Password: %c\n", account_123.password[6]);
    
    
            return 0;
    }

    Thanks.
    Last edited by Oliver Hough; 10-09-2013 at 05:30 AM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You need to pass your struct to your function display. Unless you create global bank accounts then you can refer to them the way you are already.

    EDIT: You also should define the struct outside of main (just above it). Add a prototype to your function display_account (also just above main).
    Last edited by camel-man; 10-09-2013 at 06:02 AM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's the way to do it, as camel-man suggests:

    *define the struct above main(), so the definition is global. That makes it very easy to create a temp struct in any function.

    *declare the actual struct instance in main(), and then pass it to the various functions. That cuts out errors dealing with your data being global.

    Best way to go with structs, imo.

  4. #4
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    or the struct in display function,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stucture file defined
    By dorato in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2013, 11:30 AM
  2. Replies: 2
    Last Post: 11-14-2011, 05:18 PM
  3. Pointer to stucture?
    By traxy in forum C Programming
    Replies: 5
    Last Post: 09-16-2008, 07:40 PM
  4. Sorting within a stucture
    By Zeff in forum C Programming
    Replies: 11
    Last Post: 11-29-2006, 12:00 AM