arrays in c 
Author Message
 arrays in c

Hi,
I'm a complete newbie to c but well familiar with javascript/php and wonder:

Does c support arrays, two dimensional, dynamicly resizable or how do i
resize them
and preserve it's content?

I want to do:

$twoDimensionalArr=array(array()); //declare the two dimensional array. I
don't know it's dimensions yet.

while (!recordset_eof_check) {
  $oneDimensionalArray=db_get_row($dbconn); //get one record from db as one
dimensional array
  $twoDimensionalArr[]=$oneDimensionalArray; //add the array to the 2Xarr

Quote:
}

How can I do this in C?
I don't understand a blink of C with it's * & # % ^ so pleace comment any
suggestions.

Thanks,
and try the superior open source database at firebird.sourceforge.net



Sat, 10 Jan 2004 03:57:23 GMT  
 arrays in c


Quote:
> Hi,
> I'm a complete newbie to c but well familiar with javascript/php and
wonder:

> Does c support arrays, two dimensional, dynamicly resizable or how do i
> resize them
> and preserve it's content?

> I want to do:

> $twoDimensionalArr=array(array()); //declare the two dimensional array. I
> don't know it's dimensions yet.

> while (!recordset_eof_check) {
>   $oneDimensionalArray=db_get_row($dbconn); //get one record from db as
one
> dimensional array
>   $twoDimensionalArr[]=$oneDimensionalArray; //add the array to the 2Xarr
> }

> How can I do this in C?
> I don't understand a blink of C with it's * & # % ^ so pleace comment any
> suggestions.

> Thanks,
> and try the superior open source database at firebird.sourceforge.net

Seriously consider buying a good C book.  None of these "learn C in 12
minute" books but perhaps "The C Progamming Language 2nd Edition" by
Kernighan and Ritchie.  It's not too expensive and will cover all of C, not
just the parts the author learnt in their exciting new 12 day IT class...

Tom



Sat, 10 Jan 2004 04:06:34 GMT  
 arrays in c

Quote:
> Hi,
> I'm a complete newbie to c but well familiar with javascript/php and wonder:
> Does c support arrays, two dimensional, dynamicly resizable or how do i
> resize them
> and preserve it's content?

C supports one-dimensional, two-dimensional, even 47-dimensional
arrays. However, dynamically resizing them with preserving content
is a bit difficult.
In C, you cannot dynamically resize an array. It just isn't done!
You can emulate it though, by using pointers instead of arrays and
using the functions malloc(), realloc() and free().

Here's a sample C function that will allocate a two-dimensional
array dynamically.

int **array(int width, int height) {
  int **tmp;
  int i;
  if (tmp=malloc(width*sizeof(int *)))
    for (i=0; i<width; i++)
      tmp[i]=malloc(height*sizeof(int));
  return tmp;

Quote:
}

Here's a sample C function that will free the two-dimensional array
allocated by the above function.

void unarray(int **arr, int width) {
  int i;
  for (i=0; i<width; i++)
    free(tmp[i]);
  free(tmp);

Quote:
}

By using realloc() instead of malloc() in the function array() above
you should be able to resize an already allocated array with
preserving content. Further help on realloc() is available from other
comp.lang.c readers and a C book.

Quote:
> I want to do:

(off-topic code snipped)

Quote:
> How can I do this in C?

I don't know the language that was in, so I can't comment.

Quote:
> I don't understand a blink of C with it's * & # % ^ so pleace comment any
> suggestions.

Almost every language has operators made from punctuation characters.
They won't bite you.

Quote:
> Thanks,
> and try the superior open source database at firebird.sourceforge.net

Sorry, I didn't try it yet.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.*-*-*.com/ ~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow e{*filter*}dly zooming."
   - JIPsoft



Sat, 10 Jan 2004 18:05:42 GMT  
 arrays in c


...

Quote:
> C supports one-dimensional, two-dimensional, even
> 47-dimensional arrays.

If I understand 5.2.4.1 a conforming implementation
needs to allow 12 array subscriptions (or less in combination
with function or pointer declarations) in a declaration
so 47 may not always be possible.

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Sat, 10 Jan 2004 19:18:59 GMT  
 arrays in c

Quote:



> ...

> > C supports one-dimensional, two-dimensional, even
> > 47-dimensional arrays.

> If I understand 5.2.4.1 a conforming implementation
> needs to allow 12 array subscriptions (or less in combination
> with function or pointer declarations) in a declaration
> so 47 may not always be possible.

So must every conforming implementation be able to compile the following
or not? Just curious.

(please excuse line wraps)

#include <stdio.h>

int main(void)
{
  typedef int multiarray[1][1][1][1][1][1][1][1][1][1][1][1];
                         /* 12 subscripts */
  typedef multiarray multimulti[1][1][1][1][1][1][1][1][1][1][1][1];  
                         /* another 12 subscripts */
  multimulti example[1][1][1][1][1][1][1][1][1][1][1][1];
                         /* another 12 subscripts */

example[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0
][0][0][0][0][0][0][0][0][0][0][0][0]=7;
    /* 36 subscripts */

printf("value=%d\n",example[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]);
   /* 36 subscripts */
  return 0;

Quote:
}

Owen
--
"Actually Dennis Ritchie is Lionel Ritchie's half-brother. He wrote
the original versions of all Lionel's hits.  In some cases, the
titles were changed, for instance ``Hello, World'', which Lionel
shortened by dropping ``World''." -- Kaz Kylheku, comp.lang.c


Sat, 10 Jan 2004 19:42:35 GMT  
 arrays in c
<snip>

Quote:
> Seriously consider buying a good C book.  None of these "learn C in 12
> minute" books but perhaps "The C Progamming Language 2nd Edition" by
> Kernighan and Ritchie.  It's not too expensive and will cover all of C, not
> just the parts the author learnt in their exciting new 12 day IT class...

> Tom

No, but the learning curve is somewhat steep. I do not understand why
all are against these small "{*filter*}s". You should read a C book that
is easy, and wake your interest (I'm sorry to say, but K&R did not
wake mine), then as you start to program and run into real problems
you can (and will) study books as "The C Progamming Language".

Jesper



Sat, 10 Jan 2004 20:46:43 GMT  
 arrays in c

Quote:



>> ...

>> > C supports one-dimensional, two-dimensional, even
>> > 47-dimensional arrays.

>> If I understand 5.2.4.1 a conforming implementation needs to
>> allow 12 array subscriptions (or less in combination with
>> function or pointer declarations) in a declaration so 47 may
>> not always be possible.

> So must every conforming implementation be able to compile the
> following or not? Just curious.

> (please excuse line wraps)

> #include <stdio.h>

> int main(void)
> {
>   typedef int multiarray[1][1][1][1][1][1][1][1][1][1][1][1];
>                          /* 12 subscripts */
>   typedef multiarray
>   multimulti[1][1][1][1][1][1][1][1][1][1][1][1];
>                          /* another 12 subscripts */
>   multimulti example[1][1][1][1][1][1][1][1][1][1][1][1];
>                          /* another 12 subscripts */

> example[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0
> ][0][0][0][0][0][0][0][0][0][0][0][0]=7;
>     /* 36 subscripts */

> printf("value=%d\n",example[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]
> [0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]);
>    /* 36 subscripts */
>   return 0;
> }

> Owen

from IEEE 9899:1999 5.2.4.1

The implementation shall be able to translate and execute
at least one program that contains at least one instance
of every one of the following limits:

....

-- 12 pointer, array, and function declarators (in any combinations)
modifying an arithmetic, structure, union, or incomplete
type in a declaration

...

So I would say that a conforming implmentation may refuse to
compile your program.

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Sat, 10 Jan 2004 22:52:34 GMT  
 arrays in c

Quote:

> > Seriously consider buying a good C book.  None of these "learn C in 12
> > minute" books but perhaps "The C Progamming Language 2nd Edition" by
> > Kernighan and Ritchie.  It's not too expensive and will cover all of C, not
> > just the parts the author learnt in their exciting new 12 day IT class...

> No, but the learning curve is somewhat steep. I do not understand why
> all are against these small "{*filter*}s".

The simple answer? They teach bad programming habits and
misunderstandings about C. We see the effects of those all too often
here on c.l.c, and usually accompanied by a question on the lines of
"Why does a[i]=i++; not work?".

Richard



Sat, 10 Jan 2004 23:13:34 GMT  
 arrays in c

Quote:


> > So must every conforming implementation be able to compile the
> > following or not? Just curious.

> > (please excuse line wraps)

> > #include <stdio.h>

> > int main(void)
> > {
> >   typedef int multiarray[1][1][1][1][1][1][1][1][1][1][1][1];
> >                          /* 12 subscripts */
> >   typedef multiarray
> >   multimulti[1][1][1][1][1][1][1][1][1][1][1][1];
> >                          /* another 12 subscripts */
> >   multimulti example[1][1][1][1][1][1][1][1][1][1][1][1];
> >                          /* another 12 subscripts */

> > example[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0
> > ][0][0][0][0][0][0][0][0][0][0][0][0]=7;
> >     /* 36 subscripts */

> > printf("value=%d\n",example[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]
> > [0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]);
> >    /* 36 subscripts */
> >   return 0;
> > }

> > Owen

> from IEEE 9899:1999 5.2.4.1

> The implementation shall be able to translate and execute
> at least one program that contains at least one instance
> of every one of the following limits:

> ....

> -- 12 pointer, array, and function declarators (in any combinations)
> modifying an arithmetic, structure, union, or incomplete
> type in a declaration

> ...

> So I would say that a conforming implmentation may refuse to
> compile your program.

Having had a peruse of N843, I would agree. In fact, as well as 5.2.4.1,
6.7.5 #7 is of interest:

  [#7] As discussed in 5.2.4.1, an  implementation  may  limit
       the  number of pointer, array, and function declarators that
       modify an arithmetic, structure, union, or incomplete  type,
       either directly or via one or more typedefs.
                          ^^^^^^^^^^^^^^^^^^^^^^^^^

Owen

--
"Actually Dennis Ritchie is Lionel Ritchie's half-brother. He wrote
the original versions of all Lionel's hits.  In some cases, the
titles were changed, for instance ``Hello, World'', which Lionel
shortened by dropping ``World''." -- Kaz Kylheku, comp.lang.c



Sat, 10 Jan 2004 23:12:26 GMT  
 arrays in c
I'm sure that all the regulars have replied with authority to this, but
I can't resist adding my two cents worth...

Quote:

> Hi,

Hi.

Quote:
> I'm a complete newbie to c

Well, we all were, once (and some of us still are <g>)

Quote:
> but well familiar with javascript/php

That's nice. Not applicable, but nice

Quote:
> and wonder:

> Does c support arrays,

Yes

Quote:
> two dimensional,

Yes

Quote:
> dynamicly resizable

I'm not sure. Up to C89, no (but there were/are ways to simulate this),
with C99, I believe that there is _some_ provision for dynamically
resizable arrays. I'm sure that the C experts (of which I am not a
member) here can (and have) expounded on this feature (or will expound
on my mis-remembering of it).

Quote:
> or how do i resize them and preserve it's content?

Well, at worst case, you've used malloc() to allocate the space, and
realloc() to reallocate them. memcpy() will let you move them around.

Quote:
> I want to do:

> $twoDimensionalArr=array(array()); //declare the two dimensional array. I
> don't know it's dimensions yet.

> while (!recordset_eof_check) {
>   $oneDimensionalArray=db_get_row($dbconn); //get one record from db as one
> dimensional array
>   $twoDimensionalArr[]=$oneDimensionalArray; //add the array to the 2Xarr
> }

> How can I do this in C?

Do what? Oh, you mean that stuff that you expressed in some foreign
language that's not understandable here? Well, you framastan the
frippitz, and gorgle the frortz. Then you reblippiq your abnorogs and
the rest is obvious.

Quote:
> I don't understand a blink of C with it's * & # % ^ so pleace comment any suggestions.

First and only suggestion: Go learn C. If you "don't understand a blink
of C", then _any_ other suggestion that we could make would be over your
head. Once you've learned C, you will comprehend enough of the
terminology and techniques to understand any reasonable suggestion we
could make.

Quote:
> Thanks,

You're welcome

Quote:
> and try the superior open source database at firebird.sourceforge.net

What does this have to do with C?
Besides, I like Berkeley DB, MySQL, Oracle, and DB/2 UDB.

--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576



Sun, 11 Jan 2004 00:54:27 GMT  
 arrays in c
Richard Bos on Tuesday 24 July 2001 08:13:

Quote:

>> > Seriously consider buying a good C book.  None of these "learn C
>> > in 12 minute" books but perhaps "The C Progamming Language 2nd
>> > Edition" by
>> > Kernighan and Ritchie.  It's not too expensive and will cover all
>> > of C, not just the parts the author learnt in their exciting new
>> > 12 day IT class...

>> No, but the learning curve is somewhat steep. I do not understand
>> why all are against these small "{*filter*}s".

> The simple answer? They teach bad programming habits and
> misunderstandings about C. We see the effects of those all too often
> here on c.l.c, and usually accompanied by a question on the lines of
> "Why does a[i]=i++; not work?".

Why should anyone buy a "lesser" book when they can have the
authoritative guide?  Simple.  Many people learn c as a first
language, and not every beginner is ready to digest K&R as an intro
to programming.  Most beginner's aren't.  K&R acknowledges this in
the introduction:  "This book is not an introductory programming
manual".  

I would suggest that the reason that you generally get a lower class
of questions from people reading "teach yourself..." is that people
who are reading K&R as a rule are generally a more experienced and
sophisticated caliber of student.  By 'sophisticated', I mean that
most have probably already read one or more "teach yourself" books
and/or had formal training before getting a whiff K&R.

In defense of the "{*filter*} books"... K&R is rather spartan to stand
on its own as a mechanism for teaching c to novices.  My own study of
C ( such as it is ) has benefitted tremendously from having a few
easier to digest books around as a supplement to K&R.

K&R also makes some curious choices.  I have yet to determine what
particular stroke of editorial {*filter*} prompted them to sandwich a
page and a half on bitwise operators in ch 2.9 between
increment/decrement operators and assignment operators.   As a
heuristic device, that's a little like telling a piano student to
sandwich "eroica" in between learning "mary had a little lamb" and
"chopsticks".



Sun, 11 Jan 2004 14:44:08 GMT  
 arrays in c

Quote:


>> Does c support arrays,
> Yes
>> two dimensional,
> Yes
>> dynamicly resizable
> I'm not sure. Up to C89, no (but there were/are ways to simulate this),
> with C99, I believe that there is _some_ provision for dynamically
> resizable arrays.

Well, in C99 you can create an VLA whose size is only known at runtime:

void foo(int n)
{
    int a[n];
    /*...*/

Quote:
}

AFAIK there's no possibility to change a's size once it is defined.

--
Stefan Farfeleder



Sun, 11 Jan 2004 18:00:51 GMT  
 arrays in c

Quote:

> In defense of the "{*filter*} books"... K&R is rather spartan to stand
> on its own as a mechanism for teaching c to novices.  My own study of
> C ( such as it is ) has benefitted tremendously from having a few
> easier to digest books around as a supplement to K&R.

The argument is not against "{*filter*} books" so much as the fact that most
of them are badly written. A well written, accurate book would be
welcomed.

Quote:
> K&R also makes some curious choices.  I have yet to determine what
> particular stroke of editorial {*filter*} prompted them to sandwich a
> page and a half on bitwise operators in ch 2.9 between
> increment/decrement operators and assignment operators.   As a
> heuristic device, that's a little like telling a piano student to
> sandwich "eroica" in between learning "mary had a little lamb" and
> "chopsticks".

Especially since the Eroica is a symphony with no piano part!

--
    Chris F.A. Johnson                         http://www.*-*-*.com/
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



Mon, 12 Jan 2004 01:39:03 GMT  
 arrays in c

Quote:

> > dynamicly resizable

> I'm not sure. Up to C89, no (but there were/are ways to simulate this),
> with C99, I believe that there is _some_ provision for dynamically
> resizable arrays. I'm sure that the C experts (of which I am not a
> member) here can (and have) expounded on this feature (or will expound
> on my mis-remembering of it).

C99 defines dynamically sizeable arrays, but not /resizeable/.  That
is, you can define its size using a variable when the array is
created, but you cannot then change that size during the lifetime of
the array.

Quote:
> > or how do i resize them and preserve it's content?

> Well, at worst case, you've used malloc() to allocate the space, and
> realloc() to reallocate them. memcpy() will let you move them
> around.

Why would you need memcpy()?  realloc() will move the old data to the
new space before freeing the old, if need be.

Micah

--
John Golubenko: "Ok, I'm newbie, so correct me if I'm wrong."
-hs-:           "Trust me, newbie or not, you will be corrected if you
                 are wrong!"



Mon, 12 Jan 2004 02:45:19 GMT  
 arrays in c

Quote:
> Does c support arrays, two dimensional, dynamicly resizable or how do i
> resize them
> and preserve it's content?

C does support arrays, although they are one-dimensional and of fixed size.
You can however get the behaviour you want by:
- using a one dimensional array and access it as if it was 2-dimensional
array.
- making use of malloc(), realloc() for the dynamic sizing while preserving
content.

However, as you seem to be doing some db manipulations, maybe your db vendor
provides some lib to manipulate the db from within C code (and appropriate
data types).



Mon, 12 Jan 2004 02:52:34 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. char array (array of array...) question

2. MD5 byte array to hex array

3. Implementing array of array

4. Array objects vs. square bracket [] arrays

5. Managed array of managed arrays of char

6. SOAP returning array of objects containing arrays

7. Novice: Pointing to arrays in array of structures

8. array index of smallest number in array?

9. Clean way to const-cast arrays of arrays?

10. use 1d array + macro or 2d array?

11. Storing an array in a temp array

12. Multiply and Add matrices with 2d array WITHOUT ARRAY INDEXING

 

 
Powered by phpBB® Forum Software