First, i'm new to C and trying to teach it to myself so be gentle. All i'm trying to do is malloc a block of memory so that i can store a few strings. These strings are to be pointed at by an array of pointers so that as i increment the pointer, i point to the next word. My question is how come the block of malloc'd memory appears to contain only the 1st char of each string i'm attempting to strcpy?
#include<stdio.h>
#include<string.h>
int main()
{
int nrows = 4;
int ncols = 5;
int *arrayptr;
int **rowptr;
int k, row, col;
rowptr = malloc(nrows*sizeof(int *));
arrayptr = malloc(nrows*ncols*sizeof(int));
for (k = 0; k < nrows; k++)
{
rowptr[k] = arrayptr + (k * ncols);
}
strcpy(*rowptr,"d");
strcpy(*(rowptr+1),"c++");
strcpy(*(rowptr+2),"java");
for (row = 0; row < nrows; row++)
{
printf("\n%d %p %c", row, rowptr[row],*rowptr[row]);
if (row > 0)
printf(" %d",(rowptr[row] - rowptr[row-1]));
}
for (col = 0; col < ncols*nrows; col++)
{
printf("\n %x %c",arrayptr+col,*(arrayptr+col));
}
}