Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

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