array of pointers/pointer arithmetic

This is a discussion on array of pointers/pointer arithmetic within the C Programming forums, part of the General Programming Boards category; Indeed ItC... i think i was mixed up with the notation show at the bottom of this fragment: Code: char ...

  1. #16
    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.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're throwing away (at least) 40 bytes of memory also -- those malloc's that you do at the top vanish when you do *(a+0) = "AEI", as this changes the pointer to point to read-only memory and not the 20 characters you allocated. If you want to copy strings, you use strcpy.

  3. #18
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Now, now, after correctly using all those complicated pointer initializations instead of indexing you are getting the type of the argument to test() wrong.
    Mull it over and it should be clear to you soon and yes you are wasting 80 bytes of heap storage by not using them.

  4. #19
    Registered User
    Join Date
    Nov 2008
    Posts
    7
    Quote Originally Posted by tabstop View Post
    You're throwing away (at least) 40 bytes of memory also -- those malloc's that you do at the top vanish when you do *(a+0) = "AEI", as this changes the pointer to point to read-only memory and not the 20 characters you allocated. If you want to copy strings, you use strcpy.
    whoops... i typed the example in haste and didn't notice that... In the main program i use the malloc to allocate the memory so I can copy data from a file into different char arrays byte to byte.... but i see how using strncmp would be a whole lot quicker... wish i'd known that earlier :P

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21