Count the posts

This is a discussion on Count the posts within the C++ Programming forums, part of the General Programming Boards category; How do I count the posts in an array?...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    4

    Count the posts

    How do I count the posts in an array?

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    What on earth do you mean??? You'll have to elaborate a bit, my mind-reading skills ain't what they used to be

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    4
    How do i know how many variables there are in an array?

    Like

    int hello[x];

    How do i know how many "there are" in that array without "touching" variable x.

  4. #4
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    I suppose you remember that hello[x] is not valid if x is not a constant? (Though this is e.g. a GNU extension in gcc compiler)
    int hello[x];

    sizeof(hello)/sizeof(int) is maybe what you want?

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You cannot use sizeof on an array directly.
    Using sizeof(hello); would return 4, whatever size the array have, since hello is a pointer (sort of) and not the whole array.
    To get the length, you need to store its size when you create it. I know of no other way...

  6. #6
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    That is not the case. When a pointer is passed as a parameter to sizeof it returns the size of the pointed structure in bytes. Therefore this statement (sizeof(hello)/sizeof(int)) returns the number of elements in the array of type int*.
    But this won't work for dynamically allocated arrays which was not the case.
    Last edited by raimo; 06-29-2002 at 12:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bintree and count (withouth using template)?
    By cubimongoloid in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2009, 07:22 AM
  2. input question
    By piyush_v in forum C Programming
    Replies: 9
    Last Post: 04-12-2007, 08:09 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  4. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 08:08 PM
  5. posts per day cant count
    By iain in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-13-2001, 10:18 AM

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