Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Assigning values to a dynamically allocated array

Reply
Thread Tools

Assigning values to a dynamically allocated array

 
 
Ray D.
Guest
Posts: n/a
 
      10-02-2007
I want to set matrix A with the values below, but it produced a syntax
error when I try to compile the commented code. I was told to use a
for loop to do this (as shown below), but that really isn't possible
considering I need these specific values. Is there a way to do this
in one statement instead of multiple statements of A[i][j] = ...,
where I manually specify the position? Why can you initialize like
this with a static array, but can't use the same syntax with a dynamic
array after the memory is allocated?

#define MATSIZE 8
....
int **A;
....
A = malloc(MATSIZE*sizeof(int *));
for (i = 0; i < MATSIZE; i++)
A[i] = malloc(MATSIZE*sizeof(int));

for ( i=0; i < MATSIZE; i++ )
{
for ( j=0; j < MATSIZE; j++ )
A[i][j] = ...;
}

/* A = {{0, 1, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 7, 0, 0},
{6, 0, 8, 0, 0, 0, 2, 0},
{0, 0, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 5},
{0, 2, 0, 0, 0, 0, 1, 0}}; */

 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      10-03-2007
On Tue, 02 Oct 2007 18:53:00 -0000, "Ray D."
<(E-Mail Removed)> wrote in comp.lang.c:

> I want to set matrix A with the values below, but it produced a syntax
> error when I try to compile the commented code. I was told to use a
> for loop to do this (as shown below), but that really isn't possible
> considering I need these specific values. Is there a way to do this
> in one statement instead of multiple statements of A[i][j] = ...,
> where I manually specify the position? Why can you initialize like
> this with a static array, but can't use the same syntax with a dynamic
> array after the memory is allocated?
>
> #define MATSIZE 8
> ...
> int **A;
> ...
> A = malloc(MATSIZE*sizeof(int *));
> for (i = 0; i < MATSIZE; i++)
> A[i] = malloc(MATSIZE*sizeof(int));
>
> for ( i=0; i < MATSIZE; i++ )
> {
> for ( j=0; j < MATSIZE; j++ )
> A[i][j] = ...;
> }
>
> /* A = {{0, 1, 0, 0, 0, 3, 0, 0},
> {0, 0, 0, 4, 0, 0, 0, 0},
> {0, 0, 0, 0, 0, 0, 0, 1},
> {0, 0, 0, 1, 0, 7, 0, 0},
> {6, 0, 8, 0, 0, 0, 2, 0},
> {0, 0, 0, 0, 0, 3, 0, 0},
> {0, 0, 0, 0, 0, 0, 0, 5},
> {0, 2, 0, 0, 0, 0, 1, 0}}; */


Arrays are modifiable lvalues in C. You can't assign to them. You
can only assign to the individual members, one-by-one. Perhaps you
could do that in a loop.

I would suggest that you read Section 6, "Arrays and Pointers", in the
FAQ for this group, link in my signature. Then I'd suggest you look
over the rest of the FAQ.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      10-03-2007
Jack Klein <(E-Mail Removed)> wrote:
> Arrays are modifiable lvalues in C. ...


ITYM they're *not* modifable lvalues in C. [cf 6.3.2.1p1]

--
Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is it safe to point to an element in a dynamically allocated array? songfire@gmail.com C++ 2 06-14-2005 07:51 PM
Problems initializing a dynamically allocated 2D array masood.iqbal@lycos.com C Programming 4 02-07-2005 06:55 AM
Problems initializing a dynamically allocated 2D array masood.iqbal@lycos.com C++ 6 02-07-2005 06:55 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Dynamically allocated array question Jeff Williams C++ 6 08-19-2003 01:13 PM



Advertisments