array initializer / conversion to pointer

A

Alexander Stippler

Hello,

I've got a quite simple question. I want to have something like

int
main()
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
}

The compiler complains an not allowed conversion in the assignment
field = array;
It's neccessary that field is of type double**. How to manage an
initialization like the above?

regards,
alex
 
J

Joona I Palaste

Alexander Stippler said:
I've got a quite simple question. I want to have something like
int
main()
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
}
The compiler complains an not allowed conversion in the assignment
field = array;
It's neccessary that field is of type double**. How to manage an
initialization like the above?

Declare field as double (*field)[3] instead.
 
A

Al Bowers

Alexander said:
Hello,

I've got a quite simple question. I want to have something like

int
main()
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
}

The compiler complains an not allowed conversion in the assignment
field = array;
It's neccessary that field is of type double**. How to manage an
initialization like the above?

This is a faq question and answer. Read faq question 6.18 located:
http://www.eskimo.com/~scs/C-faq/q6.18.html

As the faq suggests, make this:
double (*field)[3] = array;
An example is shown below using in function PrintARRAY, using a
typedef.

If, as you say, it is neccessary that field be type double**, then
you can still do it at the expense of making access to the elements
a little more difficult. This is shown in functions PrintARRAY2 and
PrintARRAY3 below.

#include <stdio.h>

#define ROW 3
#define COL 4

typedef double (*ARR)[COL];

void PrintARRAY(ARR row);
void PrintARRAY2(double **field);
void PrintARRAY3(double **field);

int main(void)
{
double array[ROW][COL] = {{1,2,3,11}, {4,5,6,22}, {7,8,9,33}};
ARR this = array;
double **field = (double **)array;

puts("Using function PrintARRAY");
PrintARRAY(this);
puts("\nUsing function printARRAY2");
PrintARRAY2(field);
puts("\nUsing function printARRAY3");
PrintARRAY3(field);
return 0;
}

void PrintARRAY(ARR row)
{
int i,j;

for(i = 0; i < ROW;i++)
{
for(j = 0; j < COL; j++)
printf("%.2f ",row[j]);
putchar('\n');
}
return;
}

void PrintARRAY2(double **field)
{
int i,j;
double (*arr)[COL] = (double (*)[COL])field;

for(i = 0; i < ROW;i++,arr++)
{
for(j = 0; j < COL; j++)
printf("%.2f ",(*arr)[j]);
putchar('\n');
}
return;
}

void PrintARRAY3(double **field)
{
int i,j;
double *arr= (double *)field;

for(i = 0;i < ROW; i++)
{
for(j = 0; j < COL;j++)
printf("%.2f ",arr[i*COL+j]);
putchar('\n');
}
return;
}
 
A

Alexander Stippler

Joona said:
Alexander Stippler said:
I've got a quite simple question. I want to have something like
int
main()
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
}
The compiler complains an not allowed conversion in the assignment
field = array;
It's neccessary that field is of type double**. How to manage an
initialization like the above?

Declare field as double (*field)[3] instead.

Sorry, but I cannot influence the type of field, as I already mentioned in
my first posting! It is 'external' to my influence. I need it to be
double **, so what?

best regards, alex
 
M

Mark McIntyre

Joona said:
Alexander Stippler said:
I've got a quite simple question. I want to have something like
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
The compiler complains an not allowed conversion in the assignment
field = array;
Declare field as double (*field)[3] instead.
Sorry, but I cannot influence the type of field, as I already mentioned in
my first posting! It is 'external' to my influence. I need it to be
double **, so what?

So then you are in trouble. You can't assign the array to the pointer. You
will have to alloc memory for an intermediate variable of type double**,
point field to it, and copy the data from array into it.
 
L

Leor Zolman

ARR this = array; ....
PrintARRAY(this);
....

this sure jumped out at me. I mean, 'this'.

The most recent post I found Googling for "C++ keywords" was from
1999...I'd wager there have probably been at least /some/ more recent
discussions on this topic, but searching for 'this' probably wouldn't have
been fruitful. So I thought I'd take the opportunity to revisit the topic.

I on general principles avoid using anything I'm aware is a C++ keyword in
C programs. I personally end up compiling lots of code as both C and C++,
so in my case developing that habit was a no-brainer. I suggest, however,
that "better safe than sorry" is a good enough reason for anyone who knows
the keyword conflicts to avoid using C++ keywords in C programs.

That 1999 posting mentioned some counter-arguments, such as "Well, why not
avoid Java and Perl and Python keywords, too?" and "There are enough
porting issues between C and C++ that keyword collisions would be a
relatively minor issue." But I still thing avoiding them would be a Good
Idea on general principles.

Thoughts?
-leor
 
D

Dan Pop

In said:
...

this sure jumped out at me. I mean, 'this'.

The most recent post I found Googling for "C++ keywords" was from
1999...I'd wager there have probably been at least /some/ more recent
discussions on this topic, but searching for 'this' probably wouldn't have
been fruitful. So I thought I'd take the opportunity to revisit the topic.

I on general principles avoid using anything I'm aware is a C++ keyword in
C programs. I personally end up compiling lots of code as both C and C++,
so in my case developing that habit was a no-brainer. I suggest, however,
that "better safe than sorry" is a good enough reason for anyone who knows
the keyword conflicts to avoid using C++ keywords in C programs.

Thoughts?

As well written C code seldom qualifies as well written C++ code, too,
there is little point in "porting" C code to C++. Compile it as C code
and link it to the rest of the C++ application: the C++ language provides
explicit support for this approach. In this case, there is no point in
paying *any* attention to the C++ keywords or reserved identifiers.
Paying attention to the C99 reserved identifiers is already a non-trivial
task...

Of course, if the project specification (or the customer) *requires* that
the code is compilable as both C and C++, it must be written in the
common subset of the two languages and this automatically rules out the
usage of any C++ keywords as identifiers. Until now, only P.J. Plauger
claimed that he had to satisfy such customer requirements.

Dan
 
M

Martin Ambuhl

Leor said:
I on general principles avoid using anything I'm aware is a C++ keyword in
C programs.

On genereal principles, I avoid any language that steals the common and
useful identifiers "this" and "new".
 
A

Alan Balmer

On genereal principles, I avoid any language that steals the common and
useful identifiers "this" and "new".

I recently had a disagreement with the people at Slickedit, who make
my otherwise favorite editor. They don't distinguish between C and C++
in their syntax recognition. I had a legacy program with a variable
name "class", and it refused to tag it.
 
J

Joe Wright

Alexander said:
Joona I Palaste wrote:

Alexander Stippler said:
I've got a quite simple question. I want to have something like
int
main()
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
}
The compiler complains an not allowed conversion in the assignment
field = array;
It's neccessary that field is of type double**. How to manage an
initialization like the above?

Declare field as double (*field)[3] instead.


Sorry, but I cannot influence the type of field, as I already mentioned in
my first posting! It is 'external' to my influence. I need it to be
double **, so what?

best regards, alex

I've seen some of the advice you've received. The best by far is
'Read the FAQ'. Please do so. You will not be sorry, I promise.

If you're stuck with ..

double **field;

... do this.

#include <stdlib.h>
#define ROW 3
#define COL 3
...
int r, c, d = 0;
...
field = malloc(ROW * sizeof *field);
for (r = 0; r < ROW; ++r)
field[r] = malloc(COL * sizeof **field);

for (r = 0; r < ROW; ++r)
for (c = 0; c < COL; ++c)
field[r][c] = ++d;
 
B

Barry Schwarz

Hello,

I've got a quite simple question. I want to have something like

int
main()
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
}

The compiler complains an not allowed conversion in the assignment
field = array;
It's neccessary that field is of type double**. How to manage an
initialization like the above?
Since you claim you cannot change the type of field, your only other
option is to change the type of array. You can use

double *array[3];
double xarray[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
for (i = 0; i < 2; i++)
array = xarray;
field = array;

All three variables (array, xarray, and field) can use double
subscript notation (e.g., field[j]) to reference individual
doubles.


<<Remove the del for email>>
 
P

Peter Shaggy Haywood

Groovy hepcat Alexander Stippler was jivin' on Thu, 27 May 2004
15:54:10 +0200 in comp.lang.c.
Re: array initializer / conversion to pointer's a cool scene! Dig it!
Joona said:
Alexander Stippler said:
int
main()
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
}
The compiler complains an not allowed conversion in the assignment
field = array;
It's neccessary that field is of type double**. How to manage an
initialization like the above?

Declare field as double (*field)[3] instead.

Sorry, but I cannot influence the type of field, as I already mentioned in
my first posting! It is 'external' to my influence. I need it to be
double **, so what?

Then try something like this:

int main(void)
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
double *foo[3];
int i;

for(i = 0; i < (sizeof foo / sizeof *foo); i++)
foo = array;

field = foo;
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
E

E. Robert Tisdale

Alexander said:
I've got a quite simple question. I want to have something like

int
main(int argc, char* argv[]) {
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field = &(array[0][0]);

This is probably what you meant.
return 0;
}

The compiler complains an not allowed conversion in the assignment

field = array;

It's neccessary that field is of type double**.
How to manage an initialization like the above?
> cat main.c
#include <stdio.h>
#include <stdlib.h>

void f(size_t m, size_t n, const double a[m][n]) {
for (size_t i = 0; i < m; ++i) {
for (size_t j = 0; j < n; ++j)
fprintf(stdout, " %f", a[j]);
fprintf(stdout, "\n");
}
}

int
main(int argc, char* argv[]) {
const size_t m = 3;
const size_t n = 3;
const double array[][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
fprintf(stdout, "array =\n");
f(m, n, array);
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main
array =
1.000000 2.000000 3.000000
4.000000 5.000000 6.000000
7.000000 8.000000 9.000000
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top