Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I'm new to C. Can anyone sho

Status
Not open for further replies.

joinme

IS-IT--Management
Jan 12, 2003
20
DE
Hi,

I'm new to C. Can anyone show me an example of passing by refereence? The passing param is a string of 50 in length.

Much aprreciate that.

-JO
 
you use 'pass by reference' with large arrays or structures
and use the & symbol (for the address of the item )
so you pass just 1,2,4 or 8 bytes or so (depending on datatype)
eg

/* Prototype it first */

void stringcheck(char *);

main()
{
char arrayname[50];
strcpy(arrayname,"ABCDEFGHIJKLMNOP etc etc");
stringcheck(&arrayname);
}

void stringcheck(*arrayname)
{

/*do your coding here*/

}
;-)
Dickie Bird
db@dickiebird.freeserve.co.uk
 
Help!

void stringcheck(*arrayname)-->err: missing ) before *
{

...my code
}
 
Hi, dickie bird I think you got it wrong when passing a char array by reference, here is a small program to show the difference between passing int values and arrays by reference compared to by value....not the use of the ampersand ( & ) in the call to refer_check function and the use of the pointer symbol ( * ) in the function and in the prototype and definition.

#include <stdio.h>
#include <string.h>

void refer_check(char *array, int *num);
void value_check(char array[], int num);

int main(void)
{
char arrayname[50];
int number = 1;

strcpy(arrayname,&quot;Lets Start In Main&quot;);
printf(&quot;\nString in main before is %s&quot;, arrayname);
printf(&quot;\nNumber in main before is %d&quot;, number);

refer_check(arrayname, &number);

printf(&quot;\n\nString in main after reference function is %s (changed)&quot;, arrayname);
printf(&quot;\nNumber in main after reference function is %d (changed)&quot;, number);

value_check(arrayname, number);

printf(&quot;\n\nString in main after value Function is %s (changed)&quot;, arrayname);
printf(&quot;\nNumber in main after value Function is %d (not changed)&quot;, number);
return 0;
}

void refer_check(char *array, int *num)
{
strcpy(array, &quot;Refence Function&quot;);
printf(&quot;\n\nString in the reference function is %s&quot;, array);
*num = 10;
printf(&quot;\nNumber in the reference function is %d&quot;, *num);
}

void value_check(char array[], int num)
{
strcpy(array, &quot;Value Function&quot;);
printf(&quot;\n\nString in the value function is %s&quot;, array);
num = 20;
printf(&quot;\nNumber in the value function is %d&quot;, num);
}

you can see how the values change if you compile and run this.

Hoping to get certified..in C programming.
 
Thanks bigtamscot! Your sample code work great!

I have another question: In a void function, I want to do some error checks, if the condition is failed, exit the function. I use 'return 0' to exit function but it give me the warning. I guess return 0 only works for non-void function.

In VB if I want to exit a function, I use exit sub. What syntax to exit a void function in C?

Thanks.
 
I found the answer:

Exit (main()); //this will exit the current funct & return to main

best,
JO
 
Alternatively, you could go...

void check (int number)
{
for( ; ;number++ )
if(number % 10 == 0)
return; /* don't need a value here */
} Hoping to get certified..in C programming.
 
Note that there really is no pass by reference in C. You can change automatic variables in the caller's function by using pointers (as demonstrated above), however you are still passing everything by value.

The reason that I mention this is because describing this mechanism as &quot;pass by reference&quot; can be confusing to newcomers. In particular, I suspect it is one of the causes of this common mistake:

void foo(char *s)
{
s=malloc(500);
}

int main(void)
{
char *s;
foo(s);
/* Hey, why doesn't s point at allocated memory?? */
return 0;
}

s is passed by value, so the s in main() is a different pointer than the s in foo().
Russ
bobbitts@hotmail.com
 
Does anyone know where i can learn the gist of C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top