S
Simon Schaap
Hello,
I have encountered a strange problem and I hope you can help me to
understand it. What I want to do is to pass an array of chars to a
function that will split it up (on every location where a * occurs in
the string). This split function should allocate a 2D array of chars
and put the split results in different rows. The listing below shows
how I started to work on this. To keep the program simple and help
focus the program the string is not actually split. The split function
in this case just allocates a 2D array of size 1 by the length of the
passed string and copies the entire input string into this newly
allocated array. A pointer to this array is then passed to the caller
function. By the way, allocating of these so called 2D arrays is done
by a funtion that I adapted from the book "C unleashed", R Heath, L
Kirby et al.
Unfortunately, even this simple program escapes my comprehension. When
the caller function prints the chars in the 2D array it just received
from the split function, the first char turns out to be the '\0'
character! I am completely at loss here, where does this '\0'
character come from? I hope someone will find the time to enlight me.
Sincerely,
Simon
BEGIN OF LISTING:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** allocate_2d_array_of_chars(size_t m, size_t n);
char** split_string(char *instring);
int main(void)
{
char **a=NULL;
char b[14]="*(10)*5*(1)*1";
int i;
a=split_string(b);
if (a!=NULL) {
for (i=0;i<14;i++) {
printf("%d %c\n", i, a[0]);
}
/* to show the strange behavior : */
if (a[0][0]=='\0') {
printf("a[0][0] equals '\0' \n Strange..., not?\n");
}
free(a);
}
return 0;
}
char **split_string(char *instr)
{
int instrlen;
int i;
char **retarr;
instrlen = strlen(instr);
retarr = allocate_2d_array_of_chars(1,instrlen);
if (retarr==NULL) {
printf("could not allocate retarr\n");
return NULL;
}
/* copy the string */
for (i=0;i<instrlen;i++) {
retarr[0]=instr;
}
return retarr;
}
char** allocate_2d_array_of_chars(size_t m, size_t n)
{
/* adapted from "C unleashed", R Heath, L Kirby et al.*/
/* allocates a 2D array of one contiguous chunk of memory */
typedef char T;
T **a;
T *p;
size_t Row;
a=malloc(m * n * sizeof **a + m * sizeof *a);
if (a != NULL) {
for (Row = 0, p = (T *)a + m; Row < m; Row++, p+=n) {
a[Row] = p;
}
}
return a;
}
END OF LISTING
I have encountered a strange problem and I hope you can help me to
understand it. What I want to do is to pass an array of chars to a
function that will split it up (on every location where a * occurs in
the string). This split function should allocate a 2D array of chars
and put the split results in different rows. The listing below shows
how I started to work on this. To keep the program simple and help
focus the program the string is not actually split. The split function
in this case just allocates a 2D array of size 1 by the length of the
passed string and copies the entire input string into this newly
allocated array. A pointer to this array is then passed to the caller
function. By the way, allocating of these so called 2D arrays is done
by a funtion that I adapted from the book "C unleashed", R Heath, L
Kirby et al.
Unfortunately, even this simple program escapes my comprehension. When
the caller function prints the chars in the 2D array it just received
from the split function, the first char turns out to be the '\0'
character! I am completely at loss here, where does this '\0'
character come from? I hope someone will find the time to enlight me.
Sincerely,
Simon
BEGIN OF LISTING:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** allocate_2d_array_of_chars(size_t m, size_t n);
char** split_string(char *instring);
int main(void)
{
char **a=NULL;
char b[14]="*(10)*5*(1)*1";
int i;
a=split_string(b);
if (a!=NULL) {
for (i=0;i<14;i++) {
printf("%d %c\n", i, a[0]);
}
/* to show the strange behavior : */
if (a[0][0]=='\0') {
printf("a[0][0] equals '\0' \n Strange..., not?\n");
}
free(a);
}
return 0;
}
char **split_string(char *instr)
{
int instrlen;
int i;
char **retarr;
instrlen = strlen(instr);
retarr = allocate_2d_array_of_chars(1,instrlen);
if (retarr==NULL) {
printf("could not allocate retarr\n");
return NULL;
}
/* copy the string */
for (i=0;i<instrlen;i++) {
retarr[0]=instr;
}
return retarr;
}
char** allocate_2d_array_of_chars(size_t m, size_t n)
{
/* adapted from "C unleashed", R Heath, L Kirby et al.*/
/* allocates a 2D array of one contiguous chunk of memory */
typedef char T;
T **a;
T *p;
size_t Row;
a=malloc(m * n * sizeof **a + m * sizeof *a);
if (a != NULL) {
for (Row = 0, p = (T *)a + m; Row < m; Row++, p+=n) {
a[Row] = p;
}
}
return a;
}
END OF LISTING