output of struct? confused....

This is a discussion on output of struct? confused.... within the C Programming forums, part of the General Programming Boards category; The output makes no sense to me, am i recording the info into the struct wrong? i changed from struct ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    104

    output of struct? confused....

    The output makes no sense to me, am i recording the info into the struct wrong?

    i changed from struct customer makeCustomer to void; the one that returns a customer outputs values just as weird, with no name output, time is 0, and items is a huge number

    Code:
    struct customer {
       char name[30];
       int items;
       int time;
    };
    
    void makeCustomer(struct customer a,char n[30], int i, int t) {
       strcpy(a.name,n);    
       a.items=i;
       a.time=t;
    }
    
    int main() {
       struct customer test;
       char b[30];
       b[0]='j';
       b[1]='a';
       b[2]='s';
       int items=5;
       int time=60;
       makeCustomer(test,b,items,time);
       printf("Name: %s--Items: %d--Time: %d\n",test.name,test.items,test.time);
       system("pause"); 
    }
    
    OUTPUT: Name: H*b--Items: 236--Time: 4602016

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Quote Originally Posted by Soulzityr
    The output makes no sense to me, am i recording the info into the struct wrong?
    In a way. The problem is that makeCustomer() operates on a copy of the object passed to it. You should pass the address of the object that you wish makeCustomer() to operate on, i.e., a should be a pointer to struct customer (and you should use a name that is more descriptive than a). Of course, you need to change the implementation of the function accordingly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    104
    so i have to make everything pointers, including in main????

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Quote Originally Posted by Soulzityr
    so i have to make everything pointers, including in main?
    No. Recall the use of the address-of operator, e.g., &test results in a pointer to test.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    104
    Code:
    void makeCustomer(struct customer &a,char n[30], int i, int t) {
       strcpy(a.name,n);    
       a.items=i;
       a.time=t;
    }
    Heh my output now works, thanks ois this what you meant though?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Eh, no. That is the use of a reference parameter in C++, but you are writing a C program. I suggest that you check to make sure that you are compiling as C, otherwise you could get very confused.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    104
    oh you are right, i had it saved as cpp file, and now youre right, compile says its an error :P

    hmm how do i access address for function then with the & or w/e >.<

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    You should do something like this:
    Code:
    void makeCustomer(struct customer *a, char n[], int i, int t) {
       strcpy(a->name, n);
       a->items = i;
       a->time = t;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    104
    ugh sorry i never learned this well at all; so i made my method look at yours and i tried various ways of calling it

    havnig

    struct customer* test2

    and then makeCustomer(test2...) crashes the program

    struct customer test2; then makeCustomer(test2...) doesnt run cuz of argument error which makes sense

    then struct customer test2 and makeCustomer(*test2...) also fails. sorry haha i am not quite sure on how to write the syntax >.<

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Recall my previous post about using the address-of operator:
    Code:
    makeCustomer(&test, b, items, time);
    By the way, time is not a good variable name to use since it can conflict with a function named time from <time.h>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    104
    ahh hmm i will change it then, thanks as for what you said before about address-of operators, it sort of passes over my head; im sorry >.< my understanding of it is kinda fuzzy......and what it means in terms of when icall it

    EDIT: hmm nvm...i think i got it...going to test the output

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    104
    heheh sorry thanks for helping me with that it works now

    i tried the same thing with my enqueue method but it doesnt work. is it okay if i ask you for help with that? >.<

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  3. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  4. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-02-2007, 12:48 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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