473,788 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to declare a dynamic array

How can I declare a two dimention array of character with dynamical
first dimention and static second dimention using mix of pointer* and
[]. I think "char *A[4]" means first dimention is static and second
dimension is dynamical. Thank you.
Nov 14 '05 #1
4 2919
In 'comp.lang.c', ln******@yahoo. com (Linda) wrote:
How can I declare a two dimention array of character with dynamical
first dimention and static second dimention using mix of pointer* and
[]. I think "char *A[4]" means first dimention is static and second
dimension is dynamical. Thank you.


char (*A)[4] = malloc (sizeof *A * nb_of_elements) ;

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #2

"Linda" <ln******@yahoo .com> wrote

How can I declare a two dimention array of character with dynamical
first dimention and static second dimention using mix of pointer* and
[]. I think "char *A[4]" means first dimention is static and second
dimension is dynamical. Thank you.

char *A[4];

is the same as

char* A[4];

which you will see is an array of 4 character pointers.

char (*A)[4] is what you want, but this syntax is horrible. 2d arrays in C,
beyond the very simplest declaration, are horrible.
What most programmers would do is declare

char *A;

and just manage the second dimension by hand. You see this quite commonly in
24 bit rgb images, for example.
Nov 14 '05 #3
Malcolm <ma*****@55bank .freeserve.co.u k> spoke thus:
char (*A)[4] is what you want, but this syntax is horrible. 2d arrays in C,
beyond the very simplest declaration, are horrible.
What most programmers would do is declare char *A;


Is the syntax really that bad? I would think your original
declaration would do a better job of conveying to an informed reader
that you have a dynamic array of 4 byte arrays...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4

"Christophe r Benson-Manica" <at***@nospam.c yberspace.org> wrote
Malcolm <ma*****@55bank .freeserve.co.u k> spoke thus:
char (*A)[4] is what you want, but this syntax is horrible. 2d arrays in
C, beyond the very simplest declaration, are horrible.
What most programmers would do is declare

char *A;


Is the syntax really that bad? I would think your original
declaration would do a better job of conveying to an informed reader
that you have a dynamic array of 4 byte arrays...

Yes it is. If you follow the ng you will see that there is a steady trickle
of newbies hopelessly confused by C's multi-dimensional arrays, largely
because most introductory textbooks introduce the 2d array at the same time
as the 1d array.
char (*A)[4] is gibberish. No-one should be expected to follow such a
construct, or use it in real code. Even the informed reader, who can work
out what it means, will find it hard to read and make mistakes.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6409
by: Secondpost | last post by:
I need to use a calender object with a dynamic form i.e. when a new row of fields is created using Javascript a calender button appears aswell. The problem is if I need to declare the following code for the calendar script to work: var cal6 = new calendar1(document.forms.elements); cal6.year_scroll = true; cal6.time_comp = false; where the # 6 will change as the user adds a new field (by clicking a button).
6
2834
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void generateVals() { ..... //generate some values
5
3410
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to be done using dynamic arrays. I don't know the size of the text file ahead of time, though, so I created a class that includes a method to resize the array. Here is that class: class Data { public: Data(int initialsize);
19
3086
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at runtime using malloc.In this last member of the structer "int last" is not
6
2983
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of strings'. I already know that no individual string will be longer than 50 characters. I just don't know before run time how many elements of the array will be needed. I have heard it is possible to dynamically allocate memory for a 2
3
9688
by: Himmat Solanki via .NET 247 | last post by:
How can we declare an array that have not afix length? and can it possible to inserts new itmes to it? as much as we want to enter. -------------------------------- From: Himmat Solanki ----------------------- Posted by a user from .NET 247 (http://www.dotnet247.com/) <Id>p3+b0EIBy0WXeQlqHuKhDA==</Id>
5
15801
by: Jiggaz | last post by:
Hi, Look my stored procedure : __________________ ALTER PROCEDURE dbo.CreateAccount @Nickname varchar(30), @Password varchar(15), @Email varchar(50), @Date datetime,
6
7774
by: John Bailo | last post by:
I created a sproc with 3 input parameters and one output parameter. I want to test it in the "Run SQL Scripts" app of Navigator. I wrote this code: DECLARE RTNDATE CHAR(10) DEFAULT ''; CALL MANLIB.MANDATCL('ADD','20060101','1',RTNDATE) SELECT MYVAR;
4
2617
by: ottawajn | last post by:
Hi, There, I want to declare a two-dimension array by "float coeftemp1 ;" in Dev c++. It doesn't work. But, if I change it to "float coeftemp1 ;". It works. Could any one know the reason? Thank you,
0
9656
marktang
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...
0
9498
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,...
0
10370
Oralloy
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...
1
10113
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,...
0
9969
tracyyun
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...
1
7519
isladogs
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...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.