Thread: pointer to array of arrays

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    pointer to array of arrays

    I wrote this code and have a pointer that I want to point to another character array of arrays. But it says
    Code:
    :24: error: incompatible types in assignment
    here is my code
    Code:
    #include <stdio.h>
    #define SIZE 3
    
    char array[SIZE+4][SIZE+2];
    char *view[SIZE+4];
    
    void input(void){
            int x;
            for(x=0;x<SIZE;x++){
                    fgets(&array[x][2],sizeof(array[x]),stdin);
            };
    }
    
    void print(void){
            int x;
            for(x=0;x<SIZE;x++){
                    printf("%s\n",array[x]);
            }
    }
    
    int main(void){
            int x,y;
            input();
            view=array;
            for(y=0;y<SIZE+2;y++){
                    array[SIZE+3][y]='\0';
                    array[0][y]='\0';
            }
            for(x=0;x<SIZE+4;x++){
                    array[x][0]='\0';
                    array[x][SIZE+1]='\0';
            }
            for(x=1;x<SIZE+1;y++){
                    view[x][1]=array[x][SIZE+1];
                    view[x][SIZE+2]=array[x][1];
            }
            print();
            return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > view=array;
    If you want to do this

    Then you need this
    char (*view)[SIZE+4];

    Watch the () birdies.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    ah yes,
    Code:
    char (*view)[SIZE+4];
    is a pointer to a character array of SIZE+4 rows, and
    Code:
     char *view[SIZE+4];
    creates a single array of SIZE+4 pointers to characters?
    Also. I changed the code to
    Code:
    char (*view)[SIZE+4]
    and I get the same error? What am i doing wrong?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Sorry, got the wrong dimension in there, it should be +2, not +4

    Code:
    void foo ( ) {
        char a[10][20];
        char (*pa)[10][20];
        char (*pb)[20];
        char *pc;
        pa = &a;
        pb = &a[0];     /* pb = a; also works */
        pc = &a[0][0];
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. How to determine length of pointer array
    By Dr.Zoidburg in forum C Programming
    Replies: 12
    Last Post: 02-16-2009, 06:52 PM
  3. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM