Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Problems in your code</h1> <p>Allright. Now that you've provided all the code it's easier to explain your problems:</p> <ol> <li>You're trying to define variables "in the middle" of your functions. <strong>C doesn't allow this</strong>. you have to define all your variables right at the start. That's what's giving you the<br> <code>error C2143: syntax error : missing ';' before 'type'</code><br> errors.</li> <li>Same goes for the function declaration (needs to be at the top of the function).</li> </ol> <p>Therefore, changing your code to the following makes it work:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;math.h&gt; #define ARRAY_SIZE 11 #define VECTOR_SIZE 5 main() { void f(long double fa[], long double fb[], long double fA, long double fB); long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE); long double* k = (long double*) malloc(sizeof (long double) * VECTOR_SIZE); int i; long double A, B; if (z == NULL) { printf("Out of memory\n"); exit(-1); } A = 0.5; B = 2; for (i = 0; i &lt; VECTOR_SIZE; i++) { k[i] = 0; } k[1] = 4; k[2] = 8; for (i = 0; i &lt; ARRAY_SIZE; i++) { z[i] = 0; } z[1] = 5; f(k, z, A, B); free(z); free(k); z = NULL; k = NULL; } void f(fa, fb, fA, fB) long double fa[], fb[], fA, fB; { fa[0] = fb[1]* fA; fa[1] = fa[1] - 1; fb[0] = 2* fB - fa[2]; printf("fa[2] is 8 and is the same as *[fa + 2] and is %3.3Le\n", *(fa + 2)); printf("\nAddress of &amp;fa[2] is %x\n", &amp;fa[2]); printf("same address is fa + 2 is %x\n", fa + 2); return; } </code></pre> <p><br /></p> <h2>A few other points</h2> <p>Now I'll add a few more tips, that perhaps aren't strictly errors (meaning, they still compile...), but aren't very good coding practices:</p> <ol> <li>As I said before, use <code>const</code>s to define constants rather than <code>#define</code>s.</li> <li>define <code>main()</code> properly - that is <code>int main() {...</code> rather than just <code>main()</code> without a return type. It works in C, but doesn't work in C++ and I consider it bad style. (Why the hell am I supposed to assume functions return int if nothing else is said? why not void?)</li> <li>Following that, you should explicitly return a value from <code>main()</code>.</li> <li>I prefer declaring the <code>void f(long double fa[], long double fb[], long double fA, long double fB);</code> function prototype outside <code>main()</code>.</li> <li>When defining functions use the modern syntax - the one you used in the prototype - rather than the ancient one:<br> <code>void f(fa, fb, fA, fB)</code><br> <code>long double fa[], fb[], fA, fB;</code><br> <code>{</code><br> Should become:<br> <code>void f(long double fa[], long double fb[], long double fA, long double fB) {</code>.</li> </ol> <p>This way your code turns to:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;math.h&gt; void f(long double fa[], long double fb[], long double fA, long double fB); int main() { const int ARRAY_SIZE = 11; const int VECTOR_SIZE = 5; long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE); long double* k = (long double*) malloc(sizeof (long double) * VECTOR_SIZE); int i; long double A, B; if (z == NULL) { printf("Out of memory\n"); exit(-1); } A = 0.5; B = 2; for (i = 0; i &lt; VECTOR_SIZE; i++) { k[i] = 0; } k[1] = 4; k[2] = 8; for (i = 0; i &lt; ARRAY_SIZE; i++) { z[i] = 0; } z[1] = 5; f(k, z, A, B); free(z); free(k); z = NULL; k = NULL; return 0; } void f(long double fa[], long double fb[], long double fA, long double fB) { fa[0] = fb[1]* fA; fa[1] = fa[1] - 1; fb[0] = 2* fB - fa[2]; printf("fa[2] is 8 and is the same as *[fa + 2] and is %3.3Le\n", *(fa + 2)); printf("\nAddress of &amp;fa[2] is %x\n", &amp;fa[2]); printf("same address is fa + 2 is %x\n", fa + 2); return; } </code></pre> <p>Which I think is better.</p> <p><br /></p> <h1>First Post</h1> <p>Please provide all your code. I tested the following code on Visual C++ 2008 Express with "language extensions" disabled and level 4 warnings. It works just fine:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; int main() { const int ARRAY_SIZE = 1024*1024; long double *z1 = (long double *)malloc(sizeof (long double) * ARRAY_SIZE); if (z1 == NULL) { printf("Out of memory\r\n"); exit(-1); } printf("Awesome!\r\n"); return 0; } </code></pre> <p>Maybe you forgot an include, maybe you did something else wrong. The code snippet itself seems perfectly fine. The second error you described seems totally unrelated: <code>error C2065: 'L' : undeclared identifier // this error repeats for all my identifiers</code></p> <p>By the way, prefer <code>const</code> over <code>#define</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload