Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How to copy char array to char array? (http://www.go4expert.com/forums/copy-char-array-char-array-t3401/)

gohgss 15Mar2007 01:31

How to copy char array to char array?
 
int main(int arg, char *argv [])
{

char *a[256];

// how we copy the array below
a[0]=argv[0];


return 0;
}

DaWei 15Mar2007 02:51

Re: How to copy char array to char array?
 
"char *array [256] declares an array of 256 char pointers. You have made no provisions for storage of actual characters, only the pointers. It is rather futile to duplicate argv in a local copy, since it is quite manipulable as it stands, even to the point of extending the lengths of strings to which it points.

Despite all that, you might want to copy argv, or copy the items which argv references. You need to tell us which of those things you want.

If you don't understand pointers, then refer to the link in my signature. Pointers are like entries in your address book; your girlfriend, or your data, does not live in your address book. They live at the location INDICATED in your address book. That location must exist, and be populated, or your address book isn't worth chit.

gohgss 15Mar2007 05:26

Re: How to copy char array to char array?
 
Hi,

I just want to copy the partial array value of the pointer array
e.g
a start index 0-4 and argv index is from 5 to 9
a[0]=argv[5];

gohgss 15Mar2007 05:32

Re: How to copy char array to char array?
 
In the other word, just to duplicate another subset of char array pointer.

shabbir 15Mar2007 07:40

Re: How to copy char array to char array?
 
argv is not a one dimension array but its array of pointers and so you should be copying them to the other array of pointers and not into just an array. As a pseudo code
Code:

for(int i = 0; i<255;i++)
 a[i] = arg[1][i]; // Copy the content of the second array


ganesh_don 16Mar2007 14:31

Re: How to copy char array to char array?
 
Mr.shabbir i think we cant do so bcoz a[i] is an array of pointers(as declared by gohgss) and what arg[1][i] points to is a character we cant store a charater into a pointer variable.Sorry if iam wrong

DaWei 16Mar2007 17:21

Re: How to copy char array to char array?
 
You can copy argv [5]-argv [9] to a [0]-a[4], which will give you a duplicate set of pointers to the material, or you can use, say, strdup, passing argv [5]-argv [9] and store the returned pointers in a[0]-a[4], which will give you a duplicate set of strings. In the latter case, don't forget to free the contents of a[] when finished.

shabbir 16Mar2007 17:59

Re: How to copy char array to char array?
 
Quote:

Originally Posted by ganesh_don
Mr.shabbir i think we cant do so bcoz a[i] is an array of pointers(as declared by gohgss) and what arg[1][i] points to is a character we cant store a charater into a pointer variable.Sorry if iam wrong

As a pseudo code I did not aim at giving the complete code but an idea. What I meant there is loop through it to copy it.


All times are GMT +5.5. The time now is 21:33.