字符*&安培;和char * ??? [英] char*& and char* ???

查看:68
本文介绍了字符*&安培;和char * ???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好编码员,

我有以下内容:


void f1(char *& s){

* s =''a'';

}


void f2(char * s){

* s ='' b'';

}


int main(int argc,char ** argv){

char a [] = 1234;

char * b = a;

printf("%s \ n",a);

f1 (b);

printf("%s\ n,a);

f2(b);

printf(" ;%s \ n",a);

返回0;

}


以上版画:


1234

a234

b234


我的问题是:


1. char *&与char *不同,因为他们都做同样的事情?

2.你什么时候使用第一张表格和第二张表格?


谢谢!

Hi coders,
I have the following:

void f1(char* &s){
*s = ''a'';
}

void f2(char* s){
*s = ''b'';
}

int main(int argc,char** argv){
char a[] = "1234";
char* b = a;
printf("%s\n",a);
f1(b);
printf("%s\n",a);
f2(b);
printf("%s\n",a);
return 0;
}

The above prints:

1234
a234
b234

My question is:

1. How is char*& different from char* since they both do the same thing?
2. When do you use the first form and the second form?

Thanks!

推荐答案



" pembed2003" < PE ******** @ yahoo.com>在消息中写道

"pembed2003" <pe********@yahoo.com> wrote in message
1. char *&与char *不同,因为它们都是相同的>东西?

char *&是C ++和ia通过引用传递的指针。 char *是C,只是

a指针。
2.你什么时候使用第一个表格和第二个表格?
1. How is char*& different from char* since they both do the same > thing?
char *& is C++ and ia a pointer passed by reference. char * is C and is just
a pointer.
2. When do you use the first form and the second form?



第一个表格时用C ++编程。你需要在clc ++中询问何时使用引用以及何时使用指针才能使用


只要你想传递一个数组,就可以使用第二种形式

字符(例如字符串)到C函数,以及其他一些情况。


First form when programming in C++. You need to ask in clc++ about when it
is appropriate to use references and when to use pointers.
You would use the second form whenever you want to pass an array of
characters (eg a string) to a C function, and in a few other cases.


pembed2003写道:
pembed2003 wrote:
你好编码员,
我有以下几点:

void f1(char *& s){
^^^^^^^^

语法错误

[...]


1. char *&不同于char *


''char *&''是语法错误,''char *''不是。

因为他们两者都做同样的事情?


不,他们不是。

2.你什么时候使用第一个表格


从不

和第二种形式?
Hi coders,
I have the following:

void f1(char* &s){ ^^^^^^^^
syntax error
[...]

1. How is char*& different from char*
''char*&'' is a syntax error, ''char*'' isn''t.
since they both do the same thing?
No they don''t.
2. When do you use the first form
Never
and the second form?




当你需要一个指向char的指针



When you need a pointer to char


Martin Ambuhl< ma ***** @ earthlink.net>在消息新闻中写道:< c5 ************ @ ID-227552.news.uni-berlin.de> ...
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<c5************@ID-227552.news.uni-berlin.de>...
pembed2003写道:
pembed2003 wrote:
你好编码员,
我有以下内容:

void f1(char *& s){^^^^^^^^
语法错误
[...]
Hi coders,
I have the following:

void f1(char* &s){ ^^^^^^^^
syntax error
[...]




我使用gcc编译代码,但它不会产生任何错误。你知道为什么?b $ b知道为什么? gcc是C编译器吗?



I compile the code using gcc but it doesn''t produce any error. do you
know why? Is gcc a C compiler?

1. char *&不同于char *
''char *&''是语法错误,''char *''不是。
1. How is char*& different from char*
''char*&'' is a syntax error, ''char*'' isn''t.
因为他们都做同样的事情什么?
since they both do the same thing?



不,他们不是。



No they don''t.




抱歉。我的意思是他们在示例程序中做同样的事情我发布了




sorry. i mean that they do the same thing in the sample program I
posted.

2.你什么时候使用第一个表格
2. When do you use the first form



从不



Never




为什么?



Why?

和第二个表格?



当你需要一个指向char的指针



When you need a pointer to char



谢谢!



Thanks!


这篇关于字符*&安培;和char * ???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆