Thread: printing array

  1. #1
    Registered User
    Join Date
    Nov 2004
    Slovenia, Europe

    printing array

    How to get the number of all fields in array?

    Because I would like to show an array as in PHP:

    1 => "value",
    2 => "value2"

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Manassas, VA USA
    In c++.. you'll know the number of elements in an array because:

    1) you will have to initially declare the size of a static array

    for example: int array[5]; will have 6 elements.

    2) if you are using a dynamic array, you can just keep track of the array size each time you resize the array.

    int *array;

    int size = 50;

    array = new int[size];

    3) if you are using a vector, just use the size( ) function.

    example: int size = array.size( ); //will return the current size of the array and store into an integer variable named, 'size'
    Last edited by The Brain; 11-16-2004 at 03:25 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > for example: int array[5]; will have 6 elements.
    You mean 5 elements

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing half a char array
    By theoneandonly in forum C Programming
    Replies: 19
    Last Post: 11-11-2006, 07:27 AM
  2. Replies: 3
    Last Post: 11-03-2003, 08:55 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM