top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can I declare this type of array in C?

+2 votes
561 views

I need to get value from the below table

{  {1,25,{45,6,4},5,8,{10,15},1,4,0,0},    
   {0,25,{6,4},{1,2,5,8},5,{10,15},1,{7,5,2,6,11,15},4,0},    
   {0,2,3,4,5,8,{10,15},1,{1,2,3,4,5,6,7,8,9},0}
}

first index is fixed to 3 and in each index no of elements fixed to 10, but inside of some index no of elements is not fixed ... so how can i get this value. how to declare it .

I did like array_x[3][10][];

but its not working .. please suggest

posted Jan 28, 2017 by Chakri

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

In C least indexes are to be given in the definition like

int a[2][]={{1}, {1,2}}; // invalid statement 
int a[][2]={{1}, {1,2}}; // valid statement 

So in your case x[3][10][] is a invalid statement however x[][10][5] is valid.

PS: I hope I understood your problem correctly, in case not please comment and I will love to help.

answer Jan 29, 2017 by Salil Agrawal
hi thanks for your reply, but please help me how can i declare my third index because its not fixed.

for example

{  {1,25,{45,6,4},5,8,{10,15},1,4,0,0},    
   {0,25,{6,4},{1,2,5,800},5,{10,15},1,{7,5,2,6,11,15},4,0},    
   {0,2,3,4,5,8,{10,15},1,{1,2,3,4,5,6,777,8,9},0}
}
in this how can i get the values of arr[1][3][3] /// i mean to get value 800
and also arr[2][8][6]  /// 777 in this case....

for this type of array how should i declare it .. please help
Sorry for late reply,
As I explained in C we dont have a way to define a multi-dimantional array with variable least index. So I am afraid we may need to find out a alternate way to handle this.
I hope this helps...
Thanks for your suggestions
Similar Questions
0 votes

See the following two program

#include<stdio.h>
int main()
{
  int a[][5] = { 1}; 
  printf("%d", sizeof(a)); 
}

Output: 20

#include<stdio.h>
int main()
{
  int a[2][5] = { 1}; 
  printf("%d", sizeof(a)); 
}

Output: 40

Can someone explain the logic why there is such difference of the size?

+1 vote

declare only like arr[10] but here arr[10] is 2-D

+3 votes

We can declare the array in the following two ways, I want to know what is the re-commanded way -

arr[]={1,2,3,4,5,6,7,8,9,10};
arr[10]={1,2,3,4,5,6,7,8,9,10};
0 votes

Which is the best practice for variable declaration within the function ? Or is it vary from one language to other one ?

...