Hi,
Say I have an array of strings named S and I want to copy its values to another array of strings D.
How can I do the memory allocation?
AK
Hi,
Say I have an array of strings named S and I want to copy its values to another array of strings D.
How can I do the memory allocation?
AK
http://www.cplusplus.com/ref/cstdlib/malloc.html
the problem w/the example they have is they typecast malloc. the part of buffer = (char*) malloc (i+1); dont do that...it should be something more like buffer = malloc (i+1 * sizeof(char));
Last edited by willc0de4food; 04-27-2006 at 08:46 PM.
Registered Linux User #380033. Be counted: http://counter.li.org
Couldnt get it!
I'm dealing with array of strings!How to copy that?
lol well you'd do similar to the example, but you'd already have the string so you wouldn't have to prompt for the length. you'd simply get the length of the strings, malloc the pointer to the size of the strings, including the null at the end of each one, and then copy the information over..
Registered Linux User #380033. Be counted: http://counter.li.org
Could you show an example please?
Its complicated for a newbie like me!
Still fresh into pointer
> How can I do the memory allocation?
Start by showing how you've declared S and D
How you do this depends on what you have to start with, there are many variations on the basic theme.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Code:#include <string.h> int i; if ((D = malloc(sizeof(*S)*SIZE)) == NULL) die(); for (i = 0; i < SIZE; i++) { if ((D[i] = strdup(S[i])) == NULL) { //rollback } }