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 variable dis a pointer to an array of doubles or an array of pointers to doubles.

double k;
(*d)[0] = k; // runtime error using gcc compiler
d[0] = &k;   // Compilation error, assignment to expression with array type
*d = &k;     // Compilation error, assignment to expression with array type
Meskobalazs

The dvariable is a pointer to a 3 - length doublearray . So you can assign a pointer of double[3]an array to it . E.g:

double (*d)[3];
double a[3] = {1.0, 2.0, 3.0}
d = &a; 

However, to make it more practical, dynamic memory allocation can also be used. E.g:

double (*d)[3];
d = malloc(3 * sizeof(double));
d[0][0] = 1.0;
d[0][1] = 2.0;
d[0][2] = 3.0;
printf("%f %f %f\n", d[0][0], d[0][1], d[0][2]);

This dwill point to a 3-length doublearray. The program will give the following output:

1.0 2.0 3.0

By the way, you can replace e.g. d[0][0]with (*d)[0], they mean exactly the same thing.

Related


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

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 *

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 pointers

username I know I should know this, but it's too late and my brain can't piece it all together. A question can be straightforward: I have a structure item. I want to create a pointer to an array of pointers of that item type. E.g. struct item { int data;

C - pointer to array of pointers

Simon I have to store pointers to data structures. Therefore, I need an array of pointers. I created a simple example, but received segmentation fault. why can't i do buf->data[i] = pkt?? #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <str

pointer to array of function pointers

bitcoin Suppose I have a function int foo(void) { printf("Hello world"); } I want to use a pointer to a function to point to foo(). typedef int (*f_ptr)(void); Which case would I use for this purpose? (A) f_ptr my_ptr = foo; my_ptr(); (B) f_ptr * my_

C - pointer to array of pointers

Simon I have to store pointers to data structures. Therefore, I need an array of pointers. I created a simple example, but received segmentation fault. why can't i do buf->data[i] = pkt?? #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <str

pointer to array of function pointers

bitcoin Suppose I have a function int foo(void) { printf("Hello world"); } I want to use a pointer to a function to point to foo(). typedef int (*f_ptr)(void); Which case would I use for this purpose? (A) f_ptr my_ptr = foo; my_ptr(); (B) f_ptr * my_

assign pointer to array of pointers

grassland I have a set of pointers to image block objects. I have a derived class member function that returns a pointer to one of these image blocks, it's a virtual function so I can't change the return type without breaking everything. Here is what I have so

assign pointer to array of pointers

grassland I have a set of pointers to image block objects. I have a derived class member function that returns a pointer to one of these image blocks, it's a virtual function so I can't change the return type without breaking everything. Here is what I have so

pointer to array of pointers

username I know I should know this, but it's too late and my brain can't piece it all together. A question can be straightforward: I have a structure item. I want to create a pointer to an array of pointers of that item type. E.g. struct item { int data;

pointer to array of pointers in Pascal

Tomasz Kasperczyk I don't know how to access the contents of an array of pointers through a pointer. Here is an example: Type PInteger = ^Integer; IntegerArrayP = array of PInteger; PIntegerArrayP = ^IntegerArray; var variable: Integer; pa

pointer to array of function pointers

bitcoin Suppose I have a function int foo(void) { printf("Hello world"); } I want to use a pointer to a function to point to foo(). typedef int (*f_ptr)(void); Which case would I use for this purpose? (A) f_ptr my_ptr = foo; my_ptr(); (B) f_ptr * my_

C - pointer to array of pointers

Simon I have to store pointers to data structures. Therefore, I need an array of pointers. I created a simple example, but received segmentation fault. why can't i do buf->data[i] = pkt?? #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <str

pointer to array of function pointers

bitcoin Suppose I have a function int foo(void) { printf("Hello world"); } I want to use a pointer to a function to point to foo(). typedef int (*f_ptr)(void); Which case would I use for this purpose? (A) f_ptr my_ptr = foo; my_ptr(); (B) f_ptr * my_

pointer to array of function pointers

bitcoin Suppose I have a function int foo(void) { printf("Hello world"); } I want to use a pointer to a function to point to foo(). typedef int (*f_ptr)(void); Which case would I use for this purpose? (A) f_ptr my_ptr = foo; my_ptr(); (B) f_ptr * my_

pointer to array of function pointers

bitcoin Suppose I have a function int foo(void) { printf("Hello world"); } I want to use a pointer to a function to point to foo(). typedef int (*f_ptr)(void); Which case would I use for this purpose? (A) f_ptr my_ptr = foo; my_ptr(); (B) f_ptr * my_

assign pointer to array of pointers

grassland I have a set of pointers to image block objects. I have a derived class member function that returns a pointer to one of these image blocks, it's a virtual function so I can't change the return type without breaking everything. Here is what I have so

pointer to array of pointers in Pascal

Tomasz Kasperczyk I don't know how to access the contents of an array of pointers through a pointer. Here is an example: Type PInteger = ^Integer; IntegerArrayP = array of PInteger; PIntegerArrayP = ^IntegerArray; var variable: Integer; pa

pointer to an array of pointers to an array of char

Fifth, the United Kingdom: I'm currently learning C++ using Koenig's Accelerated C++ and I'm having some trouble with pointers to initial arrays of pointers. The book says the following code int main(int argc, char** argv) { // if there are arguments, wri

pointer to an array of pointers to an array of char

Fifth, the United Kingdom: I'm currently learning C++ using Koenig's Accelerated C++ and I'm having some trouble with pointers to initial arrays of pointers. The book says the following code int main(int argc, char** argv) { // if there are arguments, wri

Is it a mutable array of pointers or a pointer to an array?

Michael I'm learning C and have some difficulty understanding pointers and arrays. In the tutorial I read, I have the following line: char* arrP1[] = { "father","mother",NULL }; My question is what is arrP1? It is an array of pointers to static strings: Or

Is it a mutable array of pointers or a pointer to an array?

Michael I'm learning C and have some difficulty understanding pointers and arrays. In the tutorial I read, I have the following line: char* arrP1[] = { "father","mother",NULL }; My question is what is arrP1? It is an array of pointers to static strings: Or

pointer to an array of pointers to an array of char

Fifth, the United Kingdom: I'm currently learning C++ using Koenig's Accelerated C++ and I'm having some trouble with pointers to initial arrays of pointers. The book says the following code int main(int argc, char** argv) { // if there are arguments, wri

Is it a mutable array of pointers or a pointer to an array?

Michael I'm learning C and have some difficulty understanding pointers and arrays. In the tutorial I read, I have the following line: char* arrP1[] = { "father","mother",NULL }; My question is what is arrP1? It is an array of pointers to static strings: Or