473,382 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 2897
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.uk> 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)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote
Malcolm <ma*****@55bank.freeserve.co.uk> 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
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...
6
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...
5
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...
19
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...
6
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...
3
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 ...
5
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
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...
4
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? ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.