This does not make sense:
Code:
h[i]=malloc(sizeof(p)*sizeof(char));
You either want to allocate space for a copy of the token, or you want h[i] to point to the first character of the token. For the former, you would do something like this:
Code:
p = strtok(line, "\t");
while (i < count && p != NULL) {
h[i] = malloc(strlen(p) + 1);
strcpy(h[i], p);
y[i] = malloc(strlen(p) + 1);
strcpy(y[i], p);
i++;
p = strtok(NULL, "\t");
}
For the latter, you would do something like this:
Code:
p = strtok(line, "\t");
while (i < count && p != NULL) {
h[i] = p;
y[i] = p;
i++;
p = strtok(NULL, "\t");
}
Notice that the actions taken for both h[i] and y[i] are respectively the same in each case.
Originally Posted by
deathmetal
When I use ** pointer for character I've to allocate memory 2 times:
- the number of strings I plan to hold
- the length of each string
- Free my pointer
As I have shown, whether you dynamically allocate just once or whether you also dynamically allocate within the loop depends on which approach you decide to take. If you dynamically allocate just once, it means you have a dynamic array of pointers, each of which points to the first character of a string within the original array. If you dynamically allocate within the loop, you can then store copies of these tokens.
Originally Posted by
deathmetal
However, if I use * pointer, I do not have to allocate any such memories but one the number of string I intend to hold.
But not the length of string.
As I have shown, you can dynamically allocate memory within the loop even when you have a variable length array of pointers, i.e., if you want each pointer to point to the first element of a copy of the token, rather than to characters within the original array.
Originally Posted by
deathmetal
- Why single pointer doesn't need length of string?
It is not a "single pointer": y is a variable length array of pointers. When you cause each pointer to point to the first character of a token within the original array, you do not need to allocate space for each token since the original array provides the storage for the tokens.
Originally Posted by
deathmetal
- Do I need to free * pointer at some point alike ** pointer?
No, variable length arrays will be automatically destroyed when they go out of scope. However, if you dynamically allocate space for each token, then you need to free the space for each token before the variable length array goes out of scope (unless you also save the pointers elsewhere).
Originally Posted by
deathmetal
- When do I use which if any general rule of thumb is there?
If you only need the array to exist within the scope of the function then a variable length array is appropriate. However, because they are less well supported, you might choose to create a dynamic array with malloc instead anyway.
EDIT:
This is wrong:
Code:
char *p = malloc(10*sizeof(char*));
You probably wanted to write:
Code:
char *p = malloc(10 * sizeof(char));
You should use this pattern instead, except when you have a pointer to void:
Code:
char *p = malloc(10 * sizeof(*p));
Though since sizeof(char) == 1, you could also write:
Code:
char *p = malloc(10);
Originally Posted by
deathmetal
If I'm not wrong both would work as intended, as long as malloc is able to allocate memory.
Is there any preference of one over other?
They are entirely different: the former is an array of 10 pointers to char; the latter is a pointer to char that points to the first element of an array of 10 char. Perhaps you had in mind:
Code:
char **p = malloc(10 * sizeof(*p));
In which case they can be used similiarly, but of course for the latter 10 can be replaced by a variable, whereas for the former it must be a constant unless you want a variable length array.