Thread: 2D arrays and double pointers

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    16

    Question 2D arrays and double pointers

    Hi All,

    The following code prints out values of a 2 D array in main as well as in the function printvalues. However, fd->mychar[i] = newptr[i]; throws an error. I understand that a ** myarray is not the same as myarray [][] and I am trying to figure out a solution to this. My problem is, in my real program (I reproduce the same problem below with minimal code), I am modifying pieces of pre-existing code of a 10,000+ lines, so the argument to printvalues(char **) cannot be changed, it has to be a double pointer, as this is how its used in several places in the code (and I dont want to break anything), and, structure mystruct is large with multiple 2D arrays such as mychar, and since I want to always ensure a contiguous block of memory is allocated to mystruct, I cannot use pointers like so: char *mychar [20].

    With these restrictions, is it still possible to make this work? How? Thanks!

    Code:
    typedef struct mystruct{
      char mychar [20][20];
    }mystruct_t;
    
    void printvalues ( char ** newptr){
      int i;
      mystruct_t * fd;
      for (i=0;i<3;i++){
       printf("My value is %s\n", newptr[i]); // prints fine if following 2 lines commented
        fd->mychar[i] = newptr[i];               // error: incompatible types in assignment
    
        printf("My value is %s and in struct %s\n", newptr[i], fd->mychar[i]);
      }
    }
    int main (int argc, char **argv){
     int i;
     char * abc[5] = {"123", "456", "789"};
    
     for (i=0;i<3;i++){
      printf("My value is %s\n", abc[i]);
     }
     printvalues(abc);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by looza View Post
    Code:
    typedef struct mystruct{
      char mychar [20][20];
    }mystruct_t;
    
    void printvalues ( char ** newptr){
      int i;
      mystruct_t * fd;
      for (i=0;i<3;i++){
       printf("My value is %s\n", newptr[i]); // prints fine if following 2 lines commented
        fd->mychar[i] = newptr[i];               // error: incompatible types in assignment
    You are trying to assign an array there. You can't do that. You will need to loop through each element in that arrray and copy it by hand:
    Code:
    for( row = 0; row < ROWS; row++ )
        for( col = 0; col < COLS; col++ )
            fd->mychar[ row ][ col ] = newptr[ row ][ col ];

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    Thanks Quzah,

    This code segfaults :-(

    Code:
    
    typedef struct mystruct{
        char mychar [5][4];
    }mystruct_t;
    
    void printvalues ( char ** newptr){
       int i,j;
       mystruct_t * fd;
       for (i=0;i<3;i++){
          for(j=0;j<3;j++){
          fd->mychar[i][j] = newptr[i][j];
          printf("%s:%s\t", newptr[i][j], fd->mychar[i][j]);
          }
          printf("\n");
       }
    }
    int main (int argc, char **argv){
       int i;
       char  abc[5][4] = {"123", "456", "789"};
    
       for (i=0;i<3;i++){
          printf("My value in main is %s\n", abc[i]);
       }
       printf("");
       printvalues((char ** )abc);
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When you type "newptr[i]" it is interpreting the value as a pointer. But it isn't, since it's just the character data "12" or whatever.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by looza View Post
    This code segfaults :-(
    Code:
    int main (int argc, char **argv){
       int i;
       char  abc[5][4] = {"123", "456", "789"};
    
       for (i=0;i<3;i++){
          printf("My value in main is %s\n", abc[i]);
       }
       printf("");
       printvalues((char ** )abc);
    }
    You should read this, and actually probably this whole thing.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having major trouble with double arrays using pointers
    By omegasli in forum C Programming
    Replies: 3
    Last Post: 04-09-2011, 08:49 PM
  2. double pointers
    By esi in forum C Programming
    Replies: 1
    Last Post: 04-14-2007, 04:06 PM
  3. Double Pointers
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 04-13-2002, 03:57 AM
  4. double pointers?
    By Kohatian 3279 in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2002, 04:16 AM
  5. more help on double pointers
    By drharv in forum C Programming
    Replies: 6
    Last Post: 03-01-2002, 10:31 AM