Thread: array of pointers/pointer arithmetic

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by itCbitC View Post
    imho by stepwise dissecting the expression *(*(a + 0) + 0) as in
    a + 0 is a pointer to the first element of a ie a + 0 == &a[0].
    *(a + 0) applies dereferencing to obtain a[0].
    *(a + 0) + 0 points to the same place that a[0] is pointing to.
    We were doing fine up to right here. *(a+0) is a character a[0], adding 0 to it gives us the same character.
    Quote Originally Posted by itCbitC View Post
    *(*(a + 0) + 0) accesses the object that a[0] is pointing to ie 'A' in this case.
    And this is just an error.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by tabstop View Post
    We were doing fine up to right here. *(a+0) is a character a[0], adding 0 to it gives us the same character.

    And this is just an error.
    care to explain if you please what you mean by the above statement??

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by itCbitC View Post
    care to explain if you please what you mean by the above statement??
    What it means is that I can't read. We have an array of char *, not of char as I had originally read it. So yes what you said is what we have. Sorry.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    ah ha! yes I had made the same mistake earlier

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    7
    Indeed ItC... i think i was mixed up with the notation show at the bottom of this fragment:

    Code:
    char *a[2];
    	char *b[2];
    	char **all[2] = {a, b};
    	a[0]= (char *)malloc(20);
    	a[1]= (char *)malloc(20);
    	b[0]= (char *)malloc(20);
    	b[1]= (char *)malloc(20);
    
    	*(a + 0) = "AEI";
    	*(a + 1) = "OUY";
    
    	printf("string: %s\n", a[0]);
    	printf("string: %s\n", a[1]);
    	printf("char: %c\n", *(*(a + 0) + 0));
    	printf("char: %c\n", *(*(a + 0) + 1));
    	printf("char: %c\n", *(*(a + 0) + 2));
    
    	*(*(all + 1) + 0) = "DOG";
    	*(*(all + 1) + 1) = "CAT";
    
    	printf("string: %s\n", b[0]);
    	printf("string: %s\n", b[1]);
    	printf("char: %c\n", *(*(*(all + 1) + 0) + 0));
    	printf("char: %c\n", *(*(*(all + 1) + 0) + 1));
    	printf("char: %c\n", *(*(*(all + 1) + 0) + 2));
    	printf("char: %c\n", *(*(*(all + 1) + 1) + 0));
    	printf("char: %c\n", *(*(*(all + 1) + 1) + 1));
    	printf("char: %c\n", *(*(*(all + 1) + 1) + 2));
    Which Outputs:

    string: AEI
    string: OUY
    char: A
    char: E
    char: I
    string: DOG
    string: CAT
    char: D
    char: O
    char: G
    char: C
    char: A
    char: T

    Many hours later I have "completed" the main program with one annoying warning about passing non const data to const parameters...

    Code:
    int main()
    {
    void test(const char * const *[]);
    char *a[MAX];
    char *b[MAX];
    char *c[MAX;
    char **all[MAXMEMBER] = {a, b, c};
    test(all);
    }
    
    void test(const char * const *total[])
    {
    ; //modify individual characters or the string as a whole is an error... but still a warning
    }
    Which gives me a "passing argument 1 of ‘test’ from incompatible pointer type" in eclipse and a similar warning in gcc. I am thinking some kind of cast is appropriate here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM