Thread: exercise with pointer and structs

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    46

    exercise with pointer and structs

    Hi i'm trying to understand many things bout structs and pointer... i've made a little file that should write a file with random access calling in the main a function to inizialize the file

    help me, think i'm doing errors in the parameter passage ( if i use the program without functions, it works )

    Code:
        #include <stdio.h>
        #include <stdlib.h>
        
        void initialize(fPtr , user x);
    
        struct user {
        int id;
        char lastName[20];
        char firstName[15];
        double balance;
        };
        
        int main(){
        
        FILE *filePtr;
        
        struct user defValue = { 0, "john", "doe", 0 };
         
         initialize(filePtr,defValue); 
        
                
        return 0;
        }
        
        /* init the file with default value */
        void initialize(fPtr, x){       
            FILE *filePtr = fPtr;
            struct user = x;     
                if ((filePtr = fopen("users.dat","wb")) == NULL ){
                    printf("Cannot open users.dat file");
                }
                else {
                 int i;   
                 for(i = 1;i<=100;i++) {
                    fwrite(&defValue,sizeof(struct user),1,filePtr);
                    }   
                    fclose(filePtr);
                }            
            }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > void initialize(fPtr , user x);
    1. This should come after your struct.

    2. fPtr needs a type (say FILE*)
    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.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That will not even compile.

    Code:
        
    void initialize(fPtr , user x);
    "fPtr" is not a type, and actually neither is "user" -- right now you have a "struct user" (altho it is declared after the function prototype, so does not exist at this point).
    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

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    void initialize(FILE *fPtr, user x){

    is the correct way?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, if you actually try using the compiler it should tell you that is not correct (unless you are compiling as C++ and not C).

    Code:
    void initialize(FILE *fPtr, struct user x);
    This:
    Code:
        void initialize(fPtr, x){       
            FILE *filePtr = fPtr;
            struct user = x;     
    is unnecessary (and the red line is another compiler error) -- just refer to fPtr and x.

    Also, you might as well use a pointer for the struct too:
    Code:
    void initialize(FILE *fPtr, struct user *x); // prototype
    struct user defValue = { 0, "john", "doe", 0 };
    initialize(filePtr,&defValue);   // use "address of" operator &
    Now let me emphasis something: It is a BIG BIG mistake to be just writing code and not trying to compile it, which is obviously what you are doing because your code has various obvious (even, non-sensical) errors which will show up when you try and compile it. This is why it is a complete waste of time to write code without using the compiler every few lines. Otherwise, you end up making up your own syntax which resembles a C program, but it is not a C program, and is therefore probably not much good for anything.

    If you do not have access to a compiler where you are right now, just stop. Don't bother doing anything more until you do. It's a waste of time. Honestly.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  2. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 4
    Last Post: 01-02-2008, 11:20 AM
  3. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  4. Creating a pointer to an array of structs
    By Jonathan_ingram in forum C Programming
    Replies: 8
    Last Post: 12-31-2003, 08:49 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM