Click here to Skip to main content
15,888,065 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm new to C.

Create an array which holds at every index a pointer to another array of dynamic size.

C
int main()
{
  unsigned int i , j;

  int* array1[2];
  int a1[] = {1,2,3};
  int a2[] = {2,3};

  array1[0] = a1;
  array1[1] = a2;

  for (i=0 ; i < 2; i++) {
     printf("The value of array1[%d] = %d" , i , *array1[i]);
  }

  return 1;
}


the value of array1[0] = 1

the value of array1[1] = 2


But similarly doing it directly ,Causing an error.

C
unsigned int* c[3];
 c[0] = {0, 5, 4, 7};  //  This Line Err` 
 c[1] = {0, 5, 4, 3, 2, 6, 7};
 c[2] = {0, 5, 4, 3, 2};


Causing an error : file try.c line xx function main: syntax error before `{'

Why ??

thanks
Posted
Updated 5-Jan-16 7:09am
v2
Comments
Sergey Alexandrovich Kryukov 5-Jan-16 14:13pm    
Essentially, an array itself is the pointer to the first element of the array. Aren't you are trying to create a pointer to that pointer? :-)
—SA
ALEX_CROME 5-Jan-16 20:56pm    
Yeah. I was trying to do that.

1 solution

The {1, 2, 3} syntax is allowed only when initializing during the array's definition. It is not allowed for general assignment.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jan-16 15:58pm    
Sure, a 5.
—SA
ALEX_CROME 5-Jan-16 20:57pm    
Thanks
ALEX_CROME 5-Jan-16 20:59pm    
Is there any way out ?? One way out that i can think is first making a defnation and then assigning the same to the pointer, the first thing that i have mentioned.
The point is i have to iterate over elements of the array which are themselves arrays.

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