473,791 Members | 3,105 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
12 1576
On Thu, 27 May 2004 14:06:45 +0200, Alexander Stippler
<st**@mathemati k.uni-ulm.de> 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
initializati on 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[i] = xarray[i];
field = array;

All three variables (array, xarray, and field) can use double
subscript notation (e.g., field[i][j]) to reference individual
doubles.
<<Remove the del for email>>
Nov 14 '05 #11
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 I Palaste wrote:
Alexander Stippler <st**@mathemati k.uni-ulm.de> scribbled the following:
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[i] = array[i];

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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #12
Alexander Stippler wrote:
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[i][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
Nov 14 '05 #13

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

Similar topics

4
15214
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
7101
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
13124
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
2744
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
152
9925
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
6800
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
9669
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
9515
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
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
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
9995
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...
1
7537
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
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.