# Beginner has problem。Can anyone have a look at my code?

Printable View

• 04-24-2011
smartfish
Beginner has problem。Can anyone have a look at my code?
I'm trying to get the coordinates of a 9x9 flat grid. The first point start at the origin. The current code has output but is apparently wrong.
Code:

```#include<math.h> #include<stdio.h> #define pi 3.1415926 double initial( int Nbead, double Rold[3][Nbead],int Nx) {int i; for (i=0;i<Nbead;i++){     Rold[0][i] = (floor(i/Nx)-1)+1;     Rold[1][i] = (i%Nx);     Rold[2][i] = 0.0; } return Rold[3][Nbead]; } int main() { int Nx=9; int Ny=9; double aa=1; int Nbead=Nx*Ny; /*total number of beads*/ double Rold[3][Nbead]; int Nbond=2*Nx*Ny-Nx-Ny;/*total number of bonds*/ int j,k; /*get the position (a flat sheet)*/ double initial(int Nbead, double Rold[3][Nbead],int Nx); for (k=0;k<Nbead;k++)     {     for (j=0;j<3;j++)         printf("the j is %d, the k is %d, Rold is %d\n",j, k,Rold[j][k]);     printf("\n");     }     return 0; }```
• 04-24-2011
stahta01
Code:

`double * Rold[3][Nbead];`
Why do you have an array of pointers?

Tim S.
• 04-24-2011
smartfish
Yes, I know there's should have something to do with the printer. My point is I need to pass the matrix from the initial function to the main function. Pointer should be the way of doing that (I guess?). Any suggestion on how I should change that?
• 04-24-2011
stahta01
Get rid of the pointer till you can think of a real reason.
An array is treated just like a pointer in most cases that a beginner needs to do.

Tim S.
• 04-24-2011
smartfish
Thanks for the suggestion. I got rid of the pointer. But still don't get the right result? I think the matrix is not passed from the initial function to the main function?
• 04-24-2011
stahta01
Code:

```/*get the position (a flat sheet)*/ double initial(int Nbead, double Rold[3][Nbead],int Nx); for (k=0;k<Nbead;k++)```
You need to call initial in your code; what you did is another prototype of the function.

something like below will be needed.
Code:

```/*get the position (a flat sheet)*/ initial(Nbead, Rold, Nx); for (k=0;k<Nbead;k++)```
Also the following is not right
Quote:

printf("the j is %d, the k is %d, Rold is %d\n",j, k,Rold[j][k]);
• 04-24-2011
smartfish
And now I'm getting an error message as follows

In function 'main':
|error: incompatible type for argument 2 of 'initial'|
|note: expected 'double (*)[<Uc630> + 1u]' but argument is of type 'double'|
||=== Build finished: 1 errors, 0 warnings ===|

Does this mean it still need a pointer?
• 04-24-2011
stahta01
What is your code; since it works for me.

Edit: You do know that you are doing things that does NOT work with all C Compilers; some of them are NOT C99 Standard.
Edit: You should know that an array name is a pointer in C. See the Next post for the exact truth; but, they are much the same in this case.
http://c-faq.com/aryptr/aryptrequiv.html

Edit: Getting tired; I suggest indenting your code; you are more likely to get more help for code that is indented.
http://sourceforge.net/apps/mediawik...le=Indentation

Tim S.
• 04-24-2011
quzah
Quote:

Originally Posted by stahta01
Edit: You should know that an array name is a pointer in C.

Except for when it's not (which is always).
Code:

```char array[ X ]; array++; /* if it were a pointer, that would be fine */```
Read: arrays and pointers

Quzah.
• 04-24-2011
quzah
Quote:

Originally Posted by stahta01
@quzah: The OP started out with an array of Double pointers without ever allocating any doubles for the pointers to point to in real memory.

It doesn't matter what the array is. An array name is not a pointer. You can't increment the array name, that means it's not a pointer. Array names are not pointers. That was the point I was making.

Quzah.
• 04-24-2011
smartfish
OK, my code is working now. Thanks for all the replies!
• 04-24-2011
quzah
Quote:

Originally Posted by stahta01
You can not increment an constant integer; but it is still an integer.
Tim S.

That's a stupid comparison. You can't compare a qualified type with a non-qualified one. And no, it's not a constant integer is not an integer. It's a constant integer. They are not the same thing. That's like trying to say "an lvalue and an rvalue are the same thing because they both are values!"

Array names are not pointers. You cannot assign a different address to the name of an array. You cannot make an array name point to anything. That means it's not a pointer.

Quzah.
• 04-24-2011
quzah
Quote:

Originally Posted by stahta01
You are not following me an array name is the same as a constant pointer in every case that I know about.

Tim S.

No, YOU are not following. They aren't the same thing. I already showed you a FAQ that explains how they are different, but here, let's do one more, shall we?
Code:

```char array[ 10 ]; char const *p = array; printf( "sizeof( array ) is %d, and sizeof( p ) is %d\n", sizeof( array ), sizeof( p ) );```
Now run along and read the link the nice man provided for you.

Quzah.