How do I create dynamically arrays using malloc???
In C++ it's :
int count;
count=40;
char *array;
array=new array[count];
but how it is in C????
Thank you in advance!
How do I create dynamically arrays using malloc???
In C++ it's :
int count;
count=40;
char *array;
array=new array[count];
but how it is in C????
Thank you in advance!
Ilia Yordanov,
http://www.cpp-home.com ; C++ Resources
For a 2d array:
The format is off because I cut and pasted part of it and than tried to type other parts.Code:#include<stdio.h> #include<stdlib.h> int main() { char **iptr = (char **) malloc (sizeof(char*) * 3); for(int i = 0;i < 3; i++) iptr[i] = (char *) malloc (sizeof(char) * 10); strcpy(iptr[0], "Chicken"); strcpy(iptr[1], "Cow"); printf("%s %s", iptr[0],iptr[1]); for(i=0;i<3;i++) free(iptr[i]); return 0; }
For a normal array
The array is the same asCode:char *array = (char *) malloc (sizeof(char) * 20);
char array[20];
Last edited by Troll_King; 10-15-2001 at 01:16 PM.
Thank you a lot Troll_King!!! It worked!!!!!!
Ilia Yordanov,
http://www.cpp-home.com ; C++ Resources
Yeah the 2d array would be the same as:
char iptr[3][10];
It's all about rows and columns. You could do this:
char iptr[rows][columns];
char **iptr = (char **) malloc (sizeof(char*) * ROWS);
for(int i = 0;i < ROWS; i++)
iptr[i] = (char *) malloc (sizeof(char) * COLUMNS);
Ofcourse you would need to give the sizes of the rows and columns. This is the format however.