Thread: Pointer of arrays - how do I access their elements?

  1. #1
    Unregistered
    Guest

    Pointer of arrays - how do I access their elements?

    Please someone help me!

    Suppose a pointer of arrays is declared like this:

    char *array[3]

    and it is initialized like this, for example:

    char *array[3]={"hello", "world", "man"};

    When we print the string by using printf inside a loop, it looks like this:

    array[0]=hello
    array[1]=world
    array[2]=man

    Question:
    How do I actually accessed the individual elements of array[0] i.e the letters h, e, l, l and o one by one by using a loop?? Same goes to the other array.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    1. It's an array of pointers, not a pointer of arrays

    2. array[0], for example, returns a pointer to "hello".. so let's say you did this...
    char * c = "hello"
    c = "hello"
    c[0] = 'h'
    c[1] = 'e'
    c[2] = 'l'

    so...
    array[0] = "hello"
    array[0][0] = 'h'
    array[0][1] = 'e'
    array[0][2] = 'l'

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    Talking Thank you again, pal!

    Dear QuestionC,

    Thank you for anticipation. I really really appreciate you aid and thanks a lot for the error correction!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Pointer Arrays
    By valaris in forum C Programming
    Replies: 3
    Last Post: 07-27-2008, 01:20 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. calculating elements of arrays
    By Kristin in forum C Programming
    Replies: 2
    Last Post: 11-02-2003, 10:38 AM