some thing easy with array

This is a discussion on some thing easy with array within the C++ Programming forums, part of the General Programming Boards category; how can i know what the size of array meaning i[n] how i can know what is the n is ...

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

    some thing easy with array

    how can i know what the size of array meaning
    i[n]
    how i can know what is the n is (total the end)

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    It is not pratical, but works with static arrays. You have n=(sizeof(yourArray)/sizeof(int)). But if you have an static array you already know the size. My suggestion is that you use the vector class.
    Nothing more to tell about me...
    Happy day =)

  3. #3
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    If you are using a static array ( i [n] ) and you want the number of elements (n), use sizeof.

    Code:
    int I [n];
    int SizeOfI
    int SizeOf sizeof I;
    If, however, you want the offset of the last element, use :

    Code:
    int I [n];
    int SizeOfI
    int SizeOf sizeof I - 1 ;
    Using vectors or deques is better. They dont overfill, you cant write past the end, - dont let me go on.

    If you want to learn vectors, strings (dont use char arrays) go to http://www.cppreference.com .
    Ph33r the sphericalCUBE

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you want to learn vectors, strings (dont use char arrays) go to http://www.cppreference.com .
    There's nothing wrong with char arrays if you use them correctly. In fact, some functions take them as a parameter, which makes it difficult to use strings.

    Generally you will pass the size of the array to the function you are using it in:
    Code:
    int somefunc(char* str,int n)
    {
    //...
    
    }
    If you don't know what the length of the string will be (a user is inputting it, for example) then using a std::string might be better.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if you use them correctly
    There you go. Many people don't know how to use them correctly, either through lack of experience or lack of proper education (or stupidity. I hate to say it, but it's true). This is why C++ discourages the use of low level constructs, they are too dangerous for most of the plebs out there.
    My best code is written with the delete key.

  6. #6
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    In fact, some functions take them as a parameter, which makes it difficult to use strings.


    [code]
    std::string szString;
    char LameCharString [60];
    LameCharString = szString.c_str();
    {/code]
    Ph33r the sphericalCUBE

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Like I said, not impossible, but slightly more difficult. Although c_str() returns a const c-string There's two other ways:

    std::string::data()
    Returns a const char* without a '\0' terminator

    std::string::copy(char* buf, size_type buf_size)
    returns a char* without a '\0' and copies up to buf_size characters

    std::string::copy(char* buf, size_type buf_size, size_type idx)
    same as above, but starts copying at the index idx

    See if you need a non-const c-string then you have to not only call the copy function, but append a '\0' at the end...Also if you use the second form of copy, it may throw an exception if you use an invalid index for idx (one that is greater than string::length())
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. cant see solution, should be easy array problem.
    By jinn in forum C Programming
    Replies: 4
    Last Post: 10-30-2007, 06:45 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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