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));
    }
}   

Your types are wrong. Your compiler should have warned you about that. If it didn't, increase your warnings level - that will save you a lot of pain in the future.

arrayptr and rowptr should have types char* and char** respectively. Since you made arrayptr and int-pointer, your second for loop will iterate int-by-int, not char-by-char. So it's skipping over characters because ints are bigger than chars.

use

printf("size of char %d size of int %d",sizeof(char),sizeof(int));

bot pointer is always 4 byte no ?

maybe you can try to cast to char ?

by the way where is problem?

strcpy(*rowptr,"d");
    strcpy(*(rowptr+1),"c++");
    printf("ROWPTR+1[%s]",*(rowptr + 1));
    strcpy(*(rowptr+2),"java");
    printf("ROWPTR+2[%s]",*(rowptr + 2));

they print correct, no ?

please see howto allocate memmory for pointer to pointer Click Here took code from there

float **float_values;
//allocate memory for rows
float_values = (float**)malloc(4 *sizeof(float*));
//for each row allocate memory for columns
for(int i=0; i<4; i++)
{
   *(float_values+i) = (float*)malloc(3 *sizeof(float));
}

You have allocated the array of poitners rowptr, but not the elements of each row. As a result, your strcpy() functions will be corrupting memory. You also need to do this:

 for (row = 0; row < nrows; row++)
 {
       rowptr[row] = (char*)calloc(ncols, sizeof(char));
 }

As mentioned, you also need to change the type of rowptr to a char** instead of an int**, and change the allocateion to rowptr = (char**)malloc(nrows*sizeof(char*)); or rowptr = (char**)calloc(nrows, sizeof(char*));

You have allocated the array of poitners rowptr, but not the elements of each row.

Not true. He allocated enough memory for all the rows on line 14. He's then making each pointer in rowptr point to one row inside arrayptr on line 17. So that part's fine.

Thanks everyone! Especially sepp2k! You were right and i was getting warnings, but i didn't understand what the issue was. I now have everything working as expected and order has been restored in the universe!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.