seg fault with nested struct from pointer

This is a discussion on seg fault with nested struct from pointer within the C Programming forums, part of the General Programming Boards category; I am having trouble accessing variables from a nested struct and pointer. When this program runs I get a seg ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    21

    seg fault with nested struct from pointer

    I am having trouble accessing variables from a nested struct and pointer. When this program runs I get a seg fault without anything before it. It doesn't even print out what's up.
    Can someone help me with these problems please?

    Thank you.

    Code:
    #include <stdio.h>
    
    struct s_struct_one {
      int variable_one;
    };
    
    struct s_struct_two {
      int variable_two;
      struct s_struct_one struct_one;
    };
    
    int main(int argc, char *argv[])
    {
      struct s_struct_two struct_two;
      struct s_struct_two *struct_two1;
      struct s_struct_one struct_one;
    
    printf("WHAT'S UP\n");
    
      printf("%d  ",struct_two.struct_one.variable_one = 20);
      printf("%d  ",struct_two.variable_two = 20);
      printf("%d\n",(*struct_two1).struct_one.variable_one = 20);
    
      return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your pointer doesn't actually point at anything specific. Just some random spot in memory. Get used to compiling with warnings on and it would have told you that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    omg
    I did it again.

    (--Wall ?)

    Thanks for finding my stupid errors for me. Gah!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I typically use:
    Code:
    gcc -o outputfile cfile0.c ... cfileN.c -Wall -pedantic

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 09:38 AM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 09:27 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

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