I need some clarity in creating a 2d array for characters. I know this that
char *str="hellowww";//here its a constant string
str can point to "hellowwww" string, without any memory allocation. so
char *str[5]
should be able to do same for each element. So if that is the case,
1) Is char *str[5] is a 2d character array?
2) When do we need to use malloc and free in this case?
3) I know I can create a loop for malloc as we do in 2d integer array, but is there a point?
#include <stdio.h>
#include <string.h>
int main(void)
{
char *arr[6];
int i, j;
arr[0] = "hurr1";
arr[1] = "hurr2";
arr[2] = "hurr3";
arr[3] = "hurr4";
arr[4] = "hurr5";
arr[5] = "hurr6";
for(i = 0; i < 6; i++)
{
for(j = 0; j<strlen(arr[i])+1;j++)
printf("%c", arr[i][j]);
printf("\n");
}
}
Above program just works without any malloc or free, and I can access each character.
-
1). No, it's an array of pointers. 2). If you want to allocate the memory dynamically, maybe to store some user input or have the "array" contents exist beyond the scope of wherever it is allocated. 3). I'm not gonna lie, I don't know what you're asking there, can you please clarify a bit?George– George2017年04月11日 19:30:19 +00:00Commented Apr 11, 2017 at 19:30
-
Without dynamic memory allocation.Shawnic Hedgehog– Shawnic Hedgehog2017年04月11日 19:30:38 +00:00Commented Apr 11, 2017 at 19:30
-
Also note the above program will let you access (i.e. read) all the characters, but won't let you write them, because string literals are immutable.Lee Daniel Crocker– Lee Daniel Crocker2017年04月11日 19:34:52 +00:00Commented Apr 11, 2017 at 19:34
2 Answers 2
char *str;
allocates enough bytes for a pointer, which it expects at some later time will be filled in with the address of a character (probably the first of a sequence, and possibly by allocating memory dynamically).
char *str = "hello";
allocates the pointer, and also allocates a 6-byte buffer (possibly in read-only memory) for the string "hello" and its terminating 0 byte, and make the pointer point to it.
I don't know what you mean "without any memory allocation".
char *str[5];
Allocates a block of memory for five consecutive pointers, which it expects will be filled in at some later time. Is this a 2d array? No, although if you happen to assign each of the pointers the address of a sequence of characters, str[x][y] will access the yth character of the xth string, making it look like a 2d array.
Comments
char *str[5]should be able to do same for each element
That is absolutely correct: each of the five elements of str can point to a string.
- Is
char *str[5]is a 2d character array?
No. It is a one-dimension array of character pointers. You can treat it as a 2d array, but you have flexibility to not have some dimensions filled in if you wish.
- When do we need to use malloc and free in this case?
It depends on what you would like to put into elements of str[5], and whether or not you'd like to modify these things. If you put string literals in all five elements, you don't need malloc at all, but the strings you have would not allow modifications.
- I know I can create a loop for
mallocas we do in 2d integer array, but is there a point?
Again, this depends on what you are trying to achieve. You can have a loop that allocates memory dynamically, you could use string literals, use pointers to statically allocated modifiable strings, and anything else that you can do with a character pointer.
4 Comments
char x[5][10] but their dimensions are limited to identical lengths. When you use arrays of pointers, you can change the size of each element in the dimension as needed.char str[][6] = {"hurr1", "hurr2", "hurr3", "hurr4", "hurr5", "hurr6"}; initializer for a 2d array of size 6x6.