Dogtree 23 Posting Whiz in Training

>> I guess this would be helpful
No, not a bit. Your code is wrong.

>> for (n=0;inp[n];n++)
Assuming that n was defined somewhere, the test for imp[n] against 0 is dngerous because imp is uninitialized. There could be a null character straight away, or 5000 characters later. You're really risking an access violation with this loop.

>> char *arr = new char [sizeof(char)];
This alloctes memory for one char. sizeof(char) is guaranteed to return 1, everywhere, without fail. That's one of the few absolutes when it comes to type sizes in C++.

>> for (n=0;arr[n];n++)
You have the same problem here as the previous loop.

Yes, your idea is valid assuming there's some sentinel value at the end of the array to stop the loop on. With the original question that's difficult because any string object is valid in the general case. That's probably why you took it upon yourself to change the example to char so that you could use a null character as the sentinel.

The best solution is to avoid using arrays in the first place because they're unsafe and most people don't understand them well enough to avoid the pitfalls, as displayed by your flawed example. The std::vector class provides a good container that grows dynamically.

SpS commented: Right ~~SpS +3
Dogtree 23 Posting Whiz in Training

You can omit the array size for the first dimension because array names are almost always converted to a pointer to the first element, so any size information is lost. That feature only applies to the first dimension, so you have to provide size information for the second dimension because the conversion to a pointer makes it a pointer to an array of N. These two function declarations are equivalent:

void foo(int a[][10]);
void bar(int (*a)[10]);

To avoid those errors, you can always provide sizes for all dimensions until you're comfortable with the rules. However, for arbitrary sizes in all dimensions, you have no choice but to use a dynamic array:

void foo(int **a, int m, int n);

int main()
{
  int **a = new int*[5];

  for (int i = 0; i < 5; i++)
    a[i] = new int[5];

  foo(a, 5, 5);

  for (int i = 0; i < 5; i++)
    delete [] a[i];
  delete [] a;
}

Of course, that's assuming you don't have libraries to work with. The standard vector class is well suited to this:

#include <vector>

void foo(std::vector<std::vector<int> > a);

The boost library also supports a multi_array class.

I_m_rude commented: nice! +0
Dogtree 23 Posting Whiz in Training

Koenig Lookup

There are three ways to use a name nested in a namespace. Making the namespace open to the current scope with a using directive:

using namespace std;

cout << "Hello world!" << endl;

Making only select names open to the current scope with a using declaration:

// Make cout and endl available, but nothing else
using std::cout;
using std::endl;

cout << "Hello world!" << endl;

And explicit qualification of every name for every use:

std::cout << "Hello world!" << std::endl;

It's best to never use the using directive unless you know for a fact that you won't have name clashes. That's usually not easily possible, and most people will suggest that you use explicit qualification all the time. The problem with that is explicit qualification is so verbose! Koenig lookup is a trick to shorten some of those names.

Here's a common example:

vector<int> c;

// ...

std::copy(c.begin(), c.end(), std::ostream_iterator<int>(std::cout, "\n"));

Koenig lookup says that if a function in a namespace takes an argument from the same namespace, the function need not be qualified because the compiler will look in that namespace for matching functions. So the following is legal C++:

copy(c.begin(), c.end(), std::ostream_iterator<int>(std::cout, "\n"));

copy takes an ostream_iterator object as an argument. Because both ostream_iterator and copy are members of namespace std, Koenig lookup finds copy via ostream_iterator. A third of the namespace qualification has been removed from this line, and that is a big timesaver when it all adds …

JoBe commented: Very friendly, Ace in C++ in my eyes, doesn't have a problem in explaining something what you didn't understand the first time! +1
Dogtree 23 Posting Whiz in Training

It depends on the compiler. Generally the files will be compiled in the order that they're listed when the compiler is run.

>>And what's the one declaration rule?
One "definition" rule. It means that you can declare an object as much as you want because a declaration only says "Hey, I exist", but you can only define it once because a definition actually makes the object exist by allocating memory and calling constructors. If there are multiple definitions then you'll get an error.

Dogtree 23 Posting Whiz in Training

I'm sorry you feel that way. You can go elsewhere and you probably won't be missed. To answer your increasingly rude question, I would probably use a combination of Visual Basic for the user interface and C++ for the back-end processing. But, since you clearly aren't going to be writing the software or encouraging someone to write it for you, the question of what languages to use is strictly theoretical.

Goodbye, and I hope you find a community that thrives on veiled insults, as that seems to be all you can offer.

~s.o.s~ commented: So true. +19