We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,285 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

probably stupid and fundamental strcpy question

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));
    }
}   
4
Contributors
5
Replies
6 Hours
Discussion Span
10 Months Ago
Last Updated
6
Views
Question
Answered
Skrell
Light Poster
34 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

sepp2k
Posting Whiz in Training
227 posts since Jul 2012
Reputation Points: 62
Solved Threads: 45
Skill Endorsements: 8

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));
}
Sokurenko
Junior Poster
111 posts since May 2012
Reputation Points: 42
Solved Threads: 13
Skill Endorsements: 0

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

rubberman
Posting Maven
2,578 posts since Mar 2010
Reputation Points: 365
Solved Threads: 307
Skill Endorsements: 52

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.

sepp2k
Posting Whiz in Training
227 posts since Jul 2012
Reputation Points: 62
Solved Threads: 45
Skill Endorsements: 8

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!

Skrell
Light Poster
34 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 10 Months Ago by sepp2k, rubberman and Sokurenko

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0720 seconds using 2.7MB