access pointer to array of doubles


Toon

I have these structures:

typedef struct {
    char *start;
    char *loops;
    char *tolerance;
    double *numbers;
} configuration;

typedef struct {
    bool help;
    bool debug;
    const char *configFile;
    const char *inputFile;
    const char *outputFile;
    bool parallel;
    int fragmentSize;

} options;

When inputFileis NULLI double pointer (set config.numbers) to the array: double numbers[] = {3,5,7};if there is one inputFileI read the number from it into the array numberListafter I set the pointer config.numbersto numberList. Here is the code:

//Safe number list into config
    if(opt.inputFile == NULL) {

        config.numbers = &numbers[0];
        lines = 3;
    } else {
        //Count lines|numbers in file
        FILE* fp;
        fp = fopen(opt.inputFile,"r");
        int bh = 0;
        while(!feof(fp))
        {
            bh = fgetc(fp);
            if(bh == '\n')
            {
                lines++;
            }
        }
        fclose(fp);
        //End counting

        //Read numbers from file
        fp = fopen(opt.inputFile,"r");
        double numberList[lines];
        double number;
        int n;
        for (n = 0; n < lines; n++) {
            fscanf(fp, "%lg", &number);
            numberList[n] = number;
        }
        //End reading numbers from file
        config.numbers = &numberList[0];

    }
    //End safing number list

When I use this for-loop numberListto print else:

int z;
for(z = 0; z < lines; ++z) {
      printf("%f\n", numberList[z]));
}

Everything works fine, but when I try to print the array using a pointer with this for-Loop:

int z;
for(z = 0; z < lines; ++z) {
    printf("%f\n", *(config.numbers + z));
}

The result is this:

71861994.719069
28587064.020609
91127582.029965
34937973.383320
49643168.000000
0.000000
0.000000
27369248.460685
54686448.003485
93571557.380991
-nan
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000

So, how to properly access pointers or other errors?

EDIT: The input file looks like this:

95181223.701304
27539279.045323
25701472.780528
29898009.416600
72366798.330269
3326112.825110
79857126.893409
38880807.039738
8199298.711586
42132873.992498
34763372.472843
14076941.001265
79042893.824653
17188914.128202
93208812.499982
42846886.181667
80882719.243356
23444107.325489

I expect the same output.

Initialization line:

int lines = 0;
Iharob Al Asimi

You've declared the array in your own elsebranch ifand then tried to print it out of that range via a pointer. This means that the array has been destroyed when you try to print it.

You need to use dynamic allocation, or try to rewrite your code in some way to allow the array to be declared/defined in the same scope where you want to print the array.

Related


access pointer to array of doubles

Toon I have these structures: typedef struct { char *start; char *loops; char *tolerance; double *numbers; } configuration; typedef struct { bool help; bool debug; const char *configFile; const char *inputFile; const char *

access pointer to array of doubles

Toon I have these structures: typedef struct { char *start; char *loops; char *tolerance; double *numbers; } configuration; typedef struct { bool help; bool debug; const char *configFile; const char *inputFile; const char *

access pointer to array of doubles

Toon I have these structures: typedef struct { char *start; char *loops; char *tolerance; double *numbers; } configuration; typedef struct { bool help; bool debug; const char *configFile; const char *inputFile; const char *

access pointer to array of doubles

Toon I have these structures: typedef struct { char *start; char *loops; char *tolerance; double *numbers; } configuration; typedef struct { bool help; bool debug; const char *configFile; const char *inputFile; const char *

Pointer to array of doubles or array of pointers?

Mershad I'm maintaining part of the code my friend wrote, this is the definition of a variable called d: double (*d)[3]; I tried to initialize variables using the code below, but there are errors in every part (runtime or compile). I am confused whether the v

Pointer to array of doubles or array of pointers?

Mershad I'm maintaining part of the code my friend wrote, this is the definition of a variable called d: double (*d)[3]; I tried to initialize variables using the code below, but there are errors in every part (runtime or compile). I am confused whether the v

Pointer to array of doubles or array of pointers?

Mershad I'm maintaining part of the code my friend wrote, this is the definition of a variable called d: double (*d)[3]; I tried to initialize variables using the code below, but there are errors in every part (runtime or compile). I am confused whether the v

Python ctypes parameter with DLL - pointer to array of doubles

lightning I'm a novice programmer using ctypes with Python and trying to use functions from a DLL written in C. I've found a lot of similar questions on SO to solve, but none of the answers to this kind of conundrum. I have the DLL loaded just fine, but one of

Python ctypes parameter with DLL - pointer to array of doubles

lightning I'm a novice programmer using ctypes with Python and trying to use functions from a DLL written in C. I've found a lot of similar questions on SO to solve, but none of the answers to this kind of conundrum. I have the DLL loaded just fine, but one of

Python ctypes parameter with DLL - pointer to array of doubles

lightning I'm a novice programmer using ctypes with Python and trying to use functions from a DLL written in C. I've found a lot of similar questions on SO to solve, but none of the answers to this kind of conundrum. I have the DLL loaded just fine, but one of

access pointer array inside pointer

Natico I want to create a method called add_order that adds a new order to the existing orders array. The first three lines of main work fine until that point I know the pointers, but I'm struggling with the add_order method and it doesn't work in printf eithe

access pointer array inside pointer

Natico I want to create a method called add_order that adds a new order to the existing orders array. The first three lines of main work fine until that point I know the pointers, but I'm struggling with the add_order method and it doesn't work in printf eithe

access pointer array inside pointer

Natico I want to create a method called add_order that adds a new order to the existing orders array. The first three lines of main work fine until that point I know the pointers, but I'm struggling with the add_order method and it doesn't work in printf eithe

access pointer array inside pointer

Natico I want to create a method called add_order that adds a new order to the existing orders array. The first three lines of main work fine until that point I know the pointers, but I'm struggling with the add_order method and it doesn't work in printf eithe

access pointer array inside pointer

Natico I want to create a method called add_order that adds a new order to the existing orders array. The first three lines of main work fine until that point I know the pointers, but I'm struggling with the add_order method and it doesn't work in printf eithe

Access array elements by pointer?

Daniel Again to []vs *: void func(const instance* I, const float* in, float* out) { for(int i=0;i<N; i++) { // some calculation goes here out[i] = ... in[i] * I.something[i] ... ; } } I often find that sub-function code is written

access array by pointer

username let int * ptr,array[10]; ptr= array;. Now, each storage cell in a contiguous position of the array has a fixed size. If the address of the first cell is 1234, the address of the next cell must be 1238. But we use pointer as access to it *(ptr+1). I'm

Access array elements by pointer?

Daniel Again to []vs *: void func(const instance* I, const float* in, float* out) { for(int i=0;i<N; i++) { // some calculation goes here out[i] = ... in[i] * I.something[i] ... ; } } I often find that sub-function code is written

Access array elements by pointer?

Daniel Again to []vs *: void func(const instance* I, const float* in, float* out) { for(int i=0;i<N; i++) { // some calculation goes here out[i] = ... in[i] * I.something[i] ... ; } } I often find that sub-function code is written

Access array elements by pointer?

Daniel Again to []vs *: void func(const instance* I, const float* in, float* out) { for(int i=0;i<N; i++) { // some calculation goes here out[i] = ... in[i] * I.something[i] ... ; } } I often find that sub-function code is written

Access array elements by pointer?

Daniel Again to []vs *: void func(const instance* I, const float* in, float* out) { for(int i=0;i<N; i++) { // some calculation goes here out[i] = ... in[i] * I.something[i] ... ; } } I often find that sub-function code is written

Access array elements by pointer?

Daniel Again to []vs *: void func(const instance* I, const float* in, float* out) { for(int i=0;i<N; i++) { // some calculation goes here out[i] = ... in[i] * I.something[i] ... ; } } I often find that sub-function code is written

Access array elements by pointer?

Daniel Again to []vs *: void func(const instance* I, const float* in, float* out) { for(int i=0;i<N; i++) { // some calculation goes here out[i] = ... in[i] * I.something[i] ... ; } } I often find that sub-function code is written

access array by pointer

username let int * ptr,array[10]; ptr= array;. Now, each storage cell in a contiguous position of the array has a fixed size. If the address of the first cell is 1234, the address of the next cell must be 1238. But we use pointer as access to it *(ptr+1). I'm

C++: Get iterator from pointer to array of doubles

Avishek Dutta I have a pointer to n cells that I want to allocate. Now I need to access the begin and end iterator objects of this pointer. Here is my code: * my_module.cpp * # include c_vector.h /* .. */ C_Vector a(n); * c_vector.h * class C_Vector{ /* ..

C++ pointer declaration access pointer array

Sputten I need help with pointer declarations, for example I have several classes with arrays of pointers. const char* const clsMainWin::mcpszXMLattrRoot[] = {"bottom","left","right","top",NULL}; const char* const clsMainWin::mcpszXMLattrA[] = {"x","y","z",NUL