Connecting Tech Pros Worldwide Forums | Help | Site Map

multi dimension array initialization

natkw1@yahoo.com.au
Guest
 
Posts: n/a
#1: Nov 15 '05
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.


Emmanuel Delahaye
Guest
 
Posts: n/a
#2: Nov 15 '05

re: multi dimension array initialization


natkw1@yahoo.com.au wrote on 17/09/05 :[color=blue]
> #include <stdio.h>
> #include <ctype.h>
>
> #define ROW 2
> #define COL 5
>
> void init_array();[/color]

Not-a-prototype (IOW, useless declaration.)
[color=blue]
> main ()
> {
> char array[ROW][COL];
>
> init_array(array);
> return 0;
> }
>
> void init_array(char data[][])[/color]

Dimensions required (the leftmost can be ommited). Better to pass the
dimensions too. (Or use a pointer to array).
[color=blue]
> {
> int r, c;
>
> for (r=0; r<=ROW; r++)[/color]

You are going ot of the limits by 1.
[color=blue]
> {
> for (c=0; c<=COL; c++)[/color]

ditto.
[color=blue]
> {
> data[r][c] = NULL;[/color]

NULL is for pointers. Use 0 for an int.
[color=blue]
> }
> }
> }[/color]

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?"


pete
Guest
 
Posts: n/a
#3: Nov 15 '05

re: multi dimension array initialization


natkw1@yahoo.com.au wrote:[color=blue]
>
> 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 ?[/color]

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
Vimal Aravindashan
Guest
 
Posts: n/a
#4: Nov 15 '05

re: multi dimension array initialization


natkw1@yahoo.com.au wrote:[color=blue]
> Hi,
>
> #include <stdio.h>
> #include <ctype.h>[/color]
You're not using anything from ctype.h, then why include it?
[color=blue]
>
> #define ROW 2
> #define COL 5
>
> void init_array();[/color]
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.
[color=blue]
>
> main ()[/color]
It should be "int main(void)"
[color=blue]
> {
> char array[ROW][COL];
>
> init_array(array);
> return 0;
> }
>
> void init_array(char data[][])[/color]
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])"
[color=blue]
> {
> int r, c;
>
> for (r=0; r<=ROW; r++)
> {
> for (c=0; c<=COL; c++)
> {
> data[r][c] = NULL;[/color]
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"
[color=blue]
> }
> }
> }
>
> 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.
>[/color]

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
Emmanuel Delahaye
Guest
 
Posts: n/a
#5: Nov 15 '05

re: multi dimension array initialization


Vimal Aravindashan wrote on 17/09/05 :[color=blue][color=green]
>> void init_array();[/color]
> When you declare a prototype,[/color]

It's not a prototype. It's a declaration.
[color=blue]
> make sure you declare the parameters too,
> improves readability.[/color]

and it makes it a separated prototype.
[color=blue]
> And if I'm not wrong, then a C compiler assumes integer
> parameters when none are specified in the prototype.[/color]

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


Vimal Aravindashan
Guest
 
Posts: n/a
#6: Nov 15 '05

re: multi dimension array initialization


Emmanuel Delahaye wrote:[color=blue]
> Vimal Aravindashan wrote on 17/09/05 :
>[color=green][color=darkred]
>>> void init_array();[/color]
>>
>> When you declare a prototype,[/color]
>
>
> It's not a prototype. It's a declaration.[/color]
Oops my mistake, careless choice of words, sorry. :-)
I meant "When you declare a funtion, ..."
[color=blue]
>[color=green]
>> make sure you declare the parameters too, improves readability.[/color]
>
>
> and it makes it a separated prototype.
>[color=green]
>> And if I'm not wrong, then a C compiler assumes integer parameters
>> when none are specified in the prototype.[/color]
>
>
> Nope. As a function declaration, it assumes that the parameters are
> unspecified.[/color]
Yes, but in case of implicit declarations it does assume integer return
type and integer parameters.
[color=blue]
>
> 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.
>[/color]


--
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
Keith Thompson
Guest
 
Posts: n/a
#7: Nov 15 '05

re: multi dimension array initialization


Vimal Aravindashan <forayer@dontspamme.pls> writes:[color=blue]
> natkw1@yahoo.com.au wrote:[/color]
[...][color=blue][color=green]
>> {
>> int r, c;
>> for (r=0; r<=ROW; r++)
>> {
>> for (c=0; c<=COL; c++)
>> {
>> data[r][c] = NULL;[/color]
> 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"[/color]

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) kst-u@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.
natkw1@yahoo.com.au
Guest
 
Posts: n/a
#8: Nov 15 '05

re: multi dimension array initialization


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

Keith Thompson
Guest
 
Posts: n/a
#9: Nov 15 '05

re: multi dimension array initialization


natkw1@yahoo.com.au writes:[color=blue]
> 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 !!![/color]

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

--
Keith Thompson (The_Other_Keith) kst-u@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.
Flash Gordon
Guest
 
Posts: n/a
#10: Nov 15 '05

re: multi dimension array initialization


Vimal Aravindashan wrote:[color=blue]
> Emmanuel Delahaye wrote:
>[color=green]
>> Vimal Aravindashan wrote on 17/09/05 :
>>[color=darkred]
>>>> void init_array();
>>>
>>> When you declare a prototype,[/color]
>>
>> It's not a prototype. It's a declaration.[/color]
>
> Oops my mistake, careless choice of words, sorry. :-)
> I meant "When you declare a funtion, ..."
>[color=green][color=darkred]
>>> make sure you declare the parameters too, improves readability.[/color]
>>
>> and it makes it a separated prototype.
>>[color=darkred]
>>> And if I'm not wrong, then a C compiler assumes integer parameters
>>> when none are specified in the prototype.[/color]
>>
>> Nope. As a function declaration, it assumes that the parameters are
>> unspecified.[/color]
>
> Yes, but in case of implicit declarations it does assume integer return
> type and integer parameters.[/color]

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.
Randy Howard
Guest
 
Posts: n/a
#11: Nov 15 '05

re: multi dimension array initialization


natkw1@yahoo.com.au wrote
(in article
<1126997550.518005.227190@g44g2000cwa.googlegroups .com>):
[color=blue]
> I messed up my prototyping ( book said you can just leave it blank )[/color]

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)

Alexei A. Frounze
Guest
 
Posts: n/a
#12: Nov 15 '05

re: multi dimension array initialization


"Randy Howard" <randyhoward@FOOverizonBAR.net> wrote in message
news:0001HW.BF60E06A021E5F94F0488550@news.verizon. net...[color=blue]
> 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.[/color]

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


Closed Thread


Similar C / C++ bytes