multi dimension array initialization

N

natkw1

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.
 
E

Emmanuel Delahaye

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

pete

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 */
 
V

Vimal Aravindashan

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.
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
 
E

Emmanuel Delahaye

Vimal Aravindashan wrote on 17/09/05 :
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
 
V

Vimal Aravindashan

Emmanuel said:
Vimal Aravindashan wrote on 17/09/05 :



It's not a prototype. It's a declaration.
Oops my mistake, careless choice of words, sorry. :)
I meant "When you declare a funtion, ..."
and it makes it a separated 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.
 
K

Keith Thompson

Vimal Aravindashan said:
(e-mail address removed) 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.
 
N

natkw1

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

Keith Thompson

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.
 
F

Flash Gordon

Vimal said:
Oops my mistake, careless choice of words, sorry. :)
I meant "When you declare a funtion, ..."


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

Randy Howard

(e-mail address removed) wrote
(in article
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.
 
A

Alexei A. Frounze

Randy Howard said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top