M
Morgan
I have read much posts on the argument but no one
clearly says if this operation is possible or not.
Simply I have a routine which reads from a text
file some integer arrays (1 or 2D).
The first array in the file has known dimension,
but it contains the dimensions (so unknown at
compile time) of all the other arrays.
So I can allocate memory only after reading the
first array, but then I cannot bring out of my
reader routine the arrays created.
Maybe I was not so clear, all my problem is
represented by the following example.
Why neither test1 nor test2 routine can bring
out the pointer to the beginning of the array?
Printf calls never print the right values :-((
Maybe I am not so able with C...
....tanx in advance,
Morgan.
EXAMPLE BEGIN
_______________________________________________
#include <iostream>
#include <stdlib.h>
using namespace std;
void test1(int *);
void test2(int *);
//
// Main routine
///////////////////////////////////////////////
int main(int argc, char *argv[])
{
int *a;
test1(a);
for(int i=0;i<3;i++) printf("%d ",a);
test2(a);
for(int i=0;i<3;i++) printf("%d ",a);
system("PAUSE");
return 0;
}
//
// First test routine to pass the array in "a"
///////////////////////////////////////////////
void test1(int *a1)
{
a1 = new int[3];
for(int i=0;i<3;i++) a1=2*i;
}
//
// Second test routine to pass the array in "a"
///////////////////////////////////////////////
void test2(int *a1)
{
int *a2;
a2 = new int[3];
for(int i=0;i<3;i++) a2=2*i;
a1 = a2;
}
_______________________________________________
EXAMPLE END.
clearly says if this operation is possible or not.
Simply I have a routine which reads from a text
file some integer arrays (1 or 2D).
The first array in the file has known dimension,
but it contains the dimensions (so unknown at
compile time) of all the other arrays.
So I can allocate memory only after reading the
first array, but then I cannot bring out of my
reader routine the arrays created.
Maybe I was not so clear, all my problem is
represented by the following example.
Why neither test1 nor test2 routine can bring
out the pointer to the beginning of the array?
Printf calls never print the right values :-((
Maybe I am not so able with C...
....tanx in advance,
Morgan.
EXAMPLE BEGIN
_______________________________________________
#include <iostream>
#include <stdlib.h>
using namespace std;
void test1(int *);
void test2(int *);
//
// Main routine
///////////////////////////////////////////////
int main(int argc, char *argv[])
{
int *a;
test1(a);
for(int i=0;i<3;i++) printf("%d ",a);
test2(a);
for(int i=0;i<3;i++) printf("%d ",a);
system("PAUSE");
return 0;
}
//
// First test routine to pass the array in "a"
///////////////////////////////////////////////
void test1(int *a1)
{
a1 = new int[3];
for(int i=0;i<3;i++) a1=2*i;
}
//
// Second test routine to pass the array in "a"
///////////////////////////////////////////////
void test2(int *a1)
{
int *a2;
a2 = new int[3];
for(int i=0;i<3;i++) a2=2*i;
a1 = a2;
}
_______________________________________________
EXAMPLE END.