By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jul 13 '07 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Virtual_X wrote:
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
Jul 13 '07 #2

P: n/a
Virtual_X <C.*******@gmail.comwrote:
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++

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 13 '07 #3

P: n/a
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[]

Jul 13 '07 #4

P: n/a
Virtual_X wrote:
>>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++.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 13 '07 #5

P: n/a
Virtual_X <C.*******@gmail.comwrote:
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[]
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);
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 13 '07 #6

P: n/a
Marcus Kwok wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 13 '07 #7

P: n/a
Victor Bazarov <v.********@comacast.netwrote:
Marcus Kwok wrote:
> 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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 13 '07 #8

P: n/a
On Jul 13, 1:25 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Victor Bazarov <v.Abaza...@comacast.netwrote:
Marcus Kwok wrote:
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
thank's for all
i got it

Jul 13 '07 #9

P: n/a

Virtual_X <C.*******@gmail.comwrote in message...
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[]
Learn to experiment, test things. Try this:

#include <iostream>

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

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

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

void st( char *&x, std::ostream &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::ostream &out ){} */.
Compile and run.
Then comment-out /* void st( char *x, std::ostream &out ){} */,
Un-comment the first (char x[]), compile and run.

Your findings?

--
Bob R
POVrookie
Jul 13 '07 #10

P: n/a

Marcus Kwok <ri******@gehennom.invalidwrote in message...
Victor Bazarov <v.********@comacast.netwrote:
Marcus Kwok wrote:
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,
Cruel! You should fire your boss! <G>

--
Bob R
POVrookie
Jul 13 '07 #11

P: n/a
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.
Jul 13 '07 #12

P: n/a
Juha Nieminen wrote:
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

Jul 13 '07 #13

P: n/a
Juha Nieminen wrote:
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?

--
rbh
Jul 13 '07 #14

P: n/a
On Jul 13, 9:52 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Virtual_X wrote:
>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.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 14 '07 #15

P: n/a
On Jul 14, 12:27 am, Juha Nieminen <nos...@thanks.invalidwrote:
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 14 '07 #16

P: n/a
James Kanze wrote:
"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
Jul 14 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.