what's the different between char[] and char* as parameters

V

Virtual_X

if we use char[] in a function parameter like that

void st(char x[])
{cout << x;}

and char* as a parameter in the same function instead of char x[]

what would be different

in my experiments i conclude that
with char[] i can change the assigned value unlike char*

but in another question

why the value assigned to char[] when i change it in the function
it will also change the passed variable

ie:

void st(char x[])
{
x[0]= 'e';
cout << x;
}

int main()
{
char q[]= "CPP"

st(q);
cout << q;
}

in st(q) it will return "qPP" that logical

but
the second line will return the same result

HOW

i pass a variable not a reference and the function also don't accept
references
why the value changed in the result of the second line
 
M

Marcus Kwok

Virtual_X said:
if we use char[] in a function parameter like that

void st(char x[])
{cout << x;}

and char* as a parameter in the same function instead of char x[]

what would be different

Nothing. When used as a function parameter, arrays will decay into
pointers to the first element. You cannot pass an array by value to a
function.
in my experiments i conclude that
with char[] i can change the assigned value unlike char*

What do you mean? In both cases you will be passing a pointer to the
first element of the array. You cannot change the value (of the
pointer) passed to the function, but you can change what it points to.
but in another question

why the value assigned to char[] when i change it in the function
it will also change the passed variable

ie:

void st(char x[])
{
x[0]= 'e';
cout << x;
}

int main()
{
char q[]= "CPP"

st(q);
cout << q;
}

in st(q) it will return "qPP" that logical

No, it should change q to be "ePP".
but
the second line will return the same result

HOW

i pass a variable not a reference and the function also don't accept
references
why the value changed in the result of the second line

Because you passed a pointer. See above.

For these basic questions, maybe you would be better posting in
alt.comp.lang.learn.c-c++
 
D

Default User

Virtual_X said:
if we use char[] in a function parameter like that

void st(char x[])
{cout << x;}

and char* as a parameter in the same function instead of char x[]

Nothing, they are two ways of saying the same thing.
what would be different

in my experiments i conclude that
with char[] i can change the assigned value unlike char*

I don't know where you got that idea, but it's untrue.
but in another question

why the value assigned to char[] when i change it in the function
it will also change the passed variable

Because it's a pointer.

Your textbook should explain all of this.





Brian
 
V

Victor Bazarov

Virtual_X said:
Because you passed a pointer. See above.

how i pass a pointer to char[]

You need to understand that 'char[]' is the same as 'char*' in C++.
If you don't mean 'char[]', please be more explicit (like use the word
"array" or something).

A pointer to an array of char has the type 'char (*)[N]' where 'N' has
to be a compile-time constant expression. A pointer to a pointer to
char has the type 'char**'.

V
 
M

Marcus Kwok

Virtual_X said:
Because you passed a pointer. See above.

how i pass a pointer to char[]

Re-read my first post and Brian (Default User)'s post. The function
signatures

void foo(char* bar);

and

void foo(char[] bar);

mean exactly the same thing.


If you are asking how to pass a pointer to an array, then the size must
be known at compile time, or you must make it a template that can deduce
the size at compile time:


#include <iostream>

template <int N>
void foo(char (*arr)[N])
{
std::cout << *arr << '\n';
}

int main()
{
char cstr[] = "foo";
foo(&cstr);
}
 
V

Victor Bazarov

Marcus said:
[..]
Re-read my first post and Brian (Default User)'s post. The function
signatures

void foo(char* bar);

and

void foo(char[] bar);

void foo(char bar[]);

the syntax 'char[] bar' does not exist in C++.
mean exactly the same thing.

[..]

V
 
M

Marcus Kwok

Victor Bazarov said:
Marcus said:
void foo(char[] bar);

void foo(char bar[]);

the syntax 'char[] bar' does not exist in C++.

Thanks, sorry about that. I've just had to start learning some C# for
work, and it's the subtle differences in the languages (like this one)
that I find are confusing me more than the major differences.
 
B

BobR

Virtual_X said:
Because you passed a pointer. See above.

how i pass a pointer to char[]

Learn to experiment, test things. Try this:

#include <iostream>

void st( char x, std::eek:stream &out ){
out<<"char x="<< x;
}

void st( char x[], std::eek:stream &out ){
out<<"char x[]="<< x;
}

void st( char *x, std::eek:stream &out ){
out<<"char *x="<< x;
}

void st( char *&x, std::eek:stream &out ){
out<<"char *&x="<< x;
}
// ....etc.

int main(){
char q[]= "CPP";
st( q, std::cout );
std::cout <<std::endl;
std::cout << q <<std::endl;

st( q[1], std::cout );
std::cout <<std::endl;
std::cout << q <<std::endl;

return 0;
}

What did your compiler say when you tried to compile that?

Then comment-out /* void st( char x[], std::eek:stream &out ){} */.
Compile and run.
Then comment-out /* void st( char *x, std::eek:stream &out ){} */,
Un-comment the first (char x[]), compile and run.

Your findings?
 
J

Juha Nieminen

Followup question:

Why is something like: void foo(int table[10]) { ... }
valid syntax given that the '10' plays absolutely no role there?
sizeof(table) gives 4.
 
D

Default User

Juha said:
Followup question:

Why is something like: void foo(int table[10]) { ... }
valid syntax given that the '10' plays absolutely no role there?
sizeof(table) gives 4.

Because the standard says so. Whatever appears in the brackets is
ignored, because "table" is converted to a pointer. It so happens that
the size of int* is four on your system.

Note that this only applies to the leftmost set. This:

void f(int a[][])
{
}

is not equivalent to:

void f(int **a)
{
}

In fact it's not even a legal declaration. You have to put a value in
the righthand set of brackets, which then makes "a" a pointer to an
array.

There's lots of array and pointer info in the C FAQ:

<http://c-faq.com/>



Brian
 
R

Robert Bauck Hamar

Juha said:
Followup question:

Why is something like: void foo(int table[10]) { ... }
valid syntax given that the '10' plays absolutely no role there?
sizeof(table) gives 4.

The dimensions would have a role if you have more than one:

void foo(int table[][10][20]);

Why create a special syntax for the first?
 
J

James Kanze

Virtual_X said:
but
the second line will return the same result
HOW
i pass a variable not a reference and the function also
don't accept references why the value changed in the
result of the second line
Because you passed a pointer. See above.
how i pass a pointer to char[]
You need to understand that 'char[]' is the same as 'char*' in C++.

Don't you start it. "char[]" and "char*" are two very different
things, in general. It's only in the specific context of a
formal function parameter that "char[]" means "char*", and not
"char[]". (The description I like is to say that in this
context, the compiler "rewrites" the declaration. And I know
you know this, but a statement like the above is dangerous. In
a few weeks, someone will post that arrays and pointers are the
same thing in C++, Victor says so, although the context will be
completely different.)
 
J

James Kanze

Followup question:
Why is something like: void foo(int table[10]) { ... }
valid syntax given that the '10' plays absolutely no role there?
sizeof(table) gives 4.

And given the above, something like:

int array[ 3 ] ;
foo( array ) ;

is perfectly legal.

As Default User said: "because the standard said so". C style
arrays are horribly broken. Don't use them unless you have to.
 
D

Default User

James said:
"char[]" and "char*" are two very different
things, in general. It's only in the specific context of a
formal function parameter that "char[]" means "char*", and not
"char[]". (The description I like is to say that in this
context, the compiler "rewrites" the declaration.

That's fairly close to what the C99 standard says, at least the draft.
I'm not at work so I can't check my copy of the C++ standard.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top