PDA

View Full Version : error: expected ] before : token error



lolthisiscool
March 11th, 2009, 07:58 AM
Hi, I am new here as well as towards the C programming language.

I have been getting an error as per the thread's title.

LUdecomposition.c: In function ‘main’:
LUdecomposition.c:56: error: expected ‘]’ before ‘:’ token
LUdecomposition.c:56: error: expected ‘]’ before ‘:’ token


The code was copied from another website which makes me rather confused as it should work for most. Anyhow, here's the code.





// Gaussian Elemination
//
// Copyright by mpC team, 1999
//
// http://www.ispras.ru/~cbr
// mpc@ispras.ru
//
// Version with variable matrix size



#include<stdio.h>


void init_matrix(int dim, double (*a) [dim][dim]) {
int i,j;

for(i=0;i<dim;i++)
for(j=0;j<dim;j++)
(*a)[i][j]=10.;

for(i=0;i<dim;i++)
(*a)[i][i]=10. * dim;
}


void print_matrix(int dim,double (*a) [dim][dim]){
int i,j;
for(i=0;i<dim;i++) {
for(j=0;j<dim;j++)
printf("%lf\t",(*a)[i][j]);
printf("\n");
}

}

main()
{
int N;
int i,j;

printf("\nEnter N (matrix dimension): ");
scanf("%d",&N);

{
double A[N][N];

init_matrix(N,&A);
printf("\n Source Matrix :\n");
print_matrix(N,&A);

for(i=0;i<N-1;i++) {
for(j=i+1;j<N;j++) {
double t;
t=A[j][i]/A[i][i];
if(A[j][i]!=0) A[j][i:N-1]-=t*A[i][i:N-1];
}
}


printf("\n Matrix after LU transformation: \n");
print_matrix(N,&A);
}

}




Thank you!

sujoy
March 11th, 2009, 08:37 AM
if(A[j][i]!=0) A[j][i:N-1]-=t*A[i][i:N-1];

array indexes can be integers only or variables that evaluates to integers.
[i:N-1] is not allowed, ':' is used to slicing in some languages but not in C

lolthisiscool
March 11th, 2009, 08:54 AM
Oh, thank you, but do you have any suggestions on how I can actually amend the code in order to suit that line?

Once again, thanks.

wmcbrine
March 11th, 2009, 02:14 PM
This isn't C code, despite a superficial resemblance. As it says at the URL in the comments in your example, it's meant to illustrate a feature of a C-derived language called "C[]" (which I'd never heard of before). As such, there is no straightforward translation to C, since the whole point of the example is to show you something you can't do in C.

sujoy
March 11th, 2009, 03:07 PM
This isn't C code, despite a superficial resemblance. As it says at the URL in the comments in your example, it's meant to illustrate a feature of a C-derived language called "C[]" (which I'd never heard of before). As such, there is no straightforward translation to C, since the whole point of the example is to show you something you can't do in C.

whoa i didnt hope to be surprised in this way :o
eyes can be so misleading !!