C
CapCity
I'm sure I'm missing something simple - I do not code in C regularly, and
the breaks are long enough for me to forget.
The situation I have is I need to create an array but I do not know the
dimension until runtime. I pass the pointer to a function which then
determines the size and then creates and populates it. I can walk the array
OK in the function, but the app crashes when I try to do so in the calling
routine.
Here's a small program that recreates the exception:
#include <stdio.h>
#include <stdlib.h>
#include "ArrayManagement.h"
#include "Test.h"
void MakeIVector(int *iv, int n1) {
int i;
iv = iVector(n1);
for (i = 0; i < n1; i++) iv = i*2;
for (i = 0; i < n1; i++) printf("%i\n", iv);
return;
}
int main() {
int n1 = 8;
int i, j, k;
int* iv = iVector(n1);
for (i = 0; i < n1; i++) iv = i;
for (i = 0; i < n1; i++) printf("%i\n", iv);
Free_iVector(iv);
printf("\n");
int *iv2;
MakeIVector(iv2, n1);
printf("\n");
for (i = 0; i < n1; i++) iv2 = i;
for (i = 0; i < n1; i++) printf("%i\n", iv2);
exit(0);
}
ArrayManagement is a tried and tested module that creates 1, 2 and 3
dimesioned arrays based on dimensions passed in.
In main a 1 dimensional array (vector) is created, populated and dumped to
console. No problems.
It then calls a function (MakeIVector) which creates, populates and dumps an
array to the console. This also works.
Upon the return from MakeIVector it blows up while trying to assign values
to elements of iv2. If I remove that loop, and only do the printf loop, I
get values to the screen, but it looks like addresses.
What am I doing wrong? I'd like to be able to pass these arrays to and from
functions.
To extend this, would anything change if I needed to pass these through
several layers of functions?
What about if I need to do this with 2 or three dimensional arrays? What
syntax would I need then?
This is ANSI C, and I use gcc.
Thanks in advance!
the breaks are long enough for me to forget.
The situation I have is I need to create an array but I do not know the
dimension until runtime. I pass the pointer to a function which then
determines the size and then creates and populates it. I can walk the array
OK in the function, but the app crashes when I try to do so in the calling
routine.
Here's a small program that recreates the exception:
#include <stdio.h>
#include <stdlib.h>
#include "ArrayManagement.h"
#include "Test.h"
void MakeIVector(int *iv, int n1) {
int i;
iv = iVector(n1);
for (i = 0; i < n1; i++) iv = i*2;
for (i = 0; i < n1; i++) printf("%i\n", iv);
return;
}
int main() {
int n1 = 8;
int i, j, k;
int* iv = iVector(n1);
for (i = 0; i < n1; i++) iv = i;
for (i = 0; i < n1; i++) printf("%i\n", iv);
Free_iVector(iv);
printf("\n");
int *iv2;
MakeIVector(iv2, n1);
printf("\n");
for (i = 0; i < n1; i++) iv2 = i;
for (i = 0; i < n1; i++) printf("%i\n", iv2);
exit(0);
}
ArrayManagement is a tried and tested module that creates 1, 2 and 3
dimesioned arrays based on dimensions passed in.
In main a 1 dimensional array (vector) is created, populated and dumped to
console. No problems.
It then calls a function (MakeIVector) which creates, populates and dumps an
array to the console. This also works.
Upon the return from MakeIVector it blows up while trying to assign values
to elements of iv2. If I remove that loop, and only do the printf loop, I
get values to the screen, but it looks like addresses.
What am I doing wrong? I'd like to be able to pass these arrays to and from
functions.
To extend this, would anything change if I needed to pass these through
several layers of functions?
What about if I need to do this with 2 or three dimensional arrays? What
syntax would I need then?
This is ANSI C, and I use gcc.
Thanks in advance!