char*& and char* ???

P

pembed2003

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!
 
M

Malcolm

pembed2003 said:
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.
 
P

pembed2003

Martin Ambuhl said:
pembed2003 said:
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?
'char*&' is a syntax error, 'char*' isn't.


No they don't.

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


When you need a pointer to char

Thanks!
 
M

Martin Ambuhl

pembed2003 said:
Martin Ambuhl said:
pembed2003 said:
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
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.

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

Martin Dickopp

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
 
C

Christian Bau

Martin Ambuhl said:
pembed2003 said:
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.
 
K

Keith Thompson

Martin Ambuhl said:
pembed2003 said:
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.
 
D

Dan Pop

In said:
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
 
P

pembed2003

Keith Thompson said:
Martin Ambuhl said:
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.

Thanks Keith and everyone else. I know my mistake.
 
J

James McIninch

pembed2003 said:
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.
 
C

Chris Fogelklou

pembed2003 said:
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.
}
 

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
474,141
Messages
2,570,817
Members
47,365
Latest member
BurtonMeec

Latest Threads

Top