hi! explain plz..., difference in declaring struct pointers outside functions...,

This is a discussion on hi! explain plz..., difference in declaring struct pointers outside functions..., within the C++ Programming forums, part of the General Programming Boards category; and inside functions... um, i want to know why the program crash and the reason for this..., here.., PART 1 ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    hi! explain plz..., difference in declaring struct pointers outside functions...,

    and inside functions... um, i want to know why the program crash and the reason for this...,

    here..,

    PART 1

    SOMESTRUCT* object; // this is declared outside a function,

    function()
    {
    object.ManipulateMemberVariables(); // this will crash the program
    }

    PART 2

    function()
    {
    SOMESTRUCT* object;
    object.ManipulateMemberVariable(); // this will not crash..,
    }

    thanks!!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you put it outside the function (and outside main) the pointer is global to the entire program.

    If you put it inside the function, you can only access it from there (not outside the function).

    And you have only declared a pointer to your object, you haven't allocated any memory for it. That might be why it crashes, since it points to a random place in memory and 'think' that it is an object, but it actually is an integer or something .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 02:59 PM
  2. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  4. 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
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 09:33 PM

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