Thread: problem getting structure to work in my function

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    37

    problem getting structure to work in my function

    Hello folks,

    I hope my question is not wasting anyones time here. I was trying to make a program that will assist me in my daily work, the idea being to automatically generate a file that contains some inforamation about products and so forth.


    I decided to put this information into a structure and then create a file and read the contents of struct to it.

    I can get the program to work when I do not use a seperate function, but i cannot seem to get the program to work while having a seperate function. My knowledge of passing structures seems to be a bit limited.

    here is the function:
    Code:
    void createFile(char pc, struct CALL){
    FILE *nf;
    
         nf = fopen(pc, "w");
    
         if (nf == NULL) {
         fprintf("Can't open created file %s!\n", pc);
         exit(1);
         }
    
       if (nf != NULL) {
        fwrite(&c, 1, sizeof(CALL), nf);
        fclose(nf);
        }
    
    }
    function prototype:
    Code:
    void createFile(char filename, struct CALL);
    and the call to the function:

    Code:
    createFile(pc,c);
    I really tried lots of different attempts and looked at many websites and resource but i still get the same problems. Can someone point out where I went wrong...sure there are numerous errors.


    Anyway this is the program that works without function(so you know that i am trying )

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<string.h>
    
    char *genCallNr();
    //void createFile(char *callnr);
    
    struct CALL {
       int qty;
       char contract[5], owner[5], partnr[6], date1[14], date2[14];
       char *callnr;
    };
    
    int main(void)
    {
     struct CALL c;
    
     strcpy(c.contract, "DELL");
     strcpy(c.owner, "EMEA");
     strcpy(c.partnr, "615TW");
     strcpy(c.date1, "030320091148");
     strcpy(c.date2, "030320091148");
     c.callnr = genCallNr();
     printf("\n%s", c.callnr);
    
    free (c.callnr);
    
    printf("\n%s\t%s\t%s\t%s\n", c.contract, c.owner, c.partnr, c.callnr);
    //createFile(c.callnr);
    int i;
    FILE *nf;
    char *pc;
    char cn[12];
    pc = cn;
    
    pc = genCallNr();
    
    printf("\n%s", pc);
    
     nf = fopen(pc, "w");
    
         if (nf == NULL) {
         fprintf("Can't open created file %s!\n", pc);
         exit(1);
         }
    
       if (nf != NULL)
    fwrite(&c, 1, sizeof(struct CALL), nf);
    
        fclose(nf);
    
    
    
    }
    
    char *genCallNr(){
    
    char *callnr = malloc(12);
    time_t now;
    
     struct tm *ts;
     char buf[12];
    
     now = time(NULL);
     ts = localtime(&now);
    
     strftime(buf, sizeof(buf), "1%y%m%d%H%M", ts);
     //printf("\n%s", buf);
     strncpy(callnr, buf,12);
    // printf("\n%s", callnr);
    
    return callnr;
    
    
    }
    
    /*
    void createFile(char *callnr){
    FILE *nf;
    
         nf = fopen(callnr, "w");
    
         if (nf == NULL) {
         fprintf("Can't open created file %s!\n", callnr);
         exit(1);
         }
    
       if (nf != NULL) {
        fwrite(callnr, 1, sizeof(callnr), nf);
        fclose(nf);
        }
    
    }
    
    */
    there are problems with the information that is writen into the file by the above program but i will tackle them when the problem with the function is resolved...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is customary to give struct variables a name. struct CALL is just the type.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    You mean in my function prototype and function have : struct CALL c instead of struct call:

    Code:
    void createFile(char filename, struct CALL c);
    is the function call :

    Code:
     
    createFile(pc,c);
    that ok?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that should work, I expect.

    It would help if you posted any error messages from the compiler (if you get hundreds, then post the first few - usually when you get hundreds of errors, it's best to fix the first one or two, then recompile - repeat until no errors).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    In your function, you're passing in a copy of the struct, and the function changes the copy, not the original where the function call happens. Your function needs to take a CALL* and operate on that, and when you call the function pass in the address of c (&c).

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    struct_callsFUNC.c: In function \u2018main\u2019:
    struct_callsFUNC.c:56: warning: passing argument 1 of \u2018createFile\u2019 makes integer from pointer without a cast
    struct_callsFUNC.c:56: error: type of formal parameter 2 is incomplete


    line 56 is:

    Code:
    createFile(pc,c);
    function and prototype looks like this:
    Code:
    void createFile(char filename, struct CALL c);
    with the structure looking like this:
    Code:
    struct CALL {
       int qty;
       char contract[5], owner[5], partnr[6], date1[14], date2[14];
       char *callnr;
    };
    
    
    int main(void){
    
     struct CALL c;
    
    ...

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    also I get an error here, when trying to write to the file:

    Code:
       if (nf != NULL) {
        fwrite(&c, 1, sizeof(CALL), nf);
        fclose(nf);
        }
    when trying to read the structure to the file
    struct_callsFUNC.c:92: error: \u2018CALL\u2019 undeclared (first use in this function)
    struct_callsFUNC.c:92: error: (Each undeclared identifier is reported only once
    struct_callsFUNC.c:92: error: for each function it appears in.)

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The second error message is because you are missing struct - you are trying to take the size of something called "CALL", which doesn't exist. You have something called struct CALL.

    The error at line 56 is because you have "char filename" when you PROBABLY want to have a filename string - so either const char * filename or const char filename[] (const added since I expect you are not planning to actually modify the filename).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You only know "struct call". Use

    Code:
    typedef struct CALL { ... } CALL;
    EDIT: You were faster

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    a quick update with the errors I am having problem solving:

    qa@qa:~/Programming$ gcc -o struct_callsFUNC struct_callsFUNC.c
    struct_callsFUNC.c:7: warning: \u2018struct CALL\u2019 declared inside parameter list
    struct_callsFUNC.c:7: warning: its scope is only this definition or declaration, which is probably not what you want
    struct_callsFUNC.c: In function \u2018main\u2019:
    struct_callsFUNC.c:55: error: type of formal parameter 2 is incomplete
    struct_callsFUNC.c: At top level:
    struct_callsFUNC.c:80: error: conflicting types for \u2018createFile\u2019
    struct_callsFUNC.c:7: error: previous declaration of \u2018createFile\u2019 was here
    struct_callsFUNC.c: In function \u2018createFile\u2019:
    struct_callsFUNC.c:86: warning: passing argument 1 of \u2018fprintf\u2019 from incompatible pointer type

    and here is the code:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<string.h>
    
    char *genCallNr();
    void createFile(const char *Ppc, struct CALL c);
    
    struct CALL {
       int qty;
       char contract[5], owner[5], partnr[6], date1[14], date2[14];
       char *callnr;
    }CALL;
    
    
    int main(void){
    
     struct CALL c;
    
     strcpy(c.contract, "DELL");
     strcpy(c.owner, "EMEA");
     strcpy(c.partnr, "615TW");
     strcpy(c.date1, "030320091148");
     strcpy(c.date2, "030320091148");
     c.callnr = genCallNr();
     printf("\n%s", c.callnr);
    
    free (c.callnr);
    
    printf("\n%s\t%s\t%s\t%s\n", c.contract, c.owner, c.partnr, c.callnr);
    //createFile(c.callnr);
    char *pc;
    char cn[12];
    pc = cn;
    
    pc = genCallNr();
    char *Ppc ;
    Ppc = pc;
    printf("\n%s", Ppc);
    
    createFile(pc, c);
    
    }
    
    char *genCallNr(){
    
    char *callnr = malloc(12);
    time_t now;
    
     struct tm *ts;
     char buf[12];
    
     now = time(NULL);
     ts = localtime(&now);
    
     strftime(buf, sizeof(buf), "1%y%m%d%H%M", ts);
     //printf("\n%s", buf);
     strncpy(callnr, buf,12);
    // printf("\n%s", callnr);
    
    return callnr;
    
    }
    
    
    void createFile(const char *Ppc, struct CALL c){
    FILE *nf;
    
         nf = fopen(Ppc, "w");
    
         if (nf == NULL) {
         fprintf("Can't open created file %s!\n", Ppc);
         exit(1);
         }
    
       if (nf != NULL) {
        fwrite(&c, 1, sizeof(CALL), nf);
        fclose(nf);
        }
    
    }
    maybe I misunderstood both of your comments?

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Tom Bombadil View Post
    a quick update with the errors I am having problem solving:

    qa@qa:~/Programming$ gcc -o struct_callsFUNC struct_callsFUNC.c
    struct_callsFUNC.c:7: warning: \u2018struct CALL\u2019 declared inside parameter list
    struct_callsFUNC.c:7: warning: its scope is only this definition or declaration, which is probably not what you want
    This one is because of this:

    Code:
    char *genCallNr();
    void createFile(const char *Ppc, struct CALL c);
    
    struct CALL {
    Define your structs before your function prototypes. The error is slightly confusing because (I think) the compiler is saying it has not seen this struct yet, and so is regarding the reference in the function prototype parameters as an attempted definition.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to tell the compiler what struct CALL looks like FIRST, then declare the function that takes it as a parameter.

    I'm not sure if that clears ALL errors, but certainly more than half of them would be fixed by ordering things correctly.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    I am blind!

    sorry matsp, i completely missed that and thanks also MK27 and Brafil

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Everything works now. I have learnt my lesson the declare the function prototypes AFTER the structure if they are calling them...

    When I run the program I get this:

    ^@^@^@^@DELL^@EMEA^@615TW^@030320091148^@K03032009 1148^@^H^H^D^H

    Why is this?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current program?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM