arithmetic on pointer to an incomplete type

  • Thread starter Pushkar Pradhan
  • Start date
P

Pushkar Pradhan

I tried to read the archives and solve this problem, but now I think I
better post my problem:
int main() {
int blkSz[NUMBASECASES][2] = { {2,2},
{2,3},
.....,
{6,6} };

write_bc_perf(mflops1, blkSz, NUMBASECASES);

void write_bc_perf(double mflops1[], int blockSz[][], int numEntries)
{
......../*other code*/
fprintf(fp, "%dx%d %g\n", blockSz[0], blockSz[1], mflops1);
......../*other code*/
}

This is my compiler error:
psp1@leto:~/pdc$ gcc -g -o main exec_basecases.c gen_matrix.c codelets.c
exec_basecases.c: In function `write_bc_perf':
exec_basecases.c:120: arithmetic on pointer to an incomplete type
exec_basecases.c:120: arithmetic on pointer to an incomplete type

When I comment out the above fprintf.. and compile,
In gdb I try to access blockSz[0] in write_bc_perf function, I get
the correct values for i=0, but the values are incorrect for i=1, i=2, etc.
Can anyone point out the problem.
 
O

Oliver Fleischmann

I tried to read the archives and solve this problem, but now I think I
better post my problem:
int main() {
int blkSz[NUMBASECASES][2] = { {2,2},
{2,3},
.....,
{6,6} };

write_bc_perf(mflops1, blkSz, NUMBASECASES);

void write_bc_perf(double mflops1[], int blockSz[][], int numEntries)
{
......./*other code*/
fprintf(fp, "%dx%d %g\n", blockSz[0], blockSz[1], mflops1);
......./*other code*/
}


Does it work if you define the function like this:

void write_bc_perf(double *mflops1, int **blockSz, int numEntries)
{
......../*other code*/
fprintf(fp, "%dx%d %g\n", blockSz[0], blockSz[1],
mflops1);
......../*other code*/
}

?


Olli
 
M

Micah Cowan

Pushkar Pradhan said:
I tried to read the archives and solve this problem, but now I
think I better post my problem:
int main() {
int blkSz[NUMBASECASES][2] = { {2,2},
{2,3},
.....,
{6,6} };

write_bc_perf(mflops1, blkSz, NUMBASECASES);

void write_bc_perf(double mflops1[], int blockSz[][], int numEntries)
{
......./*other code*/
fprintf(fp, "%dx%d %g\n", blockSz[0], blockSz[1], mflops1);
......./*other code*/
}

This is my compiler error:
psp1@leto:~/pdc$ gcc -g -o main exec_basecases.c gen_matrix.c codelets.c
exec_basecases.c: In function `write_bc_perf':
exec_basecases.c:120: arithmetic on pointer to an incomplete type
exec_basecases.c:120: arithmetic on pointer to an incomplete type

When I comment out the above fprintf.. and compile,
In gdb I try to access blockSz[0] in write_bc_perf function, I
get the correct values for i=0, but the values are incorrect for
i=1, i=2, etc.
Can anyone point out the problem.


If you invoked gcc in strict ISO conformance mode by adding the
-ansi and -pedantic switches, you'd also get:

.... warning: array type has incomplete element type

Your declaration of the blockSz parameter is not legal in C. Make
it a complete type such as "int blockSz[][2]", and your problems
should be solved.

-Micah
 
B

Barry Schwarz

I tried to read the archives and solve this problem, but now I think I
better post my problem:
int main() {
int blkSz[NUMBASECASES][2] = { {2,2},
{2,3},
.....,
{6,6} };

write_bc_perf(mflops1, blkSz, NUMBASECASES);

void write_bc_perf(double mflops1[], int blockSz[][], int numEntries)
{
......./*other code*/
fprintf(fp, "%dx%d %g\n", blockSz[0], blockSz[1], mflops1);
......./*other code*/
}


Does it work if you define the function like this:

void write_bc_perf(double *mflops1, int **blockSz, int numEntries)


This should produce a diagnostic if the prototype is in scope at the
time the function is called. An int** is nothing like an int[N][M].

If the prototype is not in scope, I hope it invokes undefined behavior
because you have passed the function a completely different type than
it expects to receive. At the very least I would expect numerous run
time errors.


<<Remove the del for email>>
 
P

Pushkar Pradhan

I tried both passing int **blockSz and declaring int blockSz[][2] = {
/*initializing*/}

int **blockSz gives this error:
psp1@leto:~/pdc$ gcc -g -o main exec_basecases.c gen_matrix.c codelets.c
exec_basecases.c: In function `main':
exec_basecases.c:73: warning: passing arg 2 of `write_bc_perf' from
incompatible pointer type

And int blockSz[][2], gives the same compile error as my previous
declaration.
Both give compile errors.

Micah said:
I tried to read the archives and solve this problem, but now I
think I better post my problem:
int main() {
int blkSz[NUMBASECASES][2] = { {2,2},
{2,3},
.....,
{6,6} };

write_bc_perf(mflops1, blkSz, NUMBASECASES);

void write_bc_perf(double mflops1[], int blockSz[][], int numEntries)
{
......./*other code*/
fprintf(fp, "%dx%d %g\n", blockSz[0], blockSz[1], mflops1);
......./*other code*/
}

This is my compiler error:
psp1@leto:~/pdc$ gcc -g -o main exec_basecases.c gen_matrix.c codelets.c
exec_basecases.c: In function `write_bc_perf':
exec_basecases.c:120: arithmetic on pointer to an incomplete type
exec_basecases.c:120: arithmetic on pointer to an incomplete type

When I comment out the above fprintf.. and compile,
In gdb I try to access blockSz[0] in write_bc_perf function, I
get the correct values for i=0, but the values are incorrect for
i=1, i=2, etc.
Can anyone point out the problem.



If you invoked gcc in strict ISO conformance mode by adding the
-ansi and -pedantic switches, you'd also get:

... warning: array type has incomplete element type

Your declaration of the blockSz parameter is not legal in C. Make
it a complete type such as "int blockSz[][2]", and your problems
should be solved.

-Micah
 
M

Micah Cowan

Oliver Fleischmann said:
I tried to read the archives and solve this problem, but now I think I
better post my problem:
int main() {
int blkSz[NUMBASECASES][2] = { {2,2},
{2,3},
.....,
{6,6} };

write_bc_perf(mflops1, blkSz, NUMBASECASES);

void write_bc_perf(double mflops1[], int blockSz[][], int numEntries)
{
......./*other code*/
fprintf(fp, "%dx%d %g\n", blockSz[0], blockSz[1], mflops1);
......./*other code*/
}


Does it work if you define the function like this:

void write_bc_perf(double *mflops1, int **blockSz, int numEntries)
{
......./*other code*/
fprintf(fp, "%dx%d %g\n", blockSz[0], blockSz[1],
mflops1);
......./*other code*/
}

?


Hell no. Shouldn't even compile: definitely diagnostic ('course,
so does the above on a conformant implementation). You can't just
substitute a pointer-to-pointer for an array-of-arrays (or
pointer-to-array). Brush the dust off your C book and study the
differences between arrays and pointers. No, they're not always
just interchangeable.

-Micah
 
A

Arthur J. O'Dwyer

I tried both passing int **blockSz and declaring int blockSz[][2] = {
/*initializing*/}

int **blockSz gives this error:

Of course.
And int blockSz[][2], gives the same compile error as my previous
declaration.

Not really.
int main() {
int blkSz[NUMBASECASES][2] = { {2,2},
{2,3},
.....,
{6,6} };

write_bc_perf(mflops1, blkSz, NUMBASECASES);

void write_bc_perf(double mflops1[], int blockSz[][], int numEntries)
^^^^^^^^^^^^^^^
Change this to 'int blockSz[][2]' and re-compile.

-Arthur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top