473,230 Members | 1,490 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,230 software developers and data experts.

help generating an array of array with malloc or calloc

Hi ! I would like to generate an array of type char[n][5];

I just dont really figure out how I could do it with malloc or calloc.. I
mean.. I know how to allocate a simple array with both of them; but when it
comes to a 2 dimension array.. I'm stuck.. I mean.. if I ask for char**
array = malloc(5*n*sizeof(char));, it'll return a void*, not a void**..
anyone have an idea ?
Nov 13 '05 #1
7 4376
Eric Boutin wrote:

Hi ! I would like to generate an array of type char[n][5];

I just dont really figure out how I could do it with malloc or calloc.. I
mean.. I know how to allocate a simple array with both of them; but when it
comes to a 2 dimension array.. I'm stuck.. I mean.. if I ask for char**
array = malloc(5*n*sizeof(char));, it'll return a void*, not a void**..
anyone have an idea ?


Output from new.c:

array[0][0] is 0
array[0][1] is 1
array[0][2] is 2
array[0][3] is 3
array[0][4] is 4
array[1][0] is 10
array[1][1] is 11
array[1][2] is 12
array[1][3] is 13
array[1][4] is 14
array[2][0] is 20
array[2][1] is 21
array[2][2] is 22
array[2][3] is 23
array[2][4] is 24

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

#define N 3

int main(void)
{
size_t n, a, b;
char (*array)[5];

n = N;
array = malloc(n * sizeof *array);
if (!array) {
fputs("I'm tired\n", stderr);
exit(EXIT_FAILURE);
}
puts("Output from new.c:\n");
for (a = 0; a != n; ++a) {
for (b = 0; b != 5; ++b)
array[a][b] = (char)(10 * a + b);
}
for (a = 0; a != n; ++a) {
for (b = 0; b != 5; ++b) {
printf("array[%u][%u] is %u\n",
(unsigned)a,
(unsigned)b,
(unsigned)array[a][b]);
}
}
return 0;
}

/* END new.c */
Nov 13 '05 #2
Eric Boutin wrote:

Hi ! I would like to generate an array of type char[n][5];

I just dont really figure out how I could do it with malloc or calloc.. I
mean.. I know how to allocate a simple array with both of them; but when it
comes to a 2 dimension array.. I'm stuck.. I mean.. if I ask for char**
array = malloc(5*n*sizeof(char));, it'll return a void*, not a void**..
anyone have an idea ?


Read the C FAQ, Question 6.16
http://www.eskimo.com/~scs/C-faq/q6.16.html
Nov 13 '05 #3
Eric Boutin wrote:

Hi ! I would like to generate an array of type char[n][5];

I just dont really figure out how I could do it with malloc or calloc.. I
mean.. I know how to allocate a simple array with both of them; but when it
comes to a 2 dimension array.. I'm stuck.. I mean.. if I ask for char**
array = malloc(5*n*sizeof(char));, it'll return a void*, not a void**..
anyone have an idea ?


You clearly need a better C book. K&R2 comes to mind. Chapter 5
pertains. There is also Steve Summit's C FAQ. Section 6 pertains. Read
more.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #4
sorry I didn't read this point in the FAQ..

sorry

"Nejat AYDIN" <ne********@superonline.com> a écrit dans le message de
news:3F***************@superonline.com...
Eric Boutin wrote:

Hi ! I would like to generate an array of type char[n][5];

I just dont really figure out how I could do it with malloc or calloc.. I mean.. I know how to allocate a simple array with both of them; but when it comes to a 2 dimension array.. I'm stuck.. I mean.. if I ask for char** array = malloc(5*n*sizeof(char));, it'll return a void*, not a void**..
anyone have an idea ?


Read the C FAQ, Question 6.16
http://www.eskimo.com/~scs/C-faq/q6.16.html

Nov 13 '05 #5
Thanks !

"pete" <pf*****@mindspring.com> a écrit dans le message de
news:3F**********@mindspring.com...
Eric Boutin wrote:

Hi ! I would like to generate an array of type char[n][5];

I just dont really figure out how I could do it with malloc or calloc.. I mean.. I know how to allocate a simple array with both of them; but when it comes to a 2 dimension array.. I'm stuck.. I mean.. if I ask for char** array = malloc(5*n*sizeof(char));, it'll return a void*, not a void**..
anyone have an idea ?


Output from new.c:

array[0][0] is 0
array[0][1] is 1
array[0][2] is 2
array[0][3] is 3
array[0][4] is 4
array[1][0] is 10
array[1][1] is 11
array[1][2] is 12
array[1][3] is 13
array[1][4] is 14
array[2][0] is 20
array[2][1] is 21
array[2][2] is 22
array[2][3] is 23
array[2][4] is 24

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

#define N 3

int main(void)
{
size_t n, a, b;
char (*array)[5];

n = N;
array = malloc(n * sizeof *array);
if (!array) {
fputs("I'm tired\n", stderr);
exit(EXIT_FAILURE);
}
puts("Output from new.c:\n");
for (a = 0; a != n; ++a) {
for (b = 0; b != 5; ++b)
array[a][b] = (char)(10 * a + b);
}
for (a = 0; a != n; ++a) {
for (b = 0; b != 5; ++b) {
printf("array[%u][%u] is %u\n",
(unsigned)a,
(unsigned)b,
(unsigned)array[a][b]);
}
}
return 0;
}

/* END new.c */

Nov 13 '05 #6
Thanks for book suggestion

"Joe Wright" <jo********@earthlink.net> a écrit dans le message de
news:3F**********@earthlink.net...
Eric Boutin wrote:

Hi ! I would like to generate an array of type char[n][5];

I just dont really figure out how I could do it with malloc or calloc.. I mean.. I know how to allocate a simple array with both of them; but when it comes to a 2 dimension array.. I'm stuck.. I mean.. if I ask for char** array = malloc(5*n*sizeof(char));, it'll return a void*, not a void**..
anyone have an idea ?


You clearly need a better C book. K&R2 comes to mind. Chapter 5
pertains. There is also Steve Summit's C FAQ. Section 6 pertains. Read
more.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Nov 13 '05 #7
Eric Boutin wrote:

Thanks !
You're welcome.
"pete" <pf*****@mindspring.com> a écrit dans le message de
news:3F**********@mindspring.com...
Eric Boutin wrote:

Hi ! I would like to generate an array of type char[n][5];

I just dont really figure out how
I could do it with malloc or calloc..
array = malloc(n * sizeof *array);
The way that this particular program was written,
it wasn't necessary to free(array),
but I believe that it's good to be in the habit of freeing
whatever is allocated, and I forgot to do that.
}


free(array);
return 0;
}

/* END new.c */

--
pete
Nov 13 '05 #8

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

Similar topics

27
by: MK | last post by:
I am a newbie. Please help. The following warning is issued by gcc-3.2.2 compiler (pc Linux): ================================================================== read_raw_data.c:51: warning:...
37
by: Harsimran | last post by:
Can any one explain what are far pointers and what is the difference between malloc and calloc .Which is better ?
21
by: smartbeginner | last post by:
main() { int i; int *a; a=calloc(4,sizeof(*a)); /* The above code I know will not compile .But why cant I allocate space for all 4 integers i need to store */ for(i=0;i<2;i++)...
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
7
by: Yuri_Юрий | last post by:
I'm confused about the VARIABLE LENGTH ARRAYS. {scanf("%d",&n);float a;} In which compiler can I use it? I tried VC++6.0 SP6,but it's reported error:CONSTANT EXPRESSION! Another question, What...
17
by: kleary00 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. I need some help. Here is what I consider an array of 100 strings: char...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
14
by: Michel Rouzic | last post by:
Hi, I've recently met issues with my program which can only be explained by heap corruption, so I've tried debugging my program with Valgrind, and here's what I get with the following...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.