Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using C copy a 1D char array into 2D char array

I am trying to copy a 1D array of Strings into a 2D array of strings in C.

I was able to achieve this with integer
enter image description here

//Here is what I tried for integers.
        int main() 
    { 
        int arr[3][3];
        int arr2[9]={1,2,3,4,5,6,7,8,9}; 
    
        int i,j,k=0; 
    
        for(i=0; i<3;i++){
            for(j=0; j<3;i++){
              arr[j][i] = arr2[i];
                //rintf("%d\n",arr2[i]);
            }
            
        }
    
     for(i=0; i<3; i++) { 
        for(j=0; j<3; j++) 
          printf("%2d  ", arr[j][i]); 
        printf("\n"); 
      } 
    
        return 0; 
    } 

I changed my data to char and I tried to run the same code I got a segmentation error.
Here is what I have tried so far and it didnt work. error :Segmentation fault (core dumped)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 #include<stdio.h> 
#include<string.h> 
 
int main() 
{ 
    char *d[3][3];  // Destination array 

    char *s[9]={"orange","apple","table","chair","cable","TV", "124","HI"};   // Source 1 Day array

    int i,j,k=0; 

    for(i=0; i<3;i++){
        for(j=0; j<3;i++){
            
          strcpy(d[j][i], s[i]);
           
        }
            }

 for(i=0; i<3; i++) { 
    for(j=0; j<3; j++) 
      printf("%s  ", d[j][i]); 
    printf("\n"); 
  } 

    return 0; 
} 

I have made some adjustment and now it print some weird strings

#include<stdio.h> 
#include<string.h> 
 
int main() { 

    char d[3][3] ={0};  // Destination array 

    char s[9][8]={"orange","apple","table","chair","cable","TV", "124","HI"};   // Source 1 Day array

    int i,j,k=0; 
    
    for(i=0; i<3;i++){
        for(j=0; j<3;j++){

            d[j][i] = *s[i];
           
        }
            }
    
    for(i=0; i<3; i++) { 
        for(j=0; j<3; j++) 
            printf("%s  ", &d[j][i]); 
            printf("\n"); 
    } 

    return 0; 
} 

enter image description here

>Solution :

This for loop

    for(i=0; i<3;i++){
        for(j=0; j<3;i++){
          arr[j][i] = arr2[i];
            //rintf("%d\n",arr2[i]);
        }
        
    }

is incorrect. In the inner loop there are used the same elements arr2[i] where i is changed from 0 to 2 inclusively.

You need to write

    for(i=0; i<3;i++){
        for(j=0; j<3;i++){
          arr[j][i] = arr2[ 3 * i + j];
        }
    }

Another way to write loops is the following

    for ( i = 0; i < 9; i++ )
    {
        arr[i / 3][i % 3] = arr2[i];
    }

As for arrays of pointers of the type char * then the nested loops will look similarly

for(i=0; i<3;i++){
    for(j=0; j<3;i++){
        
      d[i][j] = s[ 3 * i + j];
       
    }
}

provided that the array s is declared like

char * s[9]={"orange","apple","table","chair","cable","TV", "124","HI"};   

And to output the result array you need to write

 for(i=0; i<3; i++) { 
    for(j=0; j<3; j++) 
      printf("%s  ", d[i][i]);
                     ^^^^^^^ 
    printf("\n"); 
  } 

As for your last program then it can look like

#include<stdio.h> 
#include<string.h> 
 
int main( void ) 
{ 

    char d[3][3][8] ={0};  // Destination array 

    char s[9][8]={"orange","apple","table","chair","cable","TV", "124","HI"};   // Source 1 Day array

    for( size_t i = 0; i < 3; i++ )
    {
        for ( size_t j = 0; j < 3;j++ )
        {
            strcpy( d[j][i], s[3 * i + j] );
           
        }
    }
    
    for ( size_t i = 0; i < 3; i++ ) 
    { 
        for ( size_t j = 0; j < 3; j++ )
        { 
            printf( "%s  ", d[i][j] ); 
        }
        putchar( '\n' ); 
    } 

    return 0; 
} 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading