Thread: How can I print the number of elements in this array?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    How can I print the number of elements in this array?

    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);
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Set a counter to 0 and increment it on each iteration of the loop.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    More requirments

    Is there a way to do it without declaring a variable. And if you must declare one it must be a pointer?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by steals10304 View Post
    Is there a way to do it without declaring a variable. And if you must declare one it must be a pointer?
    You can also just declare an int as a pointer and still use, but probably that is not the right idea.

    Basic "pointer arithmetic":
    Code:
    int stringLength (char *ptr) {
    	char *i=ptr;
    	while (*i !='\0') i++;
    	return i-ptr;
    }
    *i is synonymous with i[0].
    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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is the OP incapable of doing such a simple thing that you must point out a solution to such a basic problem?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    Is the OP incapable of doing such a simple thing that you must point out a solution to such a basic problem?


    I guess the OP could not, or the OP would not be asking that question in a "programming advice forum"*.

    *at least, I think of it as such.
    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

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then, is it not more common to point out that we don't do homework and refuse to answer?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    Then, is it not more common to point out that we don't do homework and refuse to answer?
    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

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MK27 View Post
    This homework is far from done.
    I'd say it's 90% done now.

    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 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 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.
    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...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by MK27 View Post
    You can also just declare an int as a pointer and still use, but probably that is not the right idea.

    Basic "pointer arithmetic":
    Code:
    int stringLength (char *ptr) {
    	char *i=ptr;
    	while (*i !='\0') i++;
    	return i-ptr;
    }
    *i is synonymous with i[0].
    This is a good way of finding the length. Till now i had been using counters but never pointers.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    I would have suggested that the OP use a flowchart




    [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...

    Quote Originally Posted by roaan View Post
    This is a good way of finding the length. Till now i had been using counters but never pointers.
    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

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Pointer math comes in pretty handy sometimes. It may also be slightly faster altho I really have no idea one way or the other
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by steals10304 View Post
    Is there a way to do it without declaring a variable.
    Of course, just use strlen( source );
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  2. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. Replies: 2
    Last Post: 08-03-2003, 10:01 AM