473,383 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

char*& and char* ???

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!
Nov 14 '05 #1
11 3471

"pembed2003" <pe********@yahoo.com> wrote in message
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?

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.
Nov 14 '05 #2
pembed2003 wrote:
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?


When you need a pointer to char
Nov 14 '05 #3
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<c5************@ID-227552.news.uni-berlin.de>...
pembed2003 wrote:
Hi coders,
I have the following:

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


I compile the code using gcc but it doesn't produce any error. do you
know why? Is gcc a C compiler?
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. When do you use the first form


Never


Why?
and the second form?


When you need a pointer to char


Thanks!
Nov 14 '05 #4
pembed2003 wrote:
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<c5************@ID-227552.news.uni-berlin.de>...
pembed2003 wrote:
Hi coders,
I have the following:

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

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


You didn't invoke gcc properly.
suppost your program is named a.c
Try one of
gcc -Wall -W -std=c89 -pedantic a.c
gcc -Wall -W -ansi -pedantic a.c
gcc -Wall -W -std=c99 -pedantic a.c

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.


They don't do the same thing, period. 'char* &' is a syntax error which
gcc, when properly invoked, is reported as a parsing error. It is
impossible for a syntax error to predictably have the same effect as
legal code. The most likely result of syntax error is a compilation
failure.
2. When do you use the first form


Never

Why?


If you don't get this, you are truly an idiot.

Nov 14 '05 #5
"pembed2003" <pe********@yahoo.com> wrote in message
news:db**************************@posting.google.c om...
Hi coders,
I have the following:

void f1(char* &s){
*s = 'a';
}
That looks like C++; it's not valid C.
void f2(char* s){
*s = 'b';
}


That is valid C.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #6
pe********@yahoo.com (pembed2003) writes:
Is gcc a C compiler?


No. GCC stands for "GNU Compiler Collection". The collection currently
includes compilers for C, C++, Objective C, Java, Fortran, and Ada.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #7
In article <db**************************@posting.google.com >,
pe********@yahoo.com (pembed2003) wrote:
Martin Ambuhl <ma*****@earthlink.net> wrote in message
news:<c5************@ID-227552.news.uni-berlin.de>...
pembed2003 wrote:
Hi coders,
I have the following:

void f1(char* &s){

^^^^^^^^
syntax error
[...]


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


I should really hope that you know which language your compiler is
compiling. Read the documentation.

Try compiling this:

int main (void)
{
int new = 0;
int class = 0;
return new + class;
}

and see what your compiler thinks about it.
Nov 14 '05 #8
pe********@yahoo.com (pembed2003) writes:
Martin Ambuhl <ma*****@earthlink.net> wrote in message
news:<c5************@ID-227552.news.uni-berlin.de>...
pembed2003 wrote:
Hi coders,
I have the following:

void f1(char* &s){

^^^^^^^^
syntax error
[...]


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


It can be; it can also be a C++ compiler. It typically decides which
frontend to invoke based on the suffix on the file name. If your
source file has a name ending in ".c", gcc should correctly report a
syntax error on the above; if it ends in ".C" or ".cc", it will assume
it's C++, and behave in a manner that we're not qualified to discuss
in this newsgroup.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #9
In <db**************************@posting.google.com > pe********@yahoo.com (pembed2003) writes:
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:


The above doesn't compile:

fangorn:~/tmp 381> gcc -ansi -pedantic test.c
test.c:1: error: parse error before '&' token
test.c: In function `f1':
test.c:2: error: `s' undeclared (first use in this function)
test.c:2: error: (Each undeclared identifier is reported only once
test.c:2: error: for each function it appears in.)

Furthermore, calling printf without a correct declaration in scope
invokes undefined behaviour.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10
pembed2003 wrote:
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?
No. In C, char*& is a syntax error. There is no such syntax in C.

2. When do you use the first form and the second form?
You use char * all the time since the other form doesn't exist.


Thanks!


--
remove .spam from address to reply by e-mail.
Nov 14 '05 #11

"pembed2003" <pe********@yahoo.com> wrote in message
news:db**************************@posting.google.c om...
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! void f1(char* &s){
As everyone said... this is a syntax error in C.
1. How is char*& different from char* since they both do the same thing?
However, in C++, you are passing a char * by reference... meaning, not only
could you have modified the character pointed to by 's' in the same way, but
the pointer 's' itself:

s = NULL; //This change would be undone by returning if s was not passed by
reference.

So, you must somehow be compiling in C++ rather than C.
2. When do you use the first form and the second form?


In C/C++, use the first form when you don't want to modify the value of the
variable passed in.

You would only use the second form in C++, and only when you want the
changes that you make to the variable to be returned to the calling program.

In C, you can only modify variables passed into a function through pointers:

void f1(char **ps){
*ps = NULL; //Kill s.
}

Nov 14 '05 #12

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

Similar topics

7
by: Alex Vinokur | last post by:
========================================== Windows 2000 Professional CYGWIN_NT-5.0 1.5.4(0.94/3/2) GNU g++ version 3.2 20020927 (prerelease) GNU objdump 2.14.90 20030901...
8
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
3
by: Marcin Kalicinski | last post by:
void f(const char *&text); Is this a const reference to char * or a reference to const char *? And how to write both of them? thank you, Marcin
10
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
2
by: Luca Bart | last post by:
Dear all, I have a struct in C++: typedef struct{ char Command; short a; }PACKET; I have to send by UDP and C++ command is: UDP->SendBuffer((char*)&Data,sizeof(PACKET),sizeof(PACKET));
2
by: Zorro | last post by:
In header file istream there is an overload for every built-in type, except char. Is there a reason for this? GCC 3.3.4. is the compiler. Thanks.
3
by: gevadas | last post by:
sample program #include <iostream> #include <vector> using namespace std; int find(char*& value,char** arr,int size) { for(int i = 0;i < size;i++)
5
by: arnuld | last post by:
this is the code: ------------------------------------------------------------------ #include <iostream> void g(char&){}; void h(const char&) {}; int main() { char c;
5
by: Samant.Trupti | last post by:
Hi, There is one thing I am cofused about.... If I have a declareation say char str; Now if I want to change it to wchar so do I have to change it like .... wchar_t str; or double the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.