c query
This is what exactly I want to do:
I have one character pointer:say *c="{1,2,3,4}" The pointer contains only this kind of stuff: i.e. "{1,2}" or "{a,b,c}" . In general char pointer is a set containing either numbers or characters I want to assign it to an array, say a[], so that i can later access it like array a[]={1,2,3,4}, just like we access the arrays. if i do char a[]=c, it won't work as i cannot convert char pointer to char. Is there any way to do it? Hope to get a reply from you soon. Thank you. |
I'm not certain how you are setting up your pointer. But an array without the square brackets is a pointer to the first element.
Code:
char a[] = {1,2,3,4}; |
watch the braces. its not
*c={1,2,3,4}, but its *c="{1,2,3,4}" so, c[2] wont work as an array. |
In which case use the code as follows:
Code:
char a[] = "{1,2,3,4}"; |
Or of course:
Code:
char *c = "{1,2,3,4}"; |
done up wid the query
Hey thank you very much graemef). Everything solved.
Actually i am designing a compiler supporting set operations using Lex and Yacc. Here, the user will give input in the form of set like: {1,2,3,4}. So i need to convert it to an array as I am using Standard Template Library(STL), which contains set functions like intersection, union, set difference and set symmetric difference. And in these functions, the arguments are in the form of an array. For that purpose, i need to pass the set as an array. And therefore, i need to convert the input, which is in the form of braces(set) to array form. This is what i did. Quote:
Regards, Ankit:Pengy: |
You need to consider ill-formed input such as:
1,2,3,4} {12,3,4} {1,2,3,4 {1,1,1,1} {1,2,3,4,5,6,7,8,9,0,1} Most of this you may want to do in a separate validation function, the duplicate case you can easily do by changing the way you hold the set. First set each element to zero, then if it exists set the value to one, which would be a[c[j]] = 1;. You display routine would then be a case of seeing if it is one or not. If space is an issue you can then change this to a bit pattern. |
Quote:
Quote:
I hope i understood yr reply in the right manner. Hope to get a reply from you soon. Thanks. Regards, Ankit |
That's fine, it sounds as if you have properly thought this through. I just felt that I should mention this ;)
|
Thanks mate...for such a wonderful help !!! take care !!!
Regards, Ankit. |
All times are GMT -5. The time now is 07:42 AM. |