Thread: Structs with pointer

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    52

    Structs with pointer

    Hi guys, I'm new around here. I'm currently doing my internship, and work daily with C. My question is, why is code below crashing?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct bpf_insn {
    	short	code;
    	char 	jt;
    	char 	jf;
        int      k;
    };
    
    
    struct bpf_program {
        int en;
    	struct bpf_insn *bf_insn;
    };
    
    
    int main()
    {
    
        struct bpf_insn *bf_insn;
        bf_insn->code   =12;
        bf_insn->jt     =11;
        bf_insn->jf     =10;
        bf_insn->k      =9;
    
        printf("bf_insn->jt is %d",bf_insn->jt);
        struct bpf_program *program;
    
        return 0;
    }
    The error apears when I make a pointer of type bpf_program.
    a)why is this?
    b)How can i fix this?

    All what I want, is a pointer from type bpf_program, wich directs me to my counter and my pointer(wich point to the data)(this is the other struct)

    Thanks,

    Libpgeak

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Code:
    struct bpf_insn *bf_insn;
    This just means that you have a pointer to a bpf_insn. There is no bpf_insn yet. The pointer is dangling around pointing a nothing in particular.

    1. Initialize your pointer to NULL. That won't fix your problem, but at least it's pointing to a defined value, not at random garbage.
    2. look up malloc or calloc to actually get a real bpf_insn to point to.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Well, i tested some more and i found out that:

    -struct bpf_program program; gives no error
    struct bpf_program *program; gives the error.

    At this point, i realy don't get it...

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Libpgeak View Post
    Well, i tested some more and i found out that:

    -struct bpf_program program; gives no error
    struct bpf_program *program; gives the error.

    At this point, i realy don't get it...
    Simply having a pointer to struct bpf_program ain't gonna cut it, because the pointer has to hold the address of an object of that type else it contains garbage. So if you insist on using a pointer, then you'd need to create an object of that type too.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Libpgeak View Post
    Well, i tested some more and i found out that:

    -struct bpf_program program; gives no error
    struct bpf_program *program; gives the error.

    At this point, i realy don't get it...
    I'm assuming that "-" is a typo.

    The first one is a struct bpf_program. But you can't use indirect notation with that (->), you would use "bf_insn.code", et al.

    The second one is a pointer to a struct bpf_program, but as nvoigt points out, the struct it points to does not exist, so when you try to assign it values, you have a problem.

    Code:
    struct bpf_program *program = malloc(sizeof(struct bpf_program));
    Presuming malloc() succeeded, "program" is a pointer to a struct bpf_program that actually has an appropriate amount of memory assigned to it, and so you can put values there.

    You should get a beginner C book and read the first half a dozen chapters or something, it will make your life much easier.
    Last edited by MK27; 09-07-2011 at 11:59 AM.
    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

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    India
    Posts
    27
    @LIPGEAK
    In the main() function
    Code:
    struct bpf_insn *bf_insn;
    This is just a pointer to a structure (It can hold address of a structure), but no actual structure has been created by you yet.
    The first thing you need to do is:
    - Make a structure (i.e. Memory must be allocated for the structure), which can be done in the following ways:

    - Using a dynamic memory allocation by using malloc() function
    - Use a statement
    Code:
    struct bpf_insn x;
    Now x itself is a structure.
    Now either you can use x to access the members of the structure using the dot (.) operator
    eg: x.code=12;
    Or either you can now make ur pointer which u defines point to this structure and then use indirection (->) operator.
    eg:x->code=12;

    Hope this helps
    Regards
    Sumit


    Quote Originally Posted by Libpgeak View Post
    Hi guys, I'm new around here. I'm currently doing my internship, and work daily with C. My question is, why is code below crashing?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct bpf_insn {
    	short	code;
    	char 	jt;
    	char 	jf;
        int      k;
    };
    
    
    struct bpf_program {
        int en;
    	struct bpf_insn *bf_insn;
    };
    
    
    int main()
    {
    
        struct bpf_insn *bf_insn;
        bf_insn->code   =12;
        bf_insn->jt     =11;
        bf_insn->jf     =10;
        bf_insn->k      =9;
    
        printf("bf_insn->jt is %d",bf_insn->jt);
        struct bpf_program *program;
    
        return 0;
    }
    The error apears when I make a pointer of type bpf_program.
    a)why is this?
    b)How can i fix this?

    All what I want, is a pointer from type bpf_program, wich directs me to my counter and my pointer(wich point to the data)(this is the other struct)

    Thanks,

    Libpgeak

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Tnx guys! sumit180288 you were right, the error was in the first struct. Thnx!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exercise with pointer and structs
    By xphoenix in forum C Programming
    Replies: 4
    Last Post: 06-29-2010, 09:46 AM
  2. Help free structs and pointer.
    By TaiL in forum C Programming
    Replies: 37
    Last Post: 10-04-2008, 08:52 PM
  3. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  4. pointer probs. with structs.
    By dinjas in forum C Programming
    Replies: 6
    Last Post: 03-08-2005, 05:59 PM
  5. Pointer to Array of Structs
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 08:34 AM

Tags for this Thread