How can I print the number of elements in this array?
Code:void GetSubString(const char *source) while ( *source ) { printf("%c", *source); ++source; } int main(void) { GetSubString("character"); return(EXIT_SUCCESS); }
How can I print the number of elements in this array?
Code:void GetSubString(const char *source) while ( *source ) { printf("%c", *source); ++source; } int main(void) { GetSubString("character"); return(EXIT_SUCCESS); }
Set a counter to 0 and increment it on each iteration of the loop.
I actually just posted a snippet to do exactly that in your other thread.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Is there a way to do it without declaring a variable. And if you must declare one it must be a pointer?
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
This homework is far from done. I was just demonstrating a simple technique (pointer arithmetic). I could have just said "use pointer arithmetic" and the OP would have googled and found the exact same example, or looked in his/her textbook and found it there. What's the difference? S/he still has to come to an understanding in order to incorporate the technique into the assignment.
I mean, figuring stuff out for yourself is always the best way. But if I picked up a programming book that had no example code at all in it, I would put it back down again.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I'd say it's 90% done now.
I would have suggested that the OP use a flowchart or pseudo code. It's not exactly hard to figure out. Upside: programmer learns how to do it and writes the code.I was just demonstrating a simple technique (pointer arithmetic). I could have just said "use pointer arithmetic" and the OP would have googled and found the exact same example, or looked in his/her textbook and found it there. What's the difference? S/he still has to come to an understanding in order to incorporate the technique into the assignment.
So very true, but a good book would thoroughly explain how these examples work, and had the OP read that, he/she would probably not ask this question...I mean, figuring stuff out for yourself is always the best way. But if I picked up a programming book that had no example code at all in it, I would put it back down again.
[MK27 gets back into chair and wipes "happy hour" spittle off keyboard]
Yes, a flowchart would be *the perfect* way to explain and/or carefully -- but not explicitly -- guide the OP toward the topic of "pointer arithmetic".
You know, maybe the reason I learned C so fast was because I wasn't in school so I could spend time at cboard and, in complete honesty, not be cheating on homework. I don't think being a real student of a real institution should be a such a penalty...
Pointer math comes in pretty handy sometimes. It may also be slightly faster altho I really have no idea one way or the other (I guess that might be an "ASM forum" question
Last edited by MK27; 08-18-2009 at 04:04 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
It would be slightly faster, except that making that difference disappear by converting the index version into the pointer version is an optimisation that can be expected of a good (or even mediocre?) optimising compiler.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)