473,606 Members | 2,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
16 2344
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
Virtual_X <C.*******@gmai l.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.l earn.c-c++

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 13 '07 #3
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
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
Virtual_X <C.*******@gmai l.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
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
Victor Bazarov <v.********@com acast.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
On Jul 13, 1:25 pm, ricec...@gehenn om.invalid (Marcus Kwok) wrote:
Victor Bazarov <v.Abaza...@com acast.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

Virtual_X <C.*******@gmai l.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3194
by: Armando | last post by:
Hallo ! I habe some error in my programm,because i use <fstream.h>,I want to use <fstream> but i don´t know which fonctions i must modify in my program ? Thanks you for your help. Armando.
140
7799
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
51
4494
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
13
2365
by: poison.summer | last post by:
For instance, I'd like to use unsigned char as an 8-bit integer. Can I use like unsigned char a=0; a++ Thanks a lot!
12
2369
by: shya | last post by:
hi there! I just wrote a function that reverse a string. It seems all ok to me, but the program goes on segmentation fault on *s = *t_str. Here's the code: #include <stdio.h> #include <stdlib.h> void reverse(char *s) {
4
1327
by: Bill | last post by:
Got a form that pulls data and allows for the user to enter a response in a textbox and I have a button that is supposed to push this reponse text back to the database for that record when clicked. HOWEVER all that is happening when I do this is the data field that is supposed to be updated with the text from the textbox goes from being NULL to just being blank(almost like its updating, but not updating it with the text in the textbox)...
4
8377
by: Virtual_X | last post by:
some function make the data type of it's parameters as "const char*" why not use char or char* instead what would be different
92
6167
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
32
2711
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template issue. A noddy mixin layer example should illustrate the issue... class Base { protected: int m_Field;
0
7962
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8443
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8315
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5467
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3945
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2452
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1309
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.