0

I asked a question here: C++ - class issue

What I'm still not getting is the pointer parameter here:

void setInfo(char *strName,int id,double wage)

Where it is called by:

abder.setInfo("Abder-Rahman",123,400);

I know that the name of the array is a pointer. But, why should we have to have a pointer data type? Cannot we use a char[] in the function parameter list? As I think I got an error when I tried that.

Thanks.

Community
  • 1
  • 1
Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 5
    You need to get yourself a good book. It's by far the best solo way to learn C++. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – CB Bailey Jan 23 '11 at 10:32

4 Answers4

3

But, why should we have to have a pointer data type?

Because the size of the arguments to the function needs to be known when the function is compiled. I.e. the function needs to know how far up the list of arguments in memory to find id and wage.

Cannot we use a char[] in the function parameter list?

Yes you can, but it is still passed by pointer. None of the additional attributes the data has as part of being an array (such as sizeof() returning the total size of the array) are preserved inside the called function. The ability to use [] in the function signature is just an indicator for you that the item passed should be an array of some type (rather than a pointer to, say, a structure).

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • Thanks for your reply. when I tried to use [] in the function signature like this: "char[] strName" I got an error. How should I write it as a parameter? Thanks. – Simplicity Jan 23 '11 at 15:50
  • @SWEngineer: "An error" is wonderfully "specific" :) What's the actual error message? Also, it should be `char strName[]` not `char[] strName` :) – Billy ONeal Jan 23 '11 at 19:57
2

For reference, from the C++03 standard, § 8.3.5 3:

After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively.

Furthermore, an array declared with [] is an incomplete object type (§ 8.3.4 1), though this shouldn't cause a problem when declaring a function parameter as it will be converted to a pointer.

outis
  • 75,655
  • 22
  • 151
  • 221
  • Thanks for your reply. When I use the parameter like this: "char[] strName" I get an error. How should I write it as a parameter? Thanks. – Simplicity Jan 23 '11 at 15:49
  • @SWEngineer: C++ is not Java is not C# is not D. Get a decent C++ book and learn. – sellibitze Jan 23 '11 at 18:03
  • @SWEngineer: an identifier, if any, must come before brackets. § 8.3.4 1 demonstrates this, but you really must look to the grammar (the [direct-declarator](http://www.open-std.org/jtc1/sc22/open/n2356/gram.html#gram.dcl.decl), type-id and [new-expression](http://www.open-std.org/jtc1/sc22/open/n2356/gram.html#gram.expr) non-terminals, and the terminals they reduce to and that reduce to them) to get the whole story. The situations where no identifier is necessary (e.g. `new[]`, `sizeof`, `typeid`, casts, template arguments) doesn't include declarations. Not that you should learn from the std. – outis Jan 23 '11 at 22:37
1

char[] is also a pointer. And yes, you could use it.

Stefan H Singer
  • 5,469
  • 2
  • 25
  • 26
1

An array and a pointer are very much related - you can even use a pointer like an array, like

void foo(char *ptr) {
    ptr[2] = 'x';
}

In your case, you get a compilation error (or warning) because string literals (like "foo") are considered const, i.e. they may not be modified, and since a function that doesn't take a const pointer doesn't make a promise not to change it, the compiler refuses to hand it a string literal.

You'd need to do

void setInfo(const char *strName,int id,double wage)

In general, you should always use const for pointer and reference arguments unless you're planning to modify the object.

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • Wow, my post is double negative heaven. Don't you not agree? – EboMike Jan 23 '11 at 10:38
  • Not true -- you won't get a compile error because of a specific backwards compatibility issue in C++ -- namely that character literals can be used as non-`const` function parameters (though the function is not allowed to modify them). (Though most compilers will warn about this conversion (i.e. http://codepad.org/I8C2D9wq )) – Billy ONeal Jan 23 '11 at 10:41
  • I always compile with warnings as errors... to me, they're both the same thing :) In any case, writing to a string literal is undefined behavior, so I would *very* much advise people not to omit the `const` if there is no intention to modify the string. – EboMike Jan 23 '11 at 10:45