Thread: Character handling help

  1. #16
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Oh crap

    Thanks for the correction Quzah

    I was actually experimenting, and I am not altogether clear on pointers yet, obviously. What I had wanted to do originally was to make the variable 'character' an array like this:
    Code:
    int main( void )
    {
       int character[10];  /* Initializes character  */
     
       printf( "Enter a character: ");    /* Asks for character and inputs it */     
       character = getchar();
    I cannot remember what happened when I tried that - something about a cast - anyway, I was mistaken when I thought that

    int character[10];

    is the same as

    int *character;

    Now is it correct to say that the array
    Code:
    int character[10];
    would be ok to use, as there is space set aside for it? This would not over write another part or memory, or would it?

  2. #17
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Quote Originally Posted by vandalay
    Sorry about that. I have to input a character from the keyboard and test it against the functions in the character handling library. I have all of the functions working, except the ones requiring a string (\n, \t, \r, and such). In those, the only thing recognized is \. I've tried everything I can think of, which isnt a whole lot. Thanks for your patience and help.
    Err... Let me point out to you that in order to catch \r and \n, you'd have to use two different functions...

    As far as I know, you can only get '\r' when you are using the getch() function (which is in the conio.h, and may be none standard...) and when you press enter.

    getchar(), on the other hand, will return '\n' when you press enter.

    I haven't tried it out myself, but would assume that in order to return '\t', you'd have to press the "tab" button, not type in \t.

  3. #18
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    [QUOTE=kermit]

    Code:
    int main( void )
    {
       int character[10];  /* Initializes character  */
     
       printf( "Enter a character: ");    /* Asks for character and inputs it */     
       character = getchar(); // Err... you're trying to change the address of the array here...
    This won't work either...

    Your confusion regarding arrays and pointers may caome from the fact that the character (without the square brackets and number) actually refers to the address at which the array is stored. character[i] however, would then refer to the ith element of the character array.

    However, in any case, Quzah is correct in the sense that you don't need arrays for what this program has to do in the first place.


    This code is an example showing how you can use a pointer to store the address of an array, as well as how to access a particular element in the array through a pointer.
    Code:
    int main(void)
    {
       int character[10], *pointer;
       pointer = character;
       pointer[0] = 5;
       printf("%d", character[0]);
       return 0;
    }

  4. #19
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by tzuchan
    Your confusion regarding arrays and pointers may caome from the fact that the character (without the square brackets and number) actually refers to the address at which the array is stored. character[i] however, would then refer to the ith element of the character array.
    If you have an array called character, something like this:

    Code:
    char character[10];
    char *p;
    p = character;
    Writing character and assigning it to p (as I did above) is not assigning the whole array as you pointed out. No doubt you meant that

    Code:
    p = character;
    
    /* is the same as */
    
    p = &character[0];
    When you use the name of the array without the brackets, it is only the address of the first element, not the entire array.

    Quote Originally Posted by tzuchan
    However, in any case, Quzah is correct in the sense that you don't need arrays for what this program has to do in the first place.
    Actually Quzah never mentioned that there was no need for arrays. He did say there was no place for pointers. Pointers and arrays are not the same thing. Furthermore, if you read the entire post, you will note that vandalay stated he needed to work with strings - heck I will quote him for you....

    Quote Originally Posted by vandaly
    Any ideas on how I can get the program to take whitespace characters and test them as well? I'm able to enter and check single characters, but it needs to check short strings also.
    How do you hold a string without an array?

    The only other thing I am wondering is why are you dredging up a 4 month old post? That usually is frowned upon on this board.

    ~/

    [edit]
    As an aside, I appreciate your attempt to clear this up for me, though I have learned a bit about arrays and their relationship to pointers in the past four months, and I definitely would not write code such as I did at that time.
    [/edit]
    Last edited by kermit; 03-29-2004 at 06:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character literals incorrectly interpreted
    By DL1 in forum C Programming
    Replies: 11
    Last Post: 04-05-2009, 05:35 PM
  2. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  3. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  4. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  5. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM