Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > array initializer / conversion to pointer

Reply
Thread Tools

array initializer / conversion to pointer

 
 
Alexander Stippler
Guest
Posts: n/a
 
      05-27-2004
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
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      05-27-2004
Alexander Stippler <> scribbled the following:
> 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?


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

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
- Mika P. Nieminen
 
Reply With Quote
 
 
 
 
Al Bowers
Guest
Posts: n/a
 
      05-27-2004


Alexander Stippler wrote:
> 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[i][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;
}


--
Al Bowers
Tampa, Fl USA
mailto: (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
Alexander Stippler
Guest
Posts: n/a
 
      05-27-2004
Joona I Palaste wrote:

> Alexander Stippler <> scribbled the following:
>> 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?

>
> 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
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      05-27-2004
On Thu, 27 May 2004 15:54:10 +0200, in comp.lang.c , Alexander Stippler
<> wrote:

>Joona I Palaste wrote:
>
>> Alexander Stippler <> scribbled the following:
>>> 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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      05-27-2004
On Thu, 27 May 2004 09:48:26 -0400, Al Bowers <>
wrote:

> 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

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      05-27-2004
In <> Leor Zolman <> writes:

>On Thu, 27 May 2004 09:48:26 -0400, Al Bowers <>
>wrote:
>
>> 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.
>
>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
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-27-2004
Leor Zolman wrote:

> 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".
 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      05-27-2004
On Thu, 27 May 2004 13:16:27 -0400, Martin Ambuhl
<> wrote:

>Leor Zolman wrote:
>
>> 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".


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.

--
Al Balmer
Balmer Consulting

 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      05-28-2004
Alexander Stippler wrote:
> Joona I Palaste wrote:
>
>
>>Alexander Stippler <> scribbled the following:
>>
>>>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?

>>
>>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;


--
Joe Wright private.php?do=newpm&u=
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Cast a pointer to array to base class pointer to array Hansen C++ 3 04-24-2010 03:30 PM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments