Author |
Message |
Bill Cunningha #1 / 6
|
 k&r2 question
On pg 27 1.8 of k&r2, Brian Kernighan writes, in the last sentence, "When necessary, it is possible to arrange for a function to modify a variable in a calling routine. The caller must provide the address of the variable to be set..." What's he saying? I've seen prototypes with the * operator, is scanf() an example of what he is saying? Is he talking about calling another function or a function calling on a variable in itself? -----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.*-*-*.com/ - The #1 Newsgroup Service in the World! -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
|
Mon, 13 Jun 2005 09:34:41 GMT |
|
 |
Kevin Easto #2 / 6
|
 k&r2 question
Quote:
> On pg 27 1.8 of k&r2, Brian Kernighan writes, in the last sentence, "When > necessary, it is possible to arrange for a function to modify a variable in > a calling routine. The caller must provide the address of the variable to be > set..." What's he saying? I've seen prototypes with the * operator, is > scanf() an example of what he is saying?
Yes, that's right. Quote: > Is he talking about calling another > function or a function calling on a variable in itself?
I cannot make any sense of that sentence. - Kevin.
|
Mon, 13 Jun 2005 09:40:41 GMT |
|
 |
Bill Cunningha #3 / 6
|
 k&r2 question
Quote:
> > On pg 27 1.8 of k&r2, Brian Kernighan writes, in the last sentence, "When > > necessary, it is possible to arrange for a function to modify a variable in > > a calling routine. The caller must provide the address of the variable to be > > set..." What's he saying? I've seen prototypes with the * operator, is > > scanf() an example of what he is saying? > Yes, that's right.
What's right? Quote: > > Is he talking about calling another > > function or a function calling on a variable in itself? > I cannot make any sense of that sentence.
I think you may have answered my question there. Quote: -----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World! -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
|
Mon, 13 Jun 2005 10:10:23 GMT |
|
 |
Kevin Easto #4 / 6
|
 k&r2 question
Quote:
>> > On pg 27 1.8 of k&r2, Brian Kernighan writes, in the last sentence, > "When >> > necessary, it is possible to arrange for a function to modify a variable > in >> > a calling routine. The caller must provide the address of the variable > to be >> > set..." What's he saying? I've seen prototypes with the * operator, is >> > scanf() an example of what he is saying? >> Yes, that's right. > What's right?
The way scanf takes addresses of variables as arguments so that it can modify variables in the calling routine; yes, it is an example of what he is saying. Here's another example: #include <stdio.h> void add_one_to(int *n) { *n = *n + 1; Quote: }
int main() { int i = 1; printf("%d\n", i); add_one_to(&i); printf("%d\n", i); return 0; Quote: }
- Kevin.
|
Mon, 13 Jun 2005 11:39:32 GMT |
|
 |
Fao, Sea #5 / 6
|
 k&r2 question
Quote:
> On pg 27 1.8 of k&r2, Brian Kernighan writes, in the last sentence, "When > necessary, it is possible to arrange for a function to modify a variable > in a calling routine. The caller must provide the address of the variable > to be set..." What's he saying? I've seen prototypes with the * operator, > is scanf() an example of what he is saying? Is he talking about calling > another function or a function calling on a variable in itself?
Other languages have the ability to pass variables /by/ /reference/. *Everything* in C is /by/ /value/. Use this code as an example, if you don't understand something, read the book again. --- Begin Code --- #include <stdio.h> void foo(int); void foo2(int *); int main(void) { int num = 23; printf("Before: %d\n", num); foo(num); printf("After: %d\n", num); printf("Before: %d\n", num); foo2(&num); printf("After: %d\n", num); Quote: }
void foo(int var) { var = 100; Quote: }
void foo2(int *var) { *var = 100; Quote: }
--- End Code --- *Output:* Before: 23 After: 23 Before: 23 After: 100 Sean -- Remove I-WANT-NO-SPAM from Email Address to reply.
|
Tue, 14 Jun 2005 10:08:13 GMT |
|
 |
John Smit #6 / 6
|
 k&r2 question
Bill Cunningham wrote | On pg 27 1.8 of k&r2, Brian Kernighan writes, in the last sentence, "When | necessary, it is possible to arrange for a function to modify a variable in | a calling routine. The caller must provide the address of the variable to be | set..." What's he saying? He's saying that "In C, all function arguments are passed ``by value.'' This means that the called function is given the values of its arguments in temporary variables rather than the originals." However, "When necessary, it is possible to arrange for a function to modify a variable in a calling routine" by passing in not the value but the *address* of the variable to be set. This address, naturally, is still passed by value. But that doesn't matter, because a pointer is a pointer, and dereferencing the value will have exactly the same effect as dereferencing the original. I've seen prototypes with the * operator, is | scanf() an example of what he is saying? Is he talking about calling another | function or a function calling on a variable in itself? He's talking about a function dereferencing a pointer to an object in the caller. JS
|
Wed, 15 Jun 2005 07:09:49 GMT |
|
|
|