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.

1

4 Answers 4

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@SWEngineer: "An error" is wonderfully "specific" :) What's the actual error message? Also, it should be char strName[] not char[] strName :)
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.

3 Comments

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.
@SWEngineer: C++ is not Java is not C# is not D. Get a decent C++ book and learn.
@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, type-id and new-expression 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.
1

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

2 Comments

Only as the declaration of a function parameter, otherwise char[] is an incomplete array type.
true. I don't use it myself, I prefer * :)
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.

3 Comments

Wow, my post is double negative heaven. Don't you not agree?
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. codepad.org/I8C2D9wq ))
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.