String Addition Problem...

This is a discussion on String Addition Problem... within the C++ Programming forums, part of the General Programming Boards category; 7stud: not a good question when it comes to pointers. Yeti: Yes, it would. However, it is not a good ...

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    7stud: not a good question when it comes to pointers.

    Yeti: Yes, it would. However, it is not a good idea to do it that way, because you don't know how large the array is, and out-of-bounds access is all too easy. The typical way is to pass the size along with the pointer, i.e.
    Code:
    void a_function(int *passed_array, std::size_t passed_array_size)
    {
      if(passed_array_size < 6) {
        throw std::out_of_bounds("Array is too small.");
      }
      passed_array[5] = 1;
    }
    Of course, in C++ it's even better to pass a reference to a boost::array or a std::vector.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Marxist-Trotskyist
    Join Date
    May 2005
    Location
    Many, many miles from the center of the Earth.
    Posts
    31
    Vector, that's what I was looking for. Thanks very much
    If a=b, and b=c, then a=c, except where void and prohibited by law...

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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