![]() |
|
#1
|
|||
|
|||
Help with a simple programHi there, I'm a newbie!
I'm writing a very small program as follows, to count even and odd numbers. The loop and so on seems to work, but after I enter all ten integers, I get 0 for both the even and the odd counts. I guess I'm getting confused with pointers or something, like maybe I'm incrementing a memory address rather than an actual variable (I'm a little confused about all these * and & things!). Any help is greatly appreciated! Cheers, Matt CPP / C++ / C Code:
Last edited by LuciWiz : 11-Feb-2005 at 07:54.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#2
|
|||
|
|||
|
Quote:
1. Change this: CPP / C++ / C Code:
to this CPP / C++ / C Code:
The function scanf() takes an address as an argument (so that it can change the value of whatever variable you are scanning into). 2. Due to operator precedence, this CPP / C++ / C Code:
is the same as CPP / C++ / C Code:
That is, it fetches the int in memory pointed to by "even", then it increments the function's local copy of the pointer. (It doesn't do anything with the value from memory, and some compilers give a message something like "Code has no effect".) Now, if you want to increment the value in memory pointed to by "even", you must override the operator precedence. Here is a way to do it: CPP / C++ / C Code:
This says: fetch the int in the memory location pointed to by "even". Increment the int and store the new value back into the same place. Regards, Dave |
|
#3
|
|||
|
|||
|
Thank you - that's excellent, it works.
Thanks also for the explanation of what is actually going on, too. That will really help in the future! Cheers, Matt :-D |
|
#4
|
|||
|
|||
|
Quote:
As programming languages go, C is rather a simple language. One of the first things that typically confuses people (not just beginners) is the somewhat bewildering number of operators. Instead of memorizing precedence tables, I usually recommend that people put in extra parentheses in all but the simplest cases (multiplication and addition, for example). Real "studly" C programmers think it's a sissy thing to do, and in fact some think it's a macho thing to try to put an entire program in one incredibly complex expression statement (with no parentheses). By the way, I misspoke slightly in my description. I said, "due to operator precedence." I should have said, "due to operator precedence and associativity rules." The pointer dereference "*" and the postincrement "++" actually have the same precedence, but the associativity (order in which the operators are bound) is right-to-left. Now do you see why I said that people sometimes get confused? Parentheses are less confusing to me than "precedence and associativity rules." Regards, Dave |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [TUTORIAL] Calling an external program in C (Linux) | dsmith | C Programming Language | 4 | 22-Apr-2005 14:30 |
| Help with a simple program. | bluedragon27 | C Programming Language | 1 | 16-Nov-2004 16:40 |
| fltk-2.0 cvs | Plumb | FLTK Forum | 20 | 13-Nov-2004 08:10 |
| Need help with a C program (Long) | McFury | C Programming Language | 3 | 29-Apr-2004 21:06 |
| How to write simple program like ........? | laputa9000 | C++ Forum | 3 | 29-Oct-2003 13:09 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The