Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Problem in compiling a C code with MSVC++6.00

Reply
Thread Tools

Problem in compiling a C code with MSVC++6.00

 
 
Babak
Guest
Posts: n/a
 
      06-26-2006

Hi Everyone,

I've written a standard C code for a simple finite element analysis
in MSVC++ . When I save the file as a cpp file, it compiles and runs
perfectly, but when I save it as a c file, there are lots of errors and
warnings in compiling stage. I am really confused about this problem
because I'm not even familiar with C++ and my code only includes simple
C functions. Can anybody please tell me what's my mistake? Is there any
other way rather than changing the extension of the file from .cpp to
..c during the save stage to let the compiler know that this is just a C
file and not a C++?

As another question I also need to compile my code with gcc, but the
compilation is not successful and it gives the following error: "
Incompatible types in initialization". Can you pls tell me how to write
and save my code so that it will be OS independent. Is there any
settings in MSVC++ that should be changed or the problem is with my
code.

Here is my entire code (Sorry it's a little lengthy!). I would be
very grateful if somebody can have a look at it and give me some hints.

Babak


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct element {
int p1;
int p2;
int p3;
int er;
};

struct node {
float x;
float y;
};
void checkfile (FILE *);
void read_node_data (FILE *, struct node *);
void read_element_data (FILE *,FILE *, struct element *, int *);
void ss(struct node*,struct node*, struct node*, int *, float * ,int
*,float *);
void assembly(int *, int *,int *, float *, float **, float *, float *);

int main(void) {

FILE *fp;
FILE *ft;
FILE *fer;
FILE *ftest;
int i;
int j;
int NUMNODE=0;//number of nodes
int NUMELE=0;//number of elemnts
char *str;
struct node *nd;
struct element *ele;
int *g;

str=(char *)malloc(sizeof(char)*80);
fp=fopen("femesh2_p.txt", "r");
ftest=fopen("nodeindex_2.txt", "w");
checkfile (fp);
checkfile (ftest);
while (!feof(fp)) {
fgets (str,77,fp);
fprintf(ftest, "%d %s \n", NUMNODE+1, str);
NUMNODE ++;
}
fcloseall();
ft=fopen("femesh2_t.txt", "r");
checkfile (ft);
while (!feof(ft)) {
fgets (str,79,fp);
NUMELE ++;
}
fclose(ft);
free(str);
//allocating enough memory for reading P,T and er and g arrays
nd=(struct node*)malloc(sizeof(struct node) * NUMNODE);
if (!nd) {
printf("cannot allocate memory for nodes!");
exit(1);
}
ele=(struct element*) malloc(sizeof(struct element) * NUMELE);
if (!ele) {
printf("cannot allocate memory for elements!");
exit(1);
}
g=(int *) malloc(sizeof(*g) * NUMELE);
if (!g) {
printf("cannot allocate memory for charge density!");
exit(1);
}

float **K;
K = (float **) malloc( NUMNODE * sizeof( float*));
if( K != NULL)
for( i = 0; i < NUMNODE; i++){
K[i] =(float *) malloc( NUMNODE * sizeof( float) );
if( K[i] == NULL) {
printf("cannot allocate memory for global stiffness
matrix!");
exit (1);
}
}

float *b;
b= (float *) malloc (NUMNODE * sizeof (float *));
if (b==NULL) {
printf ("cannot allocate memory for Right Hand Side Vector!");
exit (1);
}

// next we initialze elements of K and b to zero
for ( i = 0; i < NUMNODE; i++) {
for ( j = 0; j < NUMNODE; j++)
K[i][j]=0;
b[i]=0;
}


// reading and storing node information (P array)

fp=fopen("femesh2_p.txt", "r");
checkfile (fp);
for (i=0; i<NUMNODE; i++)
read_node_data(fp,nd+i);
fclose(fp);
ft=fopen("femesh2_t.txt", "r");
checkfile(ft);
fer=fopen("femesh2_er.txt", "r");
checkfile(fer);
// reading and stroing element information (T and er arrays)
for (i=0; i<NUMELE; i++)
read_element_data(ft, fer, ele+i, g+i);
//printf ("%d \n",*(g+i));
fcloseall();
// Now that all the information are available, we'll proceed to
calculate
//stiffness matrix for each element
float Me[3][3]={NULL};
float be [3] [1]={NULL};
for (i=0; i<NUMELE; i++) {
ss((nd+((ele+i)->p1)-1),(nd+((ele+i)->p2)-1),(nd+((ele+i)->p3)-1),&((ele+i)->er),&Me[0][0],
g+i, &be[0][0]);
int n1=(((ele+i)->p1)-1);
int n2=(((ele+i)->p2)-1);
int n3=(((ele+i)->p3)-1);
assembly (&n1,&n2,&n3,&Me[0][0], K, &be[0][0], b);
}

//The next step is to apply boundary conditions to global stiffness
matrix
for (i=0; i<NUMNODE; i++) {
float param=(((nd+i)->y) * ((nd+i)->y) + (((nd+i)->x)-10) *
(((nd+i)->x)-10));
if ( param > (0.74 * 0.74) && param < (0.75 * 0.75) && ((nd+i)->y)!=0
) {
K [i] [i] = 1e+8;
b [i] = 0;
printf("%d \n" ,i+1);

}
}
//
fp=fopen("SS2.DAT","w");
ft=fopen("LOAD2.DAT","w");
checkfile(fp);
checkfile(ft);

for(i=0;i<NUMNODE;i++) {
for(j=0;j<NUMNODE;j++) {
if (j==(NUMNODE-1))
fprintf(fp,"%f \n", K[i][j]);
else
fprintf(fp,"%f ", K[i][j]);

}

fprintf(ft,"%f \n ", b[i]);
}
fcloseall();


//printf ("%f \n", K[70][70]);
//printf ("%f \n", b[70]);

free(g);
free (ele);
free(nd);
for ( i = 0; i < NUMNODE; i++)
free(K[i]);
free(K);
free(b);
return 0;
}
// This function checks if the specified file is opened or not!
void checkfile (FILE * f)
{
if(!f) {
printf("can't open the file");
exit(1);
}
}

// This function reads the node coordination
void read_node_data (FILE * input, struct node *nod)
{
fscanf (input,"%f", &(nod->x));
fscanf (input,"%f", &(nod->y));
}
//This function reads element information
void read_element_data (FILE * f1,FILE * f2, struct element * element,
int *charge)
{
fscanf(f1,"%d", &(element->p1));
fscanf(f1,"%d", &(element->p2));
fscanf(f1,"%d", &(element->p3));
fscanf(f2,"%d", &(element->er));
fscanf(f2,"%d", charge);
}

// This function calculates the local stiffness matrix
void ss(struct node *n1,struct node *n2, struct node *n3, int *err,
float *MeMat, int *g ,float *be)
{
int k,j;
float a,r[3],q[3];
//initialize local elements to zero
for(k=0;k<3;k++) {
for(j=0;j<3;j++) {
*(MeMat+3*k+j)=0;
}
*(be+k)=0;
}
for(k=0;k<3;k++) {
q[0]=(n2->y)-(n3->y);
q[1]=(n3->y)-(n1->y);
q[2]=(n1->y)-(n2->y);
r[0]=(n3->x)-(n2->x);
r[1]=(n1->x)-(n3->x);
r[2]=(n2->x)-(n1->x);
}
a=(q[0]*r[1]-q[1]*r[0])/2;

for(j=0;j<3;j++)
for(k=j;k<3;k++)
{
*(MeMat+3*k+j) += ((*err)/(4*a))*q[j]*q[k];
if (j!=k)
*(MeMat+3*j+k)+=((*err)/(4*a))*q[j]*q[k];
}

for(j=0;j<3;j++)

for(k=j;k<3;k++)
{
*(MeMat+3*k+j) +=((*err)/(4*a))*r[j]*r[k];
if (j!=k)
*(MeMat+3*j+k)+=((*err)/(4*a))*r[j]*r[k];
}
for(k=0;k<3;k++)
*(be+k)=((*g)*a)/3;
}
// This function assembles the elements of local stiffness matrix into
global matrix
void assembly(int *n1, int *n2,int *n3, float *local, float ** global,
float *be, float *b)
{
//Assembling the global matrix
global[*n1][*n1]+= * (local);
global[*n2][*n2]+= * (local+4);
global[*n3][*n3]+= * (local+;
global[*n1][*n2]+= * (local+1);
global[*n2][*n1]+= * (local+1);
global[*n1][*n3]+= * (local+2);
global[*n3][*n1]+= * (local+2);
global[*n2][*n3]+= * (local+5);
global[*n3][*n2]+= * (local+5);
//Assembling load vector
b[*n1]+= *be;
b[*n2]+= *(be+1);
b[*n3]+= *(be+2);
}

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      06-26-2006
Babak wrote:
>
> Hi Everyone,
>
> I've written a standard
> C code for a simple finite element analysis
> in MSVC++ . When I save the file as a cpp file, it compiles and runs
> perfectly, but when I save it as a c file,
> there are lots of errors and
> warnings in compiling stage. I am really confused about this problem
> because I'm not even familiar with C++
> and my code only includes simple
> C functions. Can anybody please tell me what's my mistake?


> Here is my entire code (Sorry it's a little lengthy!).


That's much better than being vague.

Most likely your c code is being compiled according to C89 rules
with some C99 features as extensions, like // comments.

Consolidate all of you object declarations in main
at the begining. C89 doesn't allow declarations
after statements within a compound statement {within a block}.
And don't initialise arrays of float, with {NULL},
use {0} instead.

int main(void)
{
FILE *fp;
FILE *ft;
FILE *fer;
FILE *ftest;
int i;
int j;
int NUMNODE=0;//number of nodes
int NUMELE=0;//number of elemnts
char *str;
struct node *nd;
struct element *ele;
int *g;
float **K;
float *b;
float Me[3][3]={0};
float be [3] [1]={0};
int n1;
int n2;
int n3;

--
pete
 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      06-26-2006
Babak posted:


> str=(char *)malloc(sizeof(char)*80);



Just so you know, sizeof(char) is always 1. You could have written:

str = malloc(80);


The cast is redundant.


> int n1=(((ele+i)->p1)-1);
> int n2=(((ele+i)->p2)-1);
> int n3=(((ele+i)->p3)-1);



You might be pleased to know that C has another way of writing that:


int n1 = ele[i].p1 - 1;
int n2 = ele[i].p2 - 1;
int n3 = ele[i].p3 - 1;



> float param=(((nd+i)->y) * ((nd+i)->y) + (((nd+i)->x)-10) *
> (((nd+i)->x)-10));



Again here:


float param = nd[i].y * nd[i].y * (nd[i].x - 10) * (nd[i].x - 10);


> global[*n1][*n1]+= * (local);
> global[*n2][*n2]+= * (local+4);
> global[*n3][*n3]+= * (local+;
> global[*n1][*n2]+= * (local+1);
> global[*n2][*n1]+= * (local+1);
> global[*n1][*n3]+= * (local+2);
> global[*n3][*n1]+= * (local+2);
> global[*n2][*n3]+= * (local+5);
> global[*n3][*n2]+= * (local+5);
> //Assembling load vector
> b[*n1]+= *be;
> b[*n2]+= *(be+1);
> b[*n3]+= *(be+2);


Again here:

global[*n1][*n1] += *local;
global[*n2][*n2] += local[4];
global[*n3][*n3] += local[8];



--

Frederick Gotham
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      06-26-2006
In article <(E-Mail Removed) .com>,
Babak <(E-Mail Removed)> wrote:

> I've written a standard C code for a simple finite element analysis
>in MSVC++ . When I save the file as a cpp file, it compiles and runs
>perfectly, but when I save it as a c file, there are lots of errors and
>warnings in compiling stage. I am really confused about this problem
>because I'm not even familiar with C++ and my code only includes simple
>C functions. Can anybody please tell me what's my mistake?


You use two features which are allowed in C++ and C99 but not C90:
slash-slash comments and declarations mixed with code. Perhaps
your compiler does not accept these new features. Change your
comments to /* this style */ and move your declarations to the
start of the function or block.

> float Me[3][3]={NULL};
> float be [3] [1]={NULL};


This is also wrong; you should use 0 rather than NULL. NULL is
for pointers. (It may work on some systems depending on how NULL
is defined, but it's wrong anyway.)

-- Richard
 
Reply With Quote
 
Babak
Guest
Posts: n/a
 
      06-26-2006
Thank you!

Moving all the declarations to the begining of main, changing NULL
to {0) and revising the format of comments solve the problem with MSVC.
I'm not still able to run my code with gcc. When I type

gcc -0 pre2 pre2.c -lm

in cygwin , the .exe file is created. But when I run it, the program
does not terminate, although it doesn't declare any errors!

Frederick Gotham wrote:
> Babak posted:
>
>
> > str=(char *)malloc(sizeof(char)*80);

>
>
> Just so you know, sizeof(char) is always 1. You could have written:
>
> str = malloc(80);
>
>
> The cast is redundant.
>
>
> > int n1=(((ele+i)->p1)-1);
> > int n2=(((ele+i)->p2)-1);
> > int n3=(((ele+i)->p3)-1);

>
>
> You might be pleased to know that C has another way of writing that:
>
>
> int n1 = ele[i].p1 - 1;
> int n2 = ele[i].p2 - 1;
> int n3 = ele[i].p3 - 1;
>
>
>
> > float param=(((nd+i)->y) * ((nd+i)->y) + (((nd+i)->x)-10) *
> > (((nd+i)->x)-10));

>
>
> Again here:
>
>
> float param = nd[i].y * nd[i].y * (nd[i].x - 10) * (nd[i].x - 10);
>
>
> > global[*n1][*n1]+= * (local);
> > global[*n2][*n2]+= * (local+4);
> > global[*n3][*n3]+= * (local+;
> > global[*n1][*n2]+= * (local+1);
> > global[*n2][*n1]+= * (local+1);
> > global[*n1][*n3]+= * (local+2);
> > global[*n3][*n1]+= * (local+2);
> > global[*n2][*n3]+= * (local+5);
> > global[*n3][*n2]+= * (local+5);
> > //Assembling load vector
> > b[*n1]+= *be;
> > b[*n2]+= *(be+1);
> > b[*n3]+= *(be+2);

>
> Again here:
>
> global[*n1][*n1] += *local;
> global[*n2][*n2] += local[4];
> global[*n3][*n3] += local[8];
>
>
>
> --
>
> Frederick Gotham


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      06-26-2006
Babak wrote:
>
> Thank you!
>
> Moving all the declarations to the begining of main, changing NULL
> to {0) and revising the format of comments
> solve the problem with MSVC.
> I'm not still able to run my code with gcc. When I type
>
> gcc -0 pre2 pre2.c -lm
>
> in cygwin , the .exe file is created. But when I run it, the program
> does not terminate, although it doesn't declare any errors!


Your code is long,
I don't have
"femesh2_p.txt" or "femesh2_t.txt" or "femesh2_er.txt",
and there are many obvious portability problems with the code,
so I don't anticipate debugging it quickly.

--
pete
 
Reply With Quote
 
Babak
Guest
Posts: n/a
 
      06-26-2006
Can you pls at least give me some general ideas about these obvious
portability problems.

pete wrote:
> Babak wrote:
> >
> > Thank you!
> >
> > Moving all the declarations to the begining of main, changing NULL
> > to {0) and revising the format of comments
> > solve the problem with MSVC.
> > I'm not still able to run my code with gcc. When I type
> >
> > gcc -0 pre2 pre2.c -lm
> >
> > in cygwin , the .exe file is created. But when I run it, the program
> > does not terminate, although it doesn't declare any errors!

>
> Your code is long,
> I don't have
> "femesh2_p.txt" or "femesh2_t.txt" or "femesh2_er.txt",
> and there are many obvious portability problems with the code,
> so I don't anticipate debugging it quickly.
>
> --
> pete


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      06-26-2006
Babak wrote:
>
> Can you pls at least give me some general ideas about these obvious
> portability problems.


It's not really as bad as I said.
exit(1);
is the only obvious portability problem.

It's mostly just the length of the code
and the nonavailabilty of the text files
and that I don't know exactly what the code is supposed to do.

> pete wrote:


> > Your code is long,
> > I don't have
> > "femesh2_p.txt" or "femesh2_t.txt" or "femesh2_er.txt",
> > and there are many obvious portability problems with the code,
> > so I don't anticipate debugging it quickly.


--
pete
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      06-26-2006
In article <(E-Mail Removed) .com>
Babak <(E-Mail Removed)> wrote:
> Here is my entire code (Sorry it's a little lengthy!).


Better "too much" than "too little" (at least usually ).

Others already provided some comments; here are some more.

> fp=fopen("femesh2_p.txt", "r");
> ftest=fopen("nodeindex_2.txt", "w");
> checkfile (fp);
> checkfile (ftest);

[where "checkfile" terminates the program if its argument is NULL]

It would probably be nicer to say *which* file(s) cannot be opened.
Also, while it is not "truly portable", printing a "reason" for the
failure to open the file tends to work and tends to be useful. For
instance:

fp = fopen("femesh2_p.txt", "r");
if (fp == NULL) {
fprintf(stderr, "myprog: unable to open %s for reading: %s\n",
"femesh2_p.txt", strerror(errno));
exit(EXIT_FAILURE);
}
ftest = fopen("nodeindex_2.txt", "w");
if (ftest == NULL) {
fprintf(stderr, "myprog: unable to open %s for writing: %s\n",
"nodeindex_2.txt", strerror(errno));
exit(EXIT_FAILURE);
}

> while (!feof(fp)) {


This loop is (virtually) always wrong. See the comp.lang.c FAQ,
question 12.2, at <http://c-faq.com/stdio/feof.html>.

> fcloseall();


This is not a standard function. Presumably you mean to fclose()
fp and/or ftest here (note, I am just guessing).

> while (!feof(ft)) {


Another instance of an (almost-)always-incorrect loop for reading
files. (Rule for beginners: never use feof(). Rule for later:
test feof() once, after the loop ends, if you want to distinguish
between "input ended because of normal ordinary EOF" from "input
ended because of bad CD-ROM or similar media problem".)

(Probably some more of the code could stand to be factored into
functions, but in a quick scan, I did not spot any other major
errors here.)

>void read_node_data (FILE * input, struct node *nod)
>{
> fscanf (input,"%f", &(nod->x));
> fscanf (input,"%f", &(nod->y));
>}


The fscanf() function can fail; it may be worth checking the return
value here. You can also do this scan in one shot:

if (fscanf(input, "%f%f", &nod->x, &nod->y) != 2)
... do something about that ...

The possible reasons for failure here range from "error reading the
data off the input file" to "input file is working fine but contains
the Encyclopedia Britannica, instead of a sequence of floating point
numbers". All fscanf() tells you is whether it read and converted
(i.e., assigned) everything it was told to read (here, two "%f"s).

(For that matter, printf() and fprintf() can fail, although testing
every single call tends to be painful at best. One call to ferror(fp)
at the end of all, or each group of, writing, to check for failures
like disk-full, tends to be good enough.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Neil
Guest
Posts: n/a
 
      06-26-2006
Richard Tobin wrote:
> In article <(E-Mail Removed) .com>,
> Babak <(E-Mail Removed)> wrote:
>
>> I've written a standard C code for a simple finite element analysis
>> in MSVC++ . When I save the file as a cpp file, it compiles and runs
>> perfectly, but when I save it as a c file, there are lots of errors and
>> warnings in compiling stage. I am really confused about this problem
>> because I'm not even familiar with C++ and my code only includes simple
>> C functions. Can anybody please tell me what's my mistake?

>
> You use two features which are allowed in C++ and C99 but not C90:
> slash-slash comments and declarations mixed with code. Perhaps
> your compiler does not accept these new features. Change your
> comments to /* this style */ and move your declarations to the


You Know I have seen this for around 15 years. The C standard may have
just picked it up, But I have not had a compiler reject a // comment
since the early 90's
MSVC 6.0 does support them (so did MSVC 1.5)
It is NOT the problem here. // or /* */ has become a style issue. or
the standard would have added it a decade ago.



> start of the function or block.
>
>> float Me[3][3]={NULL};
>> float be [3] [1]={NULL};

>
> This is also wrong; you should use 0 rather than NULL. NULL is
> for pointers. (It may work on some systems depending on how NULL
> is defined, but it's wrong anyway.)
>
> -- Richard

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Cross-compiling error when compiling 2.6.1... Garrett Cooper Python 0 02-24-2009 09:47 PM
Cross-compiling error when compiling 2.6.1... Garrett Cooper Python 0 02-24-2009 08:55 PM
Problem compiling this code rajesh.shiggaon@gmail.com Java 6 04-19-2005 11:11 AM
Compiling when libedit is in path Is there a trick to compiling Ruby when libedit must exist in the search path? Can you statically link to readline 5.0 in some manner? -- Lon Baker Lon Baker Ruby 1 03-21-2005 08:57 AM
problem in compiling the c++ code ravinder C++ 4 02-03-2004 05:46 AM



Advertisments