2 Q.

This is a discussion on 2 Q. within the C++ Programming forums, part of the General Programming Boards category; Hi , My name is max, m a student studying programming, well other day i got an assignment ,in which ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    6

    Post 2 Q.

    Hi ,


    My name is max, m a student studying programming, well other day i got an assignment ,in which i could'nt get 2 q. ,one of them was Wht is dynamic initialization of variables? ,well i know abt static variables , i searched on cplusplus.com also , but could'nt find the dynamic variables , also i need a programme in c++ to sort an array using pointers , does dynamic variables simply means declaration of variables locally as it is done in c++ ?and the code for my programme is like i have reached tilll i ask for an array to be sorted ,then i store it in a pointer ,the problem is how do i sort an array in a pointer ,pls xplain with the code if u please.


    P>S>.

    when i say :
    void main(int a, intb)


    like just the fact tht i have declared a,b, to b of int type when i use it ,and not in the beginning like we do in C ,makes these variable dynamic ?
    Last edited by vyaskartik2001; 03-01-2004 at 01:07 PM.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    Why not show us the code you have and then ask about errors/other things you find difficult????

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb new, delete, pointer arithmitic?

    The new keyword allows you to create variables/objects dynamically during runtime. The delete keyword frees-up the memory that was allocated by new.

    I'm not sure if this is what is meant by dynamic initialization. Usually, "initialize" means to give an initial (starting value) , or default value.
    Code:
    int x;  // Declare x, don't initialize
    int y = 0; // Delcare and initialize y
    x = 5;  // Initialize x=5
    As for pointers and arrays, the assignment might be an exercise using pointer arithmetic. Hint: if you incriment a pointer to an element in an array, the pointer will now point-to the next element in the array.

    The other possibility is an array of pointers. For example, you would make the first pointer (in the pointer arrray) point-to the smallest value in the data array (not to the first element in the array). When you are done with the sort, and you sequence through the pointer array, printing the pointed-to data, the results will print in the sorted order, even though the original data array is still unsorted. Sorry, I'm not explaining that very well...

    I hope this helps you get started.

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    DYNAMIC INITIALIZATION OF VARIABLES

    From my understanding the dynamic initialization of variables referes to the expression used in the assignment. If the expression used cannot be computed until runtime then it is a dynamic initializtion.

    Code:
    int function(void)
    { 
    return random_number;
    }
    
    
    int i=function();
    since the compiler doens't know what value the funciton will return until the program is actually run this would be an example of a dynamic initialization.

Popular pages Recent additions subscribe to a feed

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