Click here to Skip to main content
14,639,066 members
Rate this:
Please Sign up or sign in to vote.
See more:
I'm new to C.

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

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.

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
   
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

The {1, 2, 3} syntax is allowed only when initializing during the array's definition. It is not allowed for general assignment.
   
Comments
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100