Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all, we have some important news to share. Please follow the link here to find out more!

https://www.boards.ie/discussion/2058419143/important-news/p1?new=1

structure problem (C)

  • 08-04-2003 04:34PM
    #1
    Closed Accounts Posts: 3,322 ✭✭✭


    I'm writing a program to use pointers to access structure elements.. When I run this code I get the error "STRUCT-Q2 caused an invalid page fault in module MSVCRT.DLL at 015f:780268b2."
    And in FreeBSD I get "Segmentation fault (core dumped)", after I enter a value for 'a' for the second time - anyone know what the problem is? Thx in advance for any help

    Here is the code:
    #include <stdio.h>
    #include <stdlib.h>
    main( int argc, char *argv[] )
    {
            struct data
            {
                    int a;
                    int b;
            };
            
            typedef struct data DATA;
            DATA d;
            DATA *p;
            p = &d;
    
            printf ("Assign number to a: ");
            scanf ("%d", &d.a);
            printf ("Assign number to b: ");
            scanf ("%d", &d.b);
            printf ("a is %d and b is %d \n \n", d.a, d.b);
    
            printf ("Assign number to a: ");
            scanf ("%d", p -> a);
            printf ("Assign number to b: ");
            scanf ("%d", p -> b);
            printf ("c is %d and d is %d \n \n", d.a ,d.b);
    
            printf ("Assign number to a: ");
            scanf ("%d", (*p).a);
            printf ("Assign number to b: ");
            scanf ("%d", (*p).b);
            printf ("a is %d and b is %d \n \n", d.a, d.b);
    
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    me wrong ..


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    printf ("Assign number to a: ");
    scanf ("%d", &d.a);
    printf ("Assign number to b: ");
    scanf ("%d", &d.b);
    printf ("a is %d and b is %d \n \n", d.a, d.b);

    printf ("Assign number to a: ");
    scanf ("%d", &p->a);
    printf ("Assign number to b: ");
    scanf ("%d", &p->b);
    printf ("c is %d and d is %d \n \n", d.a ,d.b);

    printf ("Assign number to a: ");
    scanf ("%d", &(*p).a);
    printf ("Assign number to b: ");
    scanf ("%d", &(*p).b);
    printf ("a is %d and b is %d \n \n", d.a, d.b);

    You Need to put the & operator in the scanf function.

    you have only put it in the first time you got some data from the screen.


  • Closed Accounts Posts: 3,322 ✭✭✭Repli


    D'oh


  • Registered Users, Registered Users 2 Posts: 2,281 ✭✭✭DeadBankClerk


    [php]
    printf ("Assign number to a: ");
    scanf ("%d", p).a);
    printf ("Assign number to b: ");
    scanf ("%d", p.b);
    printf ("a is %d and b is %d \n \n", d.a, d.b);
    [/php]

    &(*p) cancels out to just p.


Advertisement