String and Pointers

This is a discussion on String and Pointers within the C Programming forums, part of the General Programming Boards category; Lets say I have this code, [code] void rawr(char *st1); int main(void) { char str1[] = "Rawr Rawr Rawr"; rawr(str1); ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    91

    String and Pointers

    Lets say I have this code,
    [code]
    void rawr(char *st1);

    int main(void) {
    char str1[] = "Rawr Rawr Rawr";
    rawr(str1);
    }

    void rawr(char *st1) {

    */How do I grab the first two "rawrs" and make it so that it loops and says things like
    / Rawr
    / awr
    / wr
    / r
    / Thats the output I'm trying to do... just fun
    /*


    Can anyone help me with this? Reading the online lessons, but pointers and arrays... I dunno. haha, need some help. Any help will be appreciated, thank you.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by dlwlsdn View Post
    Lets say I have this code,
    [code]
    void rawr(char *st1);

    int main(void) {
    char str1[] = "Rawr Rawr Rawr";
    rawr(str1);
    }

    void rawr(char *st1) {

    */How do I grab the first two "rawrs" and make it so that it loops and says things like
    / Rawr
    / awr
    / wr
    / r
    / Thats the output I'm trying to do... just fun
    /*
    [/code]

    Can anyone help me with this? Reading the online lessons, but pointers and arrays... I dunno. haha, need some help. Any help will be appreciated, thank you.
    Code:
    void rawr(const char *st1)
    {
      for(;*st1;st1++)
        printf("%s\n", st1);
    }

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Hmm... can you explain that to me please, msater5001. And what does "const" mean (constant?)

    and why did you use for(;*st1; st1++)? instead of for(st1=0;*st1;*st1++)?

    Sorry, I'm very new to coding.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    void
    /*
     * Return nothing
     */
    
    rawr
    /*
     * A foolishly named function name.
     */
    
    (
    /*
     * The beginning of your parameter list.
     */
    
    const
    /*
     * const is like a hot 16 year old. Look but don't touch.
     * You are promising the caller that you will never
     * change the contents of something, but you are going
     * to look at it.
     */
    
    char *
    /*
     * This is a pointer to a character sized block of data
     */
    
    st1
    /*
     * This is the name of the variable.
     */
    
    )
    /*
     * This denotes the end of your parameter list.
     */
    
    {
    /*
     * Begin your function definition.
     */
    
    /* Ok, so here is the part that is confusing you the most.
     *
     * You want to start with the string as it is now. So do NOT put
     * st1 = 0; That is just plan wrong.
     *
     * You will move the pointer up one address each time you loop. Which will
     * result in "the next letter" being pointed to.
     *
     * The loop exists once you reach a '\0' character.
     */
      for(;*st1 != '\0';st1++) /* I changed the condition to make it clearer for you */
        printf("%s\n", st1);
    
    
    }
    /*
     * End your function definition.
     */

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    You are superb... you are godly at C coding. I can see why your called "master"

    thanks a lot. You're pretty awesome.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Do you understand pointers very well just yet? If pointers are your area of confusion here, I can certainly show you less pointer-esque ways of doing this. But I won't sugar coat it, any way of doing this requires some pointers and all of them are less efficient than this way.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Hmm, i understand pointers and how they dereference things. And I was wondering about '\0', just making sure, I think I read it somewhere in the lessons. And It was talking about how it means "NULL" or something like that. Just making sure I understand '\0' and pointers. If you think my understanding on pointers and address and stuff like that is drifting in a different way, please let me know.

    Thank you for your help again.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    0 == '\0'

    It is an ascii character. It is the one with a value of zero. It is called NUL. NULL means something a little different. I know its confusing, but I didn't make this stuff up. NUL is a character and NULL means a pointer to nothing.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    Many refer to them as NULL characters or null characters, so beware, though.
    I always just refer to them as '\0'. Simple.
    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.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    \0 is less typing than NUL so it sounds like a winner to me

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    cool, makes sense now. alrighty then. So for(; *s1 != '\0'; s++) and for(; *s1; s++) are the same thing?

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    You could say that. Indeed, many C devs assume this is true. If '\0' == 0, then it would indeed hold true.
    But I am not certain that '\0' will always be guaranteed to be equal to 0.
    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.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Nice. What about writing this part in arrays?

    like what i have, which is not
    Code:
    	int x;
    	for(x=0;x;x++)
    	{
    		printf("%c\n", st1[x]);
    		fflush(stdout);
    Now, something is wrong here, but I can't get my mind around it. i was wondering if you can help me with that.. Thanks.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    You should re-think that condition for the loop. It will not do what you want it to do.
    The loop is wrong, as well, since x is just a counter. It must count to some defined end (currently it just breaks when x = 1). Presumably the length of the string?
    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.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You also are doing something fundamentally different in your code. You would be printing

    R
    a
    w
    r
    \0
    ... <- garbage until you seg fault...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Pointers
    By mbuster in forum C Programming
    Replies: 3
    Last Post: 05-28-2008, 09:39 AM
  2. String Concatenation Using Pointers No strcat()
    By oviang in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 09:31 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. string pointers
    By David Korb in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2001, 12:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21