473,796 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3517

"pembed2003 " <pe********@yah oo.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*****@earthl ink.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*****@earthl ink.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********@yah oo.com> wrote in message
news:db******** *************** ***@posting.goo gle.com...
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********@yaho o.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********@yaho o.com (pembed2003) wrote:
Martin Ambuhl <ma*****@earthl ink.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********@yaho o.com (pembed2003) writes:
Martin Ambuhl <ma*****@earthl ink.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_Keit h) 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********@yaho o.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

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

Similar topics

7
2754
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 ========================================== We can see that the same assembly code is generated for * foo2 (char& )
8
2248
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
3
14517
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
293
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
2
1870
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
1684
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
20835
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
2233
by: arnuld | last post by:
this is the code: ------------------------------------------------------------------ #include <iostream> void g(char&){}; void h(const char&) {}; int main() { char c;
5
4749
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 size? like
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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
10456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
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
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5442
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...
1
4118
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

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.