Newbie, 2d array declaration, init... question

T

T.w.

Hello,

I am learning C progressing from script languages, learning curve a
bit steep, but I am learning..

When I try to init a 2d array like the following example todo matrix
arithmetic the compiler complains about braces.

int filter[fw][fh] = {
0, 0, 0,
0, 1, 0,
0, 0, 0
};

But { 0, 0, 0 }, etc.. doesn't work also ?
What should I change to represent three rows with three values ?

Thankx a lot..

T.w.
 
A

Allan Bruce

T.w. said:
Hello,

I am learning C progressing from script languages, learning curve a
bit steep, but I am learning..

When I try to init a 2d array like the following example todo matrix
arithmetic the compiler complains about braces.

int filter[fw][fh] = {
0, 0, 0,
0, 1, 0,
0, 0, 0
};

But { 0, 0, 0 }, etc.. doesn't work also ?
What should I change to represent three rows with three values ?

Thankx a lot..

T.w.

You want something like this:

int a[3][3] = {{0,0,0},{1,2,3},{9,8,7}};

Allan
 
E

Emmanuel Delahaye

T.w. wrote on 27/03/05 :
Hello,

I am learning C progressing from script languages, learning curve a
bit steep, but I am learning..

When I try to init a 2d array like the following example todo matrix
arithmetic the compiler complains about braces.

int filter[fw][fh] = {
0, 0, 0,
0, 1, 0,
0, 0, 0
};

But { 0, 0, 0 }, etc.. doesn't work also ?
What should I change to represent three rows with three values ?

int filter[fw][fh] = {
{0, 0, 0},
{0, 1, 0},
{0, 0, 0}
};

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

T.w.

T.w. wrote on 27/03/05 :
Hello,

I am learning C progressing from script languages, learning curve a
bit steep, but I am learning..

When I try to init a 2d array like the following example todo matrix
arithmetic the compiler complains about braces.

int filter[fw][fh] = {
0, 0, 0,
0, 1, 0,
0, 0, 0
};

But { 0, 0, 0 }, etc.. doesn't work also ?
What should I change to represent three rows with three values ?

int filter[fw][fh] = {
{0, 0, 0},
{0, 1, 0},
{0, 0, 0}
};

Ahh.. ok guys.. thnkx.. !
 
T

T.w.

T.w. said:
Hello,

I am learning C progressing from script languages, learning curve a
bit steep, but I am learning..

When I try to init a 2d array like the following example todo matrix
arithmetic the compiler complains about braces.

int filter[fw][fh] = {
0, 0, 0,
0, 1, 0,
0, 0, 0
};

But { 0, 0, 0 }, etc.. doesn't work also ?
What should I change to represent three rows with three values ?

Thankx a lot..

T.w.

You want something like this:

int a[3][3] = {{0,0,0},{1,2,3},{9,8,7}};

Allan

Sorry to ask, but does that mean that I can do something as:

struct filter {
double factor;
double bias;
int fw;
int fh;
int ftmat[fw][fh];
};

struct filter ftab[] = {
{ 1.0, 0.0, 3, 3, { {0,0,0}, {0,1,0}, {0,0,0} } }
};

If I would use ftmat[][] the compiler doesn't know how big the arrays
have to be I guess. But in this case it has to search back for the
size of each array can that be possible ?
Grouping all var's together would be a nice option.. If it's
possible.. ?

Thnkx..

T.w. not Tv..
 
B

Barry Schwarz

Sorry to ask, but does that mean that I can do something as:

struct filter {
double factor;
double bias;
int fw;
int fh;
int ftmat[fw][fh];
};

struct filter ftab[] = {
{ 1.0, 0.0, 3, 3, { {0,0,0}, {0,1,0}, {0,0,0} } }
};

If I would use ftmat[][] the compiler doesn't know how big the arrays
have to be I guess. But in this case it has to search back for the
size of each array can that be possible ?
Grouping all var's together would be a nice option.. If it's
possible.. ?

You can't use ftmat[][]. If you want the compiler to perform the
initialization, then you must specify the size of each dimension
except the first. If you wish, you can let the compiler determine the
size of the first dimension by counting the number of initialization
groups exist for the array.

And unless you have a C99 compiler, the specification must be a
compile time constant, not the name of a variable.


<<Remove the del for email>>
 
D

Dick D

Help! can someone tell me why i can't access fname[1][1]?

Thanks.



void initialize(char **fname,char **lname,float **scores,char *lgrade,int
*records)
{
int i,j;

printf("How many records do you have?");
scanf("%d",records);
printf("\n");

fname = (char **) calloc(*records,sizeof(char *));
for(i = 0; i < *records; i++)
fname = (char *) calloc(50,sizeof(char));

printf("%c \n",fname[1][1]);
printf("%d \n",scores[2][2]);
}

int main()
{
char **lname;
char **fname;
float **scores;
char *lgrade;

initialize(fname,lname,scores,lgrade,&records);
}
 
B

Barry Schwarz

Help! can someone tell me why i can't access fname[1][1]?

Thanks.



void initialize(char **fname,char **lname,float **scores,char *lgrade,int
*records)
{
int i,j;

printf("How many records do you have?");
scanf("%d",records);
printf("\n");

fname = (char **) calloc(*records,sizeof(char *));
for(i = 0; i < *records; i++)
fname = (char *) calloc(50,sizeof(char));

printf("%c \n",fname[1][1]);


If you get this far, fname[1][1] contains a value of all bits 0,
courtesy of calloc. This is not a printable/displayable character in
most character sets. What did you expect printf to do with it?

By the way, when you exit this function, the value in fname will be
lost since C passes by value. The variable fname in main will remain
unchanged.
printf("%d \n",scores[2][2]);

This invokes undefined behavior since scores is never initialized.
}

int main()
{
char **lname;
char **fname;
float **scores;
char *lgrade;

initialize(fname,lname,scores,lgrade,&records);

This would invoke undefined behavior if it weren't for the syntax
error. The first four arguments were never initialized and the
function call will attempt to evaluate them. In any case, records is
undefined so you cannot evaluate its address.



<<Remove the del for email>>
 
T

T.w.

Sorry to ask, but does that mean that I can do something as:

struct filter {
double factor;
double bias;
int fw;
int fh;
int ftmat[fw][fh];
};

struct filter ftab[] = {
{ 1.0, 0.0, 3, 3, { {0,0,0}, {0,1,0}, {0,0,0} } }
};

If I would use ftmat[][] the compiler doesn't know how big the arrays
have to be I guess. But in this case it has to search back for the
size of each array can that be possible ?
Grouping all var's together would be a nice option.. If it's
possible.. ?

You can't use ftmat[][]. If you want the compiler to perform the
initialization, then you must specify the size of each dimension
except the first. If you wish, you can let the compiler determine the
size of the first dimension by counting the number of initialization
groups exist for the array.

Euh, so If I understand it correctly I can't use varname[][] within a
struct. The size has to be known at compile time.

I have a lot of filters like that in the program I am writing but I
would like to group these [different sized] matrix's somehow and
access them like that.

Like the struct give above and;
struct filter ftab[] = {
{ 1.0, 0.0, 3, 3, { {0,0,0}, {0,1,0}, {0,0,0} } }, /* edge */
{ 1.0, 3.1, 4, 4, { {1,0,1.0}, {0,1,0,0}, {0,1,0,0} } } /* blur */
..... etc....
};

I don't know how more experienced programmers would solve this since I
am just starting with C.
And unless you have a C99 compiler, the specification must be a
compile time constant, not the name of a variable.


<<Remove the del for email>>

Thankx.
 
R

rickd5326

dang, sorry I snipped the records definition away.

int records;

the scores array i was too concerned about, but the program was
crashing when i did this:

fname[0][0] = 'c';

please replace the printf-s with that above line.

and what's this?! i can't pass by reference the 2D array?! :(
 
O

Old Wolf

dang, sorry I snipped the records definition away.

You snipped all the message context too. In this group
it's polite to include relevant quotes from previous
messages in the thread, so that people can read your
message without having to refer back.
and what's this?! i can't pass by reference the 2D array?! :(

You can, but that isn't what your program did. Your
program passed a pointer-to-a-pointer by value.
There is no 2D array in your program. Pointers are not
arrays.
Please read chapter 6 'Pointers and Arrays' of the FAQ.
This section also explains how to do what your program
is trying to do.

[Note. Read the text version of the FAQ that is posted
periodically in this newsgroup, if possible. The HTML
version of the FAQ contains some errors in chapter 6].
 
D

Dick D

You snipped all the message context too. In this group
it's polite to include relevant quotes from previous
messages in the thread, so that people can read your
message without having to refer back.


Oops, sorry. the code SHOULD have looked like the code below.

Then while running it abended at the assignment of 'c', but i'll read the
chapter you suggested. thanks.

-----

void initialize(char **fname,char **lname,float **scores,char *lgrade,int
*records)
{
int i,j;

printf("How many records do you have?");
scanf("%d",records);
printf("\n");

fname = (char **) calloc(*records,sizeof(char *));
for(i = 0; i < *records; i++)
fname = (char *) calloc(50,sizeof(char));

fname[0][0]='c';

}

int main()
{
char **lname;
char **fname;
float **scores;
char *lgrade;
int *recors;

initialize(fname,lname,scores,lgrade,&records);
return 0;
}
 
D

Dick D

int main()
{
char **lname;
char **fname;
float **scores;
char *lgrade;
int *recors;

initialize(fname,lname,scores,lgrade,&records);
return 0;
}

and this:
----
http://www.eskimo.com/~scs/C-faq/q6.14.html

Question 6.14

How can I set an array's size at run time?
How can I avoid fixed-sized arrays?

The equivalence between arrays and pointers (see question 6.3) allows a
pointer to malloc'ed memory to simulate an array quite effectively. After
executing

#include <stdlib.h>
int *dynarray = (int *)malloc(10 * sizeof(int));

(and if the call to malloc succeeds), you can reference dynarray (for
i from 0 to 9) just as if dynarray were a conventional, statically-
allocated array (int a[10]). See also question 6.16.
 
D

Dave Thompson

You can't use ftmat[][]. If you want the compiler to perform the
initialization, then you must specify the size of each dimension
except the first. If you wish, you can let the compiler determine the
size of the first dimension by counting the number of initialization
groups exist for the array.

Euh, so If I understand it correctly I can't use varname[][] within a
struct. The size has to be known at compile time.
Right. In a struct you can't even use varname[][N] where N is a
suitable (integer constant) value -- you must specify both bounds, or
more generally all -- except in C99 as the last element where it
becomes a Flexible Array Member. The feature of automatically
determining the first/outer bound (only) from the initializer applies
only to 'standalone' variables.
I have a lot of filters like that in the program I am writing but I
would like to group these [different sized] matrix's somehow and
access them like that.

Like the struct give above and;
struct filter ftab[] = {
{ 1.0, 0.0, 3, 3, { {0,0,0}, {0,1,0}, {0,0,0} } }, /* edge */
{ 1.0, 3.1, 4, 4, { {1,0,1.0}, {0,1,0,0}, {0,1,0,0} } } /* blur */
..... etc....
};
If the sizes vary only within some modest range, like 2 to 5 or even 1
to 10, and you don't have a huge number of them, you could simply
declare and hence allocate the maximum you ever need, but initialize
and then use only the needed parts of each one. This wastes some
space, but if amounts to only say 1-millionth of a cent of RAM, fine.

If they vary a lot, like up to many thousands or millions, you
probably want to use (in the struct) pointers to separately, and
perhaps dynamically, allocated spaces of the appropriate differing
sizes. If you want to vary both bounds, or more generally more than
one, you need to use intermediate pointers -- which in C can be
accessed in code as if they were multi-dim arrays, i.e. foo[x][y] etc.
-- BUT THEY AREN'T, see section 6 of the FAQ, and don't believe any
one who tells you that e.g. int [] [] and int * * are the same.

Well, or do the subscript (stride) calculation yourself and actually
code a 1-D array.

Also remember in these cases that assigning the struct copies only the
pointer, not its target(s). If you actually want a "deep" copy (that
is a new, safe, separate copy of the data) you must code that
explicitly. Similarly if you write out to a file and read back in, or
<offtopic=nonstandard> send to another process/program </>, you need
to explicitly handle the target(s) rather than the pointer(s).

- David.Thompson1 at worldnet.att.net
 

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

Similar Threads

Question about declaration of two dimension array 1
typedef, 2D arrays, design: question 11
creating a 2D array 2
2d array of hashes 3
alocation of 2D array 0
Creating 2D Array 1
Creating a 2D Array 0
2d array 0

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top