Thread: Can you initialize and assign pointers in the same instance?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Can you initialize and assign pointers in the same instance?

    1) My question refers to "char *charPtr[ 3 ]". I guess it's a dumb question it seems obvious you can't. But is there anyway to assign pointers through using "{ }" at least?

    Code:
    /*
    Write a program that inputs several lines of text and a search character and 
    uses function strchr to determine the total occurrences of the character in 
    the lines of text.
    */
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main()
    {
      char string1[ 50 ] = { };
      char string2[ 50 ] = { };
      char string3[ 50 ] = { };
      char character;
      char *charPtr[ 3 ] = { string1, string2, string3 };
      int counter = 0;
      int i;
    
      printf( "Enter 3 lines:\n" );
      scanf( "&#37;s", string1 );
      scanf( "%s", string2 );
      scanf( "%s", string3 );
    
      printf( "Enter character: " );
      character = getchar();
    
      for (i = 0; i < 3; ++i) {
        while ( strchr( charPtr[ i ], character ) ) {
          ++counter;
        }
      }
    
      printf( "%d\n", counter );
    
      return 0;
    }

    2) Also my getchar doesn't seem to be working?
    Code:
    root[~]# ./a.out
    Enter 3 lines:
    d
    d
    d
    Enter character: 0
    root[~]#
    It enters 0 on its own.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    FAQ > Explanations of... > Definition of EOF and how to use it effectively

    [edit]If you don't move the pointer resulting from the strchr, if you find what you're looking for you will keep finding it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not sure what you're asking with #1. The line works, and does what you want it to do. The usual idiom would be to make it a two-dimensional array of char:
    Code:
    char charPtr[3][50];
    Also, isn't your while loop infinite? strchr doesn't change your string or move a pointer or anything.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    182
    I was under the impression that your idiom would only work in setting up a read-only string.

    The reason I'm confused with 1) is because it's char *charPtr. I would think it expects me to pass a dereferenced value, not point to a memory address. Since the presence of * typically indicates that. But this isn't a typical situation( for me at least ).
    Last edited by yougene; 03-09-2008 at 09:37 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by yougene View Post
    I was under the impression that your idiom would only work in setting up a read-only string.
    You've got this exactly backwards, I think:
    Code:
    char *charPtr = "I'm a read-only string";
    is accurate. char arrays are always writable.

    Quote Originally Posted by yougene View Post
    The reason I'm confused with 1) is because it's char *charPtr. I would think it expects me to pass a dereferenced value, not point to a memory address. Since the presence of * typically indicates that. But this isn't a typical situation( for me at least ).
    Pass who a dereferenced value? The declaration says that *charPtr is a char, or that after dereferencing you have a char, so charPtr is a memory address. The name of an array (such as string1), by itself, decays into its starting memory address. So that's all good. And of course scanf needs a memory address in order to work at all.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    182
    You've got this exactly backwards, I think:
    I'm probably getting things confused. I'm thinking of pointer arrays of strings.

    Pass who a dereferenced value? The declaration says that *charPtr is a char, or that after dereferencing you have a char, so charPtr is a memory address. The name of an array (such as string1), by itself, decays into its starting memory address. So that's all good. And of course scanf needs a memory address in order to work at all.
    Yes, but the way you usually assign memory address to a pointer would be
    charPtr = string1

    *charPtr = string1 (which is what I seem to have) isn't supposed to assign a memory address to the pointer. It is supposed to pass a value into the memory located at that address.



    Also, isn't your while loop infinite? strchr doesn't change your string or move a pointer or anything.
    strchr is supposed to return NULL when it can't find any more instances of the character, in effect terminating the while loop.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The appearance of initialization is different than that of assignment.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by yougene View Post

    *charPtr = string1 (which is what I seem to have) isn't supposed to assign a memory address to the pointer. It is supposed to pass a value into the memory located at that address.
    You don't have that, or anything vaguely similar to that, anywhere in your code. charPtr[0] = string1, yes. But charPtr[0] is a char * (that's how you defined the array after all), so you want it to hold a memory address, and conveniently string1 is a memory address.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    182
    Ahh ok. So what you are saying is because it's getting initialized I can pretend the * isn't there in terms of initializing a pointer address? After all it's a little silly to pass a value when there is no memory space to pass it to. I had a feeling that's what tabstop was saying but I wanted to make sure.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    182
    Quote Originally Posted by tabstop View Post
    You don't have that, or anything vaguely similar to that, anywhere in your code. charPtr[0] = string1, yes. But charPtr[0] is a char * (that's how you defined the array after all), so you want it to hold a memory address, and conveniently string1 is a memory address.
    char *charPtr[ 0 ] = string1 looks awfully similar to *charPtr[ 0 ] = string1 which is where my confusion stems from. I think I see what you are saying though as I indicated in my above post. Please let me know if I'm wrong though.

    Thanks for the help guys.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by yougene View Post
    Ahh ok. So what you are saying is because it's getting initialized I can pretend the * isn't there in terms of initializing a pointer address? After all it's a little silly to pass a value when there is no memory space to pass it to. I had a feeling that's what tabstop was saying but I wanted to make sure.
    You are initializing the variable charPtr[3]. There is no * there to ignore.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    182
    I'm not sure what you mean by "There is no * there to ignore." It's clearly in my source code.
    Code:
      char *charPtr[ 3 ] = { string1, string2, string3 };


    Keep in mind that I'm new to the language of programming( jargon ). What you say may not have the same precise meaning to me that it has to you. You may have to dumb it down for us mere mortals. ; )

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by yougene View Post
    I'm not sure what you mean by "There is no * there to ignore." It's clearly in my source code.
    Code:
      char *charPtr[ 3 ] = { string1, string2, string3 };


    Keep in mind that I'm new to the language of programming( jargon ). What you say may not have the same precise meaning to me that it has to you. You may have to dumb it down for us mere mortals. ; )
    If you did
    Code:
    int bob = 5;
    you wouldn't worry about the int would you? It's not part of the variable name. Nothing has changed here: the variable name is still charPtr; the type is not part of the variable name.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    182
    Ahh ok, so the * is in fact part of the data type
    char *somePtr;
    Gotchya, thanks

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    182
    I know getchar() isn't a good choice but I actually tried scanf before I tried this.. Oddly, I get the exact same result no matter which one I use.

    Code:
    root[~]# ./a.out
    Enter 3 lines:
    d
    d
    d
    Enter character: 0
    root[~]#
    That 0 isn't entered by me, it's entered on it's own. Why is that?


    Here's the code with scanf.
    Code:
    /*
    Write a program that inputs several lines of text and a search character and 
    uses function strchr to determine the total occurrences of the character in 
    the lines of text.
    */
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main()
    {
      char string1[ 50 ] = { };
      char string2[ 50 ] = { };
      char string3[ 50 ] = { };
      char character = " ";
      char *charPtr[ 3 ] = { string1, string2, string3 };
      int counter = 0;
      int i;
    
      printf( "Enter 3 lines:\n" );
      scanf( "%s", string1 );
      scanf( "%s", string2 );
      scanf( "%s", string3 );
    
      printf( "Enter character: " );
      scanf( "%c", &character );
      //  character = getchar();
    
      for (i = 0; i < 3; ++i) {
        while ( strchr( charPtr[ i ], character ) ) {
          ++counter;
        }
      }
    
      printf( "%d\n", counter );
    
      return 0;
    }

    [edit]If you don't move the pointer resulting from the strchr, if you find what you're looking for you will keep finding it.
    Hmm, you're right. I'll have to do something with that as soon as my program can actually get to it. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM