This smells a bit like home-work but I am going to give you the benefit of the doubt on that one.

You can "

*walk*" the target array and "

*turn right*" whenever you end up outside of

`order`

by

`order`

dimensions or hit an already filled position.

Something like this might work;

#include <stdio.h>
#include <stdlib.h>
void print(int* array, int order) {
int row;
int col;
for (row = 0; row < order; ++row) {
for (col = 0; col < order; ++col) {
printf("%i ", array[row * order + col]);
}
printf("\r\n");
}
}
void spiral_fill(int* source, int *target, int count, int order) {
int i;
int v;
int index = 0;
int x = 0;
int y = 0;
int tx = 0;
int ty = 0;
int dx = 1;
int dy = 0;
for (i = 0; i < order * order; ++i)
target[i] = INT_MIN;
for (i = 0; i < order * order; ++i) {
target[y * order + x] = i < count ? source[i] : 0;
tx = x + dx;
ty = y + dy;
if (dx == 1 && (tx >= order || target[ty * order + tx] != INT_MIN)) {
dx = 0;
dy = 1;
}
else {
if (dx == -1 && (tx < 0 || target[ty * order + tx] != INT_MIN)) {
dx = 0;
dy = -1;
}
else {
if (dy == 1 && (ty >= order || target[ty * order + tx] != INT_MIN)) {
dx = -1;
dy = 0;
}
else {
if (dy == -1 && (ty < 0 || target[ty * order + tx] != INT_MIN)) {
dx = 1;
dy = 0;
}
}
}
}
x += dx;
y += dy;
}
}
int main() {
int i;
int count = 9; int order = 3;
int* source = (int*)malloc(sizeof(int) * count);
int* target = (int*)malloc(sizeof(int) * order * order);
for (i = 0; i < count; ++i)
source[i] = i + 1;
printf("Source:\r\n");
print(source, order);
spiral_fill(source, target, count, order);
printf("Target:\r\n");
print(target, order);
return 0;
}

Hope this helps,

Fredrik