Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C/C++ question: How to pass a variable by reference?

Status
Not open for further replies.

saturn

Member level 1
Joined
Jul 3, 2004
Messages
32
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,286
Location
Solar System
Activity points
267
Hi,
1 year+ of assembly programming really spoil my C skill. I think there is a way to do this but forget how it can be done.

If I create a variable "char buf[80];", how could I pass it to a function so that inside the function, buf can be written directly.

Thanks & regards.
 

checkmate

Advanced Member level 3
Joined
Feb 25, 2004
Messages
833
Helped
178
Reputation
356
Reaction score
125
Trophy points
1,323
Location
Toilet Seat
Activity points
7,815
Geez ... you really need a C reference book.

void func1(char* buffer) {
...
*buffer = ...;
buffer++;
...
}

void main() {
char buf[80];
...
func1(buf);
...
}
 

ntfreak

Full Member level 3
Joined
Feb 22, 2002
Messages
152
Helped
6
Reputation
12
Reaction score
0
Trophy points
1,296
Location
UK
Activity points
1,249
By reference is actually c++, checkmate has given an example of the c method.
you could also do by reference, eg.

void cfunc(char* pszText)
{
// using c pointer
strcpy( pszText, "hello" );
}

void cpfunc(char& pszText)
{
// using c++ reference
strcpy( &pszText, "hello" );
}

void main(void)
{
char szText[80];
cfunc( szText );
cpfunc( *szText );
}

Both above will give the same results
Regards
NTFreak
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top