![]() |
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; } |
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. |
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]; |
Re: How to copy char array to char array?
In the other word, just to duplicate another subset of char array pointer.
|
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++) |
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
|
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.
|
Re: How to copy char array to char array?
Quote:
|
| All times are GMT +5.5. The time now is 18:46. |