0

I want to declare a three dimensional array of strings where each row has two strings.

Here I am able to declare it:

char *szArray[][2] = {
    {"string1", "string2"},
    {"string3", "string4"},
    {"string5", "string6"},
    {0, 0}
};

I want to do the same thing but number of rows are dynamically allocated.

Here you can assume 1st dimension is dynamically decided. 2nd dimension is 2, 3rd dimension is 25.

Tried in many ways no luck :(.

Thanks in advance !

5
  • What kind of code have you tried thus far to get the dynamic allocation to work? Commented Feb 14, 2011 at 5:52
  • C and C++ are not the same language. Commented Feb 14, 2011 at 5:56
  • You've changed the entire tenor of the question with your edit <Jefromi>. I had assumed he wanted an either/or solution. Commented Feb 14, 2011 at 6:00
  • char * szIPList[2][25] ; szIPList[0] = new char[2][25]; vs 2005 compiler error : '=' : cannot convert from 'char (*)[25]' to 'char *[25]' Sorry Jefromi , i am trying with vs 2005 C++ compiler and it is cpp program. Commented Feb 14, 2011 at 6:03
  • [code] //@Thanks michael i found the way i guess. int n =5 ; char *** szData = (char *** )malloc(n * sizeof(char ** )) ; for(int i = 0 ; i < n ; i++ ) { szData[i] = (char ** ) malloc(2 * sizeof(char * )) ; for ( int j = 0 ; j < 2 ; j++ ) { szData[i][j] = (char *) malloc (25 * sizeof(char)); } } for( int i = 0 ; i < n ; i++ ) { sprintf(szData[i][0],"string%d",i); sprintf(szData[i][1],"string1%d",i); } for( int i = 0 ; i < n ; i++ ) { printf("%s\n",szData[i][0]); printf("%s\n",szData[i][1]); } [code] Commented Feb 14, 2011 at 6:15

4 Answers 4

3

First what you have in your question is not a 3-dimensional array of char but a 2-dimensional array of pointers to char. In C, pointers and arrays are not the same thing.

To answer your question, the simplest way in modern C, C99, is to use variable length arrays, VLA, for your purpose. For a 3-dimensional array you'd do

char szData[x][y][z];

where x, y and z are variables or expressions that are only determined at run time when you hit that declaration.

The inconvenience of VLA are that you have to be careful that they don't become too large for your stack, and that you have to initialize them by assignment to the individual entries (here by a nested for-loop).

To stay with your example of 2-d array of strings

char* szData[x][y];

and then you'd have to assign either individual strings to each of the pointers or to set them to 0.

for (size_t i = 0; i < x; ++i)
   for (size_t j = 0; j < y; ++j)
     szData[i][j] = 0;
Sign up to request clarification or add additional context in comments.

Comments

1
int n = 0 ;

printf("Enter the number of rows\n");

scanf("%d",&n);

char *** szData = (char *** )malloc(n * sizeof(char ** )) ;

//Allocate memroy for each row

for(int i = 0 ; i < n ; i++ )
{ 
    szData[i] = (char ** ) malloc(2 * sizeof(char * )) ;
    for ( int j = 0 ; j < 2 ; j++ )
    { 
        szData[i][j] = (char *) malloc (25 * sizeof(char));
    }
 }

 //Assign some data

 for( int i = 0 ; i < n ; i++ )
 { 
    sprintf(szData[i][0],"string%d", i); 
    sprintf(szData[i][1],"string1%d", i);
 }

 //print all the elements

 for( int i = 0 ; i < n ; i++ ) 
 { 
    printf("%s\n",szData[i][0]);
    printf("%s\n",szData[i][1]);
 } 

 //free memory here
 for(int i = 0 ; i < n ; i++ )
 {  
    for ( int j = 0 ; j < 2 ; j++ )
    { 
        delete szData[i][j];
    } 
 }

 for(int i = 0 ; i < n ; i++ )
 {  
    delete szData[i];
 }

 delete szData;

Comments

0

I didnt get any error:

#include <stdio.h>

int main(){
char *szArray[][2] = { {"string1", "string2"}, {"string3", "string4"}, {"string5", "string6"}, {0, 0} };

 printf("%s\n", szArray[0][0]);
 printf("%s\n", szArray[2][0]);

}

Here is the output:

$ gcc test.c
$ ./a.exe
string1
string5

But you cannot print szArray[3][0][0] because it is 0, if you want to initialize the value to whatever, you can set to "\0" instead of just 0

Comments

0

Ok, no compiler here to double check, but you can do this a number of ways. The most straight forward would be to declare a Vector of char[25][3] and let c++ do it for you. Something like Vector foo and then just push_back your dynamic elements. Without a compiler though, I'm not 100% certain that this would work as there are funky rules when it comes to multi-dimensional arrays. You can always undimensionalize your array as a first pass just to get it working as well - something like a Vector and start adding how ever you want. You can also do the vector of vector of char approach and have jagged arrays of chars where and of the 3 dimensions can be dynamic, and this would probably even be more memory efficent. Lots of choices here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.