Note that there are some explanatory texts on larger screens.

plurals
  1. POShifting elements in an array in C -- pointer-based
    primarykey
    data
    text
    <p>So I'm trying to learn C right now, and I have two functions: one that shifts elements in an array:</p> <pre><code>void shift(int a[], int n) { int i; for(i = 0; i != n-1; i++){ a[i] = a[i+1]; } } </code></pre> <p>and a version of the same function, except pointer-based:</p> <pre><code>void pointer_shift(int *a[], int n) { int i; for (i = 0; i != n - 1; i++) { *a[i] = *a[i + 1]; } } </code></pre> <p>I don't know whether the pointer-based version is correct or not, but I guess my most important question is how I'm supposed to actually test that both/either work. Besides the definition of these two functions, I have:</p> <pre><code>#include &lt;stdio.h&gt; void shift(int a[], int n); void pointer_shift(int *a[], int n); int main(void) { printf("Begin execution of testing Problem 1\n"); int a1[] = {100, 101, 102}; int i; for(i = 0; i&lt;3;i++) printf("Before Shift: " "%d\n", a1[i]); //shift(a1, 3); pointer_shift(&amp;a1, 3); for(i = 0; i&lt;3;i++) printf("After Shift In Main: " "%d\n", a1[i]); return 0; } </code></pre> <blockquote> <p>shift(a1, 3)</p> </blockquote> <p>works fine, but I, for the life of me, can't figure out how to correctly test pointer_shift. </p> <p>I get two errors; one is that in the line</p> <blockquote> <p>pointer_shift(&amp;a1, 3)</p> </blockquote> <p>I am passing argument 1 from an incompatible pointer type. The other error is indecipherable, but I was hoping the problem would be obvious enough that someone would be able to help me. So... how to test my two functions in my main?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    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. 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