0

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.

asked Apr 11, 2017 at 19:24
3
  • 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? Commented Apr 11, 2017 at 19:30
  • Without dynamic memory allocation. Commented 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. Commented Apr 11, 2017 at 19:34

2 Answers 2

1
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.

answered Apr 11, 2017 at 19:31
Sign up to request clarification or add additional context in comments.

Comments

1
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.

  1. 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.

  1. 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.

  1. I know I can create a loop for malloc as 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.

answered Apr 11, 2017 at 19:32

4 Comments

Then I don't think there is ever a 2d array. Surely my program is very limited to be of any use, but by definition of 2d array in C its a 2d array, but actully a 1d array only.
@acidlategamer C does have multidimensional arrays, e.g. 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.
What I have written in the program is an array of pointer. I can assign any length string to each one. So does it becomes a 2d array? In dynamic case when I do malloc for each row, I have to have a 'column' variable length, so how do element size can change in arr[x][y] when I have to use malloc(sizeof(char)*y), as 'y' is the max number of characters n a row.
@acidlategamer Since all lines that you put in the array are of the same size (6 characters) you can use char str[][6] = {"hurr1", "hurr2", "hurr3", "hurr4", "hurr5", "hurr6"}; initializer for a 2d array of size 6x6.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.