Array initialization with int variable

This is a discussion on Array initialization with int variable within the C++ Programming forums, part of the General Programming Boards category; How can I initialize an index giving its size thru an int variable aiming the something like the above: int ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    Post Array initialization with int variable

    How can I initialize an index giving its size thru an int variable aiming the something like the above:

    int x=10;

    char test[x];


    Thank you,
    Thiago Santana

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    std::vector<T> v(size);
    But if it's just a string, use std::string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    Thanks Elysia,

    But quite not what I am looking for. I just want to inform the simple array size being the size an int variable. It has to be a very simple array like the one I have sample on my original post.

    Thank you,
    Thiago Santana

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Are you looking into making a dynamically sized array?
    For example, setting a size and making an array that big, or are you looking for some string?
    I don't get your question really.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You can't do that legally with a variable. If you know x will be 10 (or some other number) when you write the program use this:
    Code:
    const int x=10;
    
    char test[x];
    Some compilers allow your original code as an extension, but as I said that's not strictly legal in C++.

    If the size is not a constant (and you want to use standard C++), then you have no choice but to use a dynamic array container. The best choice for that is vector (or string) as shown by Elysia. You can also use new[]/delete[], but that would make sense only if your instructor doesn't allow you to use vector for some reason.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    Thanks Daved and Elysa for the inputs. That helps me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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