Thread: Array names and char*'s

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    60

    Array names and char*'s

    I was just wondering about character array offsets and
    array names. If I declare:
    char* a;
    char* theString[]="Hello World";
    a=theString;
    a++;

    Will a[0] now show 'e'?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A minor correction:
    Code:
    char* a; 
    char theString[]="Hello World";  // Notice no '*' after char
    a=theString;
    a++;
    The variable a is not an array so you really shouldn't be using the [] array indexing scheme. After the above code is run, a will point to the second element of theString which means that *a will equal 'e'.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Look at my program, in my case, it print's 'e':
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *ptrStr;
        char *str = "Hello World";
        
        ptrStr = str;
        printf("%c\n",*(++ptrStr));
        getchar();
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    Thanks. This means array names are much more powerful than I thought!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-16-2008, 12:15 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM