How to pass an Array to AngularJS?

How to pass a 2d array to a function..?

  • I am trying to write a c program for solving sudoku..and at a point I came across this situation.. I have to pass an array to a function and when I pass it normally.. i get some warnings and segmentation fault.. when executed.. ill try to put up my program here soon.. i have used gdb to find the bug..and it stopped executing at the line a[q][w]==expval; now this a[q][w] is inside a function and when i asked to print value mof q,w it prints 6,6 and when i asked to print the value of a[q][w] it says it cant access that memory location.. i ll try to post up the code soon .. please try to fix it..

  • Answer:

    Examine the following code. It's a chess puzzle solver for the Knight's Tour on a 5x5 grid. It passes a 2D array around. The declaration "int (*c)[5]" says "c is a pointer to an array of 5 int's." So c[0] would be the first array of 5 int's, c[1] would be the second, c[2] would be the third, etc. Tris's reply is screwed up. C doesn't create 2D arrays as an array of pointers to arrays. You can do that in C if you work hard at it, but it's not how C sets up a 2D array using the usual syntax. Defining "int x[8][8];" does NOT create ANY pointers at all. It simply allocates 64 integers. That's all. And sets them up in a certain order. If you want to pass x to a function, you do NOT declare the parameter as "int **p" for example. But as "int (*p)[8]". Do NOT follow Tris's advice. #include <stdio.h> static int s[50], t[50], z; int iscompleted( int (*c)[5] ) {     int i, j;         for ( i= 0; i < 5; ++i )             for ( j= 0; j < 5; ++j )                 if ( c[i][j] == 0 )                     return 0;     return 1; } int move( int a, int b, int (*c)[5] ) {     s[z]= a;     t[z++]= b;     c[a][b]= 1;     if ( iscompleted( c ) ) {         int i, count= 0;         for ( i= 0; i < z-1; ++i ) {             printf( "<%d %d>", s[i], t[i] );             count++;             if ( count == 10 ) {                 printf( "\n      " );                 count= 0;             } else                 printf( "  " );         }                 return printf( "<%d %d>\n", s[z-1], t[z-1] ), 1;     }     if ( ( a < 4 && ( ( b < 3 && c[a+1][b+2] == 0 && move( a+1, b+2, c ) ) ||                    ( b > 1 && c[a+1][b-2] == 0 && move( a+1, b-2, c ) )) ) ||          ( a < 3 && ( ( b < 4 && c[a+2][b+1] == 0 && move( a+2, b+1, c ) ) ||                    ( b > 0 && c[a+2][b-1] == 0 && move( a+2, b-1, c ) )) ) ||          ( a > 0 && ( ( b < 3 && c[a-1][b+2] == 0 && move( a-1, b+2, c ) ) ||                    ( b > 1 && c[a-1][b-2] == 0 && move( a-1, b-2, c ) )) ) ||          ( a > 1 && ( ( b < 4 && c[a-2][b+1] == 0 && move( a-2, b+1, c ) ) ||                    ( b > 0 && c[a-2][b-1] == 0 && move( a-2, b-1, c ) )) ) )         return 1;     c[a][b]= 0;     --z;     return 0; } int main( int argc, char *argv[] ) {     int board[5][5], i, j, x, y;         for ( x= 0; x < 5; ++x )             for ( y= 0; y < 5; ++y ) {                 int r;                 z= 0;                 for ( i= 0; i < 5; ++i )                     for ( j= 0; j < 5; ++j )                         board[i][j]= 0;                 printf( "%d %d = ", x, y );                 r= move( x, y, board );                 if ( !r )                     printf( "failure\n" );             }     return 0; }

shasha at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Evidently that part of the array isn't in valid memory. Bear in mind that an array of length n has as its maximum index the number n-1. That is to say, to access element 6 of an array, the array's length must be at least 7. For a 2D array, it must be true of both dimensions.

green meklar

// Did you put 2 stars? int function(int ** arr) // A 2d array is an array of pointers to arrays. int main(void) { int arr2d [10][10] function(arr2d) } int function(int ** arr) { // You may now use arr like a normal 2d array // arr is a pointer to the array so arr[x][y] will return element (x, y) of arr2d }

tris

Is it perhaps the program is searching outside the highest subscript of the array?

Aaron

You need to calculate your moves

Elle

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.