Thread: Variables

  1. #1
    Unregistered
    Guest

    Variables

    How do I decalre variables as they are needed? For instance, a user says he wants to store x numbers, how would i go about doing that?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One way is to use dynamic memory allocation. For example:

    Code:
       int x;
       cout << "How many number do you want to store? ";
       cin >> x;
       int *numbers;
       numbers = new int[x];
       for (i=0; i<x; i++)
          numbers[i] = i;
       for (i=0; i<x; i++)
          printf("numbers[%d]:%d\n",i,numbers[i]);
       delete []numbers;
    You can declare and allocate the array in one step like this, but I prefer the above method:
    int *numbers = new int[x];

    Make sure to free the memory once you're done using it with the delete statement.

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    you could also use malloc / free... with these you can use user-defined type casting, i'm not sure if you can do this with new / delete...
    hasafraggin shizigishin oppashigger...

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    Not sure?
    New / delete are much better in this area than malloc, because you don't even have to case.
    simply use:

    class MyClass {
    ...
    }

    int x = GetCount();
    MyClass* list = new MyClass[x];

    // do what is needed...

    delete [] list;

  5. #5
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    i see... so the [type] field doesn't necessarily need to be a C/++ keyword for a type... niice... hmm... but what are other differences? i tend to stray away from using operators and syntax that is different from what i've normally used... thanks...
    hasafraggin shizigishin oppashigger...

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
       int x;
       cout << "How many number do you want to store? ";
       cin >> x;
       int *numbers;
       numbers = new int[x];
       for (i=0; i<x; i++)
          numbers[i] = i;
       for (i=0; i<x; i++)
          printf("numbers[%d]:%d\n",i,numbers[i]);
       delete []numbers;
    This bit of C is very interesting.. someone mind explaining it to me? I've always been used to the C++ shortcut.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    this code is actually C++ - it uses cin, cout, new and delete.
    The only thing "C" about it is printf.
    lookup printf in your documentation.

    what this lines mean is to print a string. the first parameter is the string format. every % indicates a parameter to be inserted to the string with it's type - %d stands for decimal integer.
    So the first %d will be replaced with the first argument - i, and the second %d will be replaced with numbers[i]

    HTH

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  4. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  5. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM