473,782 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array initializer / conversion to pointer

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
Nov 14 '05 #1
12 1575
Alexander Stippler <st**@mathemati k.uni-ulm.de> 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 (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
- Mika P. Nieminen
Nov 14 '05 #2


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(dou ble **field);
void PrintARRAY3(dou ble **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(fie ld);
puts("\nUsing function printARRAY3");
PrintARRAY3(fie ld);
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(dou ble **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(dou ble **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: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #3
Joona I Palaste wrote:
Alexander Stippler <st**@mathemati k.uni-ulm.de> 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
Nov 14 '05 #4
On Thu, 27 May 2004 15:54:10 +0200, in comp.lang.c , Alexander Stippler
<st**@mathemati k.uni-ulm.de> wrote:
Joona I Palaste wrote:
Alexander Stippler <st**@mathemati k.uni-ulm.de> 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.c om/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 =---
Nov 14 '05 #5
On Thu, 27 May 2004 09:48:26 -0400, Al Bowers <xa******@rapid sys.com>
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
Nov 14 '05 #6
In <4o************ *************** *****@4ax.com> Leor Zolman <le**@bdsoft.co m> writes:
On Thu, 27 May 2004 09:48:26 -0400, Al Bowers <xa******@rapid sys.com>
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: Da*****@ifh.de
Nov 14 '05 #7
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".
Nov 14 '05 #8
On Thu, 27 May 2004 13:16:27 -0400, Martin Ambuhl
<ma*****@earthl ink.net> 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
re************* ***********@att .net
Nov 14 '05 #9
Alexander Stippler wrote:
Joona I Palaste wrote:

Alexander Stippler <st**@mathemati k.uni-ulm.de> 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
initializati on 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 mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
15213
by: John Ratliff | last post by:
How can you define a static character pointer array in C++? ex: class tmp { private: static const char *ARR; }; const char *tmp::ARR =
20
7099
by: Petter Reinholdtsen | last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The problematic part is '("a")'. I am sure 'char a = "a";' is valid ANSI C, but I am more unsure if it is allowed to place () around the string literal.
204
13118
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
28
2452
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions 1: Why cannot I do the following:
11
1980
by: copx | last post by:
Unforuntately, I know next to nothing about ASM and compiler construction, and while I was aware of the syntactic differences between pointers and arrays, I was not aware of this: http://udrepper.livejournal.com/13851.html (Yes, it is livejournal, but it is the journal of the glibc maintainer) I am not sure I really understand this low-level difference between pointers and arrays, and I do not have the time to learn ASM and compiler...
15
636
by: Jess | last post by:
Hi, If I have an array of pointer like: char* a = {"a","b","c"}; then it works fine. Since "a" is effectively "a" char**, I tried the following, which doesn't work: char** a = {"a","b","c"};
10
2743
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
152
9911
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
16
6798
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.