473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

multi dimension array initialization

Hi,
I'm new to C so hopefully someone can give me some guidance on where
I've gone wrong.

I've written the following code, trying to initialize the multi-dim
array but it's not working:

#include <stdio.h>
#include <ctype.h>

#define ROW 2
#define COL 5

void init_array();

main ()
{
char array[ROW][COL];

init_array(array);
return 0;
}

void init_array(char data[][])
{
int r, c;

for (r=0; r<=ROW; r++)
{
for (c=0; c<=COL; c++)
{
data[r][c] = NULL;
}
}
}

But when I try to compile with gcc, I'm getting this message
a2.c: In function `init_array':
a2.c:28: arithmetic on pointer to an incomplete type

Can anyone explain what the error means in english ?
What am I doing wrong ?

Nat

I've tried posting to comp.lang.c.moderated but I must have done
something incorrect as it hasn't been posted yet. My appologies if it
appears twice.

Nov 15 '05 #1
11 5060
na****@yahoo.com.au wrote on 17/09/05 :
#include <stdio.h>
#include <ctype.h>

#define ROW 2
#define COL 5

void init_array();
Not-a-prototype (IOW, useless declaration.)
main ()
{
char array[ROW][COL];

init_array(array);
return 0;
}

void init_array(char data[][])
Dimensions required (the leftmost can be ommited). Better to pass the
dimensions too. (Or use a pointer to array).
{
int r, c;

for (r=0; r<=ROW; r++)
You are going ot of the limits by 1.
{
for (c=0; c<=COL; c++)
ditto.
{
data[r][c] = NULL;
NULL is for pointers. Use 0 for an int.
}
}
}


Try that. Ask for details you don't understand.

#include <stdio.h>
#include <ctype.h>

#define ROW 2
#define COL 5

static void init_array (char data[][COL], size_t row, size_t col)
{
size_t r;

for (r = 0; r < row; r++)
{
size_t c;
for (c = 0; c < col; c++)
{
data[r][c] = r + c;
}
}
}

static void print_array (char const data[][COL], size_t row, size_t
col)
{
size_t r;

for (r = 0; r < row; r++)
{
size_t c;
for (c = 0; c < col; c++)
{
printf ("%3d", data[r][c]);
}
printf ("\n");
}
}

int main (void)
{
char array[ROW][COL];

init_array (array, ROW, COL);
print_array (array, ROW, COL);
return 0;
}

0 1 2 3 4
1 2 3 4 5

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
Nov 15 '05 #2
na****@yahoo.com.au wrote:

Hi,
I'm new to C so hopefully someone can give me some guidance on where
I've gone wrong.

I've written the following code, trying to initialize the multi-dim
array but it's not working:

#include <stdio.h>
#include <ctype.h>

#define ROW 2
#define COL 5

void init_array();

main ()
{
char array[ROW][COL];

init_array(array);
return 0;
}

void init_array(char data[][])
{
int r, c;

for (r=0; r<=ROW; r++)
{
for (c=0; c<=COL; c++)
{
data[r][c] = NULL;
}
}
}

But when I try to compile with gcc, I'm getting this message
a2.c: In function `init_array':
a2.c:28: arithmetic on pointer to an incomplete type

Can anyone explain what the error means in english ?


I wish I could.
I'm not good at explantions.

/* BEGIN new.c */

#include <stdio.h>

#define ROW 2
#define COL 5

void init_array(char (*data)[5]);
void print_array(char (*data)[5]);

int main (void)
{
char array[ROW][COL];

init_array(array);
print_array(array);
return 0;
}

void init_array(char (*data)[5])
{
int r, c;

for (r = 0; r != ROW; ++r) {
for (c = 0; c != COL; ++c) {
data[r][c] = (char)(r + c);
}
}
}

void print_array(char (*data)[5])
{
int r, c;

for (r = 0; r != ROW; ++r) {
for (c = 0; c != COL; ++c) {
printf("%d ", data[r][c]);
}
}
putchar('\n');
}

/* END new.c */
--
pete
Nov 15 '05 #3
na****@yahoo.com.au wrote:
Hi,

#include <stdio.h>
#include <ctype.h> You're not using anything from ctype.h, then why include it?

#define ROW 2
#define COL 5

void init_array(); When you declare a prototype, make sure you declare the parameters too,
improves readability. And if I'm not wrong, then a C compiler assumes
integer parameters when none are specified in the prototype.

main () It should be "int main(void)"
{
char array[ROW][COL];

init_array(array);
return 0;
}

void init_array(char data[][]) For unsized arrays, you can leave out only the leftmost dimension.
You must specify the rest. So correct the above as:
"void init_array(char data[][COL])"
{
int r, c;

for (r=0; r<=ROW; r++)
{
for (c=0; c<=COL; c++)
{
data[r][c] = NULL; Use "data[r][c] = 0;". NULL is defined as a pointer to nothing, the
above statement will cause this warning: "assignment makes integer from
pointer without a cast"
}
}
}

But when I try to compile with gcc, I'm getting this message
a2.c: In function `init_array':
a2.c:28: arithmetic on pointer to an incomplete type

Can anyone explain what the error means in english ?
What am I doing wrong ?

Nat

I've tried posting to comp.lang.c.moderated but I must have done
something incorrect as it hasn't been posted yet. My appologies if it
appears twice.


cheers,
forayer

--
If you would be a real seeker after truth, it is necessary that at least
once in your life you doubt, as far as possible, all things."
-- Rene Descartes
Nov 15 '05 #4
Vimal Aravindashan wrote on 17/09/05 :
void init_array(); When you declare a prototype,


It's not a prototype. It's a declaration.
make sure you declare the parameters too,
improves readability.
and it makes it a separated prototype.
And if I'm not wrong, then a C compiler assumes integer
parameters when none are specified in the prototype.


Nope. As a function declaration, it assumes that the parameters are
unspecified.

OTOH,

int f()
{
...
}

is a function with no parameters. Similar to

int f(void)
{
...
}

except that the first form has not a prototype, but the second has one.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
Nov 15 '05 #5
Emmanuel Delahaye wrote:
Vimal Aravindashan wrote on 17/09/05 :
void init_array();
When you declare a prototype,

It's not a prototype. It's a declaration.

Oops my mistake, careless choice of words, sorry. :-)
I meant "When you declare a funtion, ..."
make sure you declare the parameters too, improves readability.

and it makes it a separated prototype.
And if I'm not wrong, then a C compiler assumes integer parameters
when none are specified in the prototype.

Nope. As a function declaration, it assumes that the parameters are
unspecified.

Yes, but in case of implicit declarations it does assume integer return
type and integer parameters.

OTOH,

int f()
{
...
}

is a function with no parameters. Similar to

int f(void)
{
...
}

except that the first form has not a prototype, but the second has one.

--
If you would be a real seeker after truth, it is necessary that at least
once in your life you doubt, as far as possible, all things."
-- Rene Descartes
Nov 15 '05 #6
Vimal Aravindashan <fo*****@dontspamme.pls> writes:
na****@yahoo.com.au wrote:

[...]
{
int r, c;
for (r=0; r<=ROW; r++)
{
for (c=0; c<=COL; c++)
{
data[r][c] = NULL;

Use "data[r][c] = 0;". NULL is defined as a pointer to nothing, the
above statement will cause this warning: "assignment makes integer
from pointer without a cast"


Not necessarily. An unadorned 0 is a valid null pointer constant, and
therefore a valid expansion for the NULL macro. You certainly
shouldn't assign NULL to an int object, but it's
implementation-defined whether the compiler will warn you about it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #7
Thank you all for your advice. It's been very helpful.

I messed up my prototyping ( book said you can just leave it blank )
but following your advice:
my prototype is void init_array(char data[][COL]);
and it's function init_array(char data[][COL]);
and changed the loop condition so it doesn't go over by one.
The rest of the code is left as is and it works !!!

Nov 15 '05 #8
na****@yahoo.com.au writes:
Thank you all for your advice. It's been very helpful.

I messed up my prototyping ( book said you can just leave it blank )
but following your advice:
my prototype is void init_array(char data[][COL]);
and it's function init_array(char data[][COL]);
and changed the loop condition so it doesn't go over by one.
The rest of the code is left as is and it works !!!


Get a better book. K&R2 (Kernighan & Ritchie, "The C Programming
Language", 2nd edition) is a good one.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #9
Vimal Aravindashan wrote:
Emmanuel Delahaye wrote:
Vimal Aravindashan wrote on 17/09/05 :
void init_array();

When you declare a prototype,


It's not a prototype. It's a declaration.


Oops my mistake, careless choice of words, sorry. :-)
I meant "When you declare a funtion, ..."
make sure you declare the parameters too, improves readability.


and it makes it a separated prototype.
And if I'm not wrong, then a C compiler assumes integer parameters
when none are specified in the prototype.


Nope. As a function declaration, it assumes that the parameters are
unspecified.


Yes, but in case of implicit declarations it does assume integer return
type and integer parameters.


No, you get implicit int for the return type in C89 if you don't specify
one, but for the arguments the default promotions will apply. So if you
pass a char it will on most systems be promoted to an int, but if you
pass a pointer will be passed as the pointer type you passed.

<snip>
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #10
na****@yahoo.com.au wrote
(in article
<11**********************@g44g2000cwa.googlegroups .com>):
I messed up my prototyping ( book said you can just leave it blank )


What book? The author wouldn't happen to be "Schildt" by any
chance? If so, set fire to it, erase from your memory everthing
contained within, and start over with a new book.

--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #11
"Randy Howard" <ra*********@FOOverizonBAR.net> wrote in message
news:00*****************************@news.verizon. net...
What book? The author wouldn't happen to be "Schildt" by any
chance? If so, set fire to it, erase from your memory everthing
contained within, and start over with a new book.


And never ever by any books by him but perhaps fairy tales :) Recommend his
books only to your enemies if you've got any :)

Alex
Nov 15 '05 #12

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

Similar topics

1
by: sugaray | last post by:
I don't get why the correct way to allocate memory space for 3d-array should be like this in C++: long (*ptr); ptr=new long; and when deallocation delete ptr;
6
by: Adam Hartshorne | last post by:
The input to a function of a 3rd party library I want to use requires a double**, which is a multi-dimension array of doubles. I have looked on the net etc and seen several ways of supposedly...
8
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
3
by: Eric Laberge | last post by:
Aloha! I've been reading the standard (May '05 draft, actually) and stumbled across this: 6.7.1 Initialization §20 "If the aggregate or union contains elements or members that are aggregates...
8
by: kd | last post by:
Newbie question here. It's been a while since I've done C programming, and I hit a wall last night. Let's say I have a three dimensional array, like so: int p = {{{0,0,0}, {1,1,1},...
4
by: chy1013m1 | last post by:
I am slightly confused as to how to reference multi-dimensional array with pointers. I've tried the following code, and I was able to reference 33 as pptr int multi = {{11,27,33}, {12,13,14}};...
4
by: Gernot Frisch | last post by:
Hi, I need a class, that has a 4 dimensional array (can be 3 dimensional, too) with such an operator: T operator()(int x1, int x2=0, int x3=0, int x4=0); that can be used as:
152
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 = { {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.