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}}; */ 2 4049
On Tue, 02 Oct 2007 18:53:00 -0000, "Ray D."
<ra************ @gmail.comwrote 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.l earn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jack Klein <jackkl...@spam cop.netwrote:
Arrays are modifiable lvalues in C. ...
ITYM they're *not* modifable lvalues in C. [cf 6.3.2.1p1]
--
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ben |
last post by:
Hi all,
This may sound easy but I'm having trouble assigning values to array
element. My problem is as follows:
m =
for o in m:
# Here first o is 'Peter'.. I want to do something like this:
Peter = 10
|
by: songfire |
last post by:
Hi everybody!
Just wondering if it is possible to point to variables in the heap.
For example, is this okay?
int * ptr_to_DAIA; // pointer to dynamically allocated integer array
ptr_to_DAIA = new int ;
for(i=0; i<SIZE; i++) ptr_to_DAIA=i;
// now say I want a pointer that points to the element that contains
value TARGET
|
by: Eric Bantock |
last post by:
Very basic question I'm afraid. Once an array has been declared, is there
a less tedious way of assigning values to its members than the following:
myarray=8;
myarray=3;
myarray=4;
myarray=0;
myarray=0;
myarray=1;
myarray=8;
|
by: yogi |
last post by:
Hi guys,
I'm trying to write a program that will read in a series of files and
create a 3D array from the files read in for converting 2D images to 3D
objects. The values read in will be considered as pixel colours and the
size that 1 pixel occupies will depend on an input from the user when
running the program. How can I dynamically change the data type of my 3D
array at runtime. eg. 3D array was defined as type unsigned char in the
code...
|
by: smnoff |
last post by:
I have searched the internet for malloc and dynamic malloc; however, I still
don't know or readily see what is general way to allocate memory to char *
variable that I want to assign the substring that I found inside of a
string.
Any ideas?
| |
by: bwaichu |
last post by:
Is my understanding of the allocation of these correct?
I used fixed sized allocations for the example below, so I realize
there is some optimization that can be done to those.
I would like to use these in a linked list for something else I am
working on, but I want to make sure my understanding of the concept is
correct.
For example, from the code below string would equal 'h' after
|
by: wyrmmage |
last post by:
hello :) I need to make a dynamically allocated array of pointers, using a .hpp and .cpp file; how do I accomplish this? I think I know how to make an array of pointers, and I know how to make a dynamically allocated array, but I am lost as to how to put the two together...
Any help would be appreciated :)
-wyrmmage
|
by: sandy |
last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory',
within my class Directory (Can you already smell disaster?)
Each Directory can have subdirectories so I thought to put these in an
array. The application compiles but aborts without giving me any useful
information.
What I suspect is happening is infinite recursion. Each Directory
object creates an array of Subdirectories each of which has an array of...
|
by: Serpent |
last post by:
The C-FAQ describes some techniques here: http://c-faq.com/aryptr/dynmuldimary.html
I was using something slightly different from the C-FAQ and I was
wondering if it was legal.
Say I want a two-dimensional array, like this:
int x;
but I want it dynamically-allocated, and I want expressions that refer
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |