-8
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main()  
{
    int i,j;
    double xcord[201], ycord[201];
    double *a,*b;
    float delx,dely;

    double *phi = (double *)malloc(201 *201* sizeof(double));
    double *r = (double *)malloc(201 *201* sizeof(double));

    delx=0.005;

    dely=0.005;
    a=xcord;
    b=xcord;

    for(i=1;i<=201;i++)//grid generation
    {
        xcord[i]=-0.5+0.005*(i-1);
        ycord[i]=-0.5+0.005*(i-1);
    }
    for(i=1;i<=201;i++)
    {   
        for(j=1;j<=201;j++)
        { 
            printf("%f %f\n",xcord[i],ycord[j]);
        }
    }
    for(i=1;i<=201;i++)//phi value calculation
    {   
        for(j=1;j<=201;j++)
        {   
        /* Next 2 lines are highlighted */ 
            *(r + (i-1)*201+ (j-1))=1500*[{(*(a+(i-1))+0.25)*(*(a+(i-1))+0.25)}+{(*(b+(j-1)))*(*(b+(j-1)))};
            *(phi + (i-1)*201+ (j-1))=5*exp (*(r + (i-1)*201+ (j-1)));
        }
    }
    for(i=1;i<=201;i++)
    {   
        for(j=1;j<=201;j++)
        { 
            printf("%f \n",*(phi + (i-1)*201+ (j-1)));
        }
    }
    return 0;
}

This the code I am writing to generate a computational grid and afterwards to calculate the value of variable phi at each node of the grid. I am trying to put the equation of phi (=5exp[-1500{(x+0.25)*(x+0.25)+y*y}]) in the two line highlighted. But each time I compile I get an error "expected expression before [ token in the line 34.

Dhara
  • 6,587
  • 2
  • 31
  • 46
Mr . X
  • 1
  • 2
  • 2
    Please format your code properly. Check the 'Preview' area before submitting. Also you should post your exact code. On the line starting `***(r` I'm not sure if those stars are meant to be part of the code or not... – M.M Feb 04 '16 at 06:30
  • its not a part of my code.i was trying to highlight the line of problem in stack overflow and they appeared as **. – Mr . X Feb 04 '16 at 06:54
  • Looking a that row, the compiler error is just the first of your problems. Is this for the [IOCCC](http://www.ioccc.org/)? – Lundin Feb 04 '16 at 07:27

1 Answers1

3

Parentheses are parentheses. In C you don't use any other kind of braces when grouping expressions, as the square-braces [] and curly-braces {} have special meaning in the language. Just use parentheses ().


Then a couple of general hints: For any pointer or array p and index i, the expression *(p + i) is equivalent to p[i]. The latter is usually easier to read and understand, which means it's also more maintainable. Note the p can be any expression that yields a pointer (or array), and i can be any expression that yields an non-negative integer.

Also, if you have a statically sized "array" like e.g. phi in your code, declare it as an array instead. That will also make it easier to understand and maintain. And if phi is an so-called "2d" array, then make it an array of arrays:

double phi[201][201];

This is especially important as you actually don't free the memory you allocate. It works out fine in this case since you exit the program which on most system will mean that allocated memory will be free'd automatically, but making a habit to free what you allocate will save you trouble down the road. Using arrays you don't need to free anything.

Lastly, in C you should not cast the result of malloc (or any function returning void *). See this SO question and answer for more information.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621