array of character pointers

This is a discussion on array of character pointers within the C++ Programming forums, part of the General Programming Boards category; I'm writing a function that inserts a small list into a tree view control, and when the program gets certain ...

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    array of character pointers

    I'm writing a function that inserts a small list into a tree view control, and when the program gets certain input, it can have variable numbers of entries. ex:

    char *numbers[] = {"one","two","three"};

    my function has to calculate how many 'words' are in the array. I can't seem to figure out how to do this.

    int num = sizeof(numbers)/sizeof(numbers[0]);
    int num = sizeof(numbers)/sizeof(char*);
    ...

    so far, num turns out to be 2, even if i add a "four" into the array. Anyone have ideas.

    PS: I'm sorry if there's a post about this, but i searched many times and the search phrases i used didn't bring up anything useful.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you can only use the sizeof(array) / sizeof(element) trick if you use it in the same block that the array was declared. Once you pass it to a function, it will see it as only a pointer, not a pointer to an array, so size of will return 4 instead of 4 * sizeof(element).

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    are you saying that it's either very difficult or impossible to find the info i need?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    its neither impossible, nor difficult. You just have to let the function somehow know what the size of the array is. The easiest way to do that is pass it as an extra variable.

    If your code is similar to what you posted, this will not concern you, but another note:
    if you dynamicly create the array, the sizeof(array) / sizeof(element) trick won't work either. For that method to work, it has to be declared staticly (by that, I mean without using new).

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    well, the problem is that the program doesn't know how many strings there are going to be. an example of it reading a text file (like an ebook) and the tree view is going to have to list chapters:

    Code:
    -Chapter 1
      +Page 1
      +Page 2
    -Chapter 2
      +Page 1
      +Page 2
      +Page 3
      +Page 4
    It's going to parse the text in a file and it won't know how many pages are in each chapter until it reads in the ebook.

    note: the program doesn't have anything to do with ebooks, it was just an example i could think of.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    When you're parsing through the file, can't you keep track of how many pages you've read since the last chapter was recognized?

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    well, it's not going to be from a text file. the information is going to be received from sockets. but im thinking about it and i guess it's possible to keep track of it the same way.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 09:21 AM
  3. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Help with 2-D character array
    By choykawairicky in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 01:12 AM

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