Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to say like this in C :

python
Python
[[0],[1],[0,1]]


I tried to state that its two dimensional array, arr[x][y] but i'm stuck coz it requires same length to be used in place of y even if i use 3 d array i have same problem. can i state this in c anyways ?

Thanks
Help greatly appreciated.

What I have tried:

I tried to state that its two dimensional array, arr[x][y] but i'm stuck coz it requires same length to be used in place of y even if i use 3 d array i have same problem. can i state this in c anyways ?
Posted
Updated 21-Apr-16 0:42am

1 solution

I'd use:
C++
int *myArray[x];
And then populate it with my variable lengths:
C++
for (i = 0; i < x; i++)
   {
   myArray[i] = (int*) malloc((i + 1) * 3 * sizeof(int));
   }

[edit]I HATE MARKDOWN! :mad: [/edit]
[edit]Oops! Forgot the sizeof...[/edit]
 
Share this answer
 
v4
Comments
ALEX_CROME 21-Apr-16 6:56am    
@originalGriff Its unclear to me what the code is doing ? as far as i can see you are defining array of length x. Then its not clear how would u assign {0} , {1}, {0,1} to that array
OriginalGriff 21-Apr-16 7:01am    
Its an array of "pointer to int" values: which means that the x dimension is fixed, but each element of the array points to a different area of memory which holds integers, and each area can be of a different length.
The example gave gives you 3 ints for myArray[0], 6 for myArray[1], 9 for myArray[2], and so on.
Try it: see what happens in the debugger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900