Thread: Incrementing Pointers Along arrays

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    Incrementing Pointers Along arrays

    Code:
    char *key;
    char blah[10];
    
    gets(blah);
    key = blah;
    
    //How would I get a specific character from key?
    Ie. blah is qwerty

    I want to go through, using key, to get, say, the third character.

    how would I go about doing that? I was thinking putchar(key[2]); but that doesn't seem to be the case.

    Any help for this n00b would be appreciated

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >I was thinking putchar(key[2]); but that doesn't seem to be the case.
    Why not? It's perfectly legal
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *key;
        char blah[10];
    
        gets(blah); /* gets is never correct, use fgets */
        key = blah;
    
        putchar(key[2]);
    
        return 0;
    }
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2
    Yeah, it does work, ignore this thread. My brain said putchar, my body put putc, my Eyes read putc, and the error message spoke of macros.... lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 10:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM