LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c query (https://www.linuxquestions.org/questions/programming-9/c-query-448144/)

ankit4u1 05-24-2006 12:35 PM

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.

graemef 05-24-2006 01:26 PM

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};
        char * c = a;
        printf("%i\n\n",c[2]);

Hence the following will display the 3rd element.

ankit4u1 05-24-2006 02:06 PM

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.

graemef 05-24-2006 02:29 PM

In which case use the code as follows:
Code:

        char a[] = "{1,2,3,4}";
        char * c = a;
        printf("%c\n\n",c[2]);

This will return the 3rd element, namely the comma.

graemef 05-24-2006 02:31 PM

Or of course:
Code:

        char *c = "{1,2,3,4}";
        printf("%c\n\n",c[2]);


ankit4u1 05-25-2006 03:33 AM

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:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *c="{1,2,3,4}",a[10];
int i,j;

for(i=1,j=0;i<strlen(c);i=i+2,j++)
a[j]=c[i];

for(i=0;i<j;i++)
printf("a[%d]= %c\n",i,a[i]);

getch();
}
Thanks for the help.

Regards,
Ankit:Pengy:

graemef 05-25-2006 07:37 AM

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.

ankit4u1 05-26-2006 03:31 AM

Quote:

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}
Yes, you are right. But Flex has a very good option regarding ill-formed input. I just need to match the pattern like { //input } , i.e. starting parenthesis , elements separated by comma, closing parenthesis. so any other input is considered as ill-formed input and it wont' be converted to token. So all the inputs(sets) will be scanned but not passed to the parser.

Quote:

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.
Thanks for the help,but I have used STL functions, which automatically detect this duplication. So i need not device another logic for that purpose.

I hope i understood yr reply in the right manner.

Hope to get a reply from you soon. Thanks.

Regards,
Ankit

graemef 05-26-2006 09:28 AM

That's fine, it sounds as if you have properly thought this through. I just felt that I should mention this ;)

ankit4u1 05-27-2006 11:43 AM

Thanks mate...for such a wonderful help !!! take care !!!

Regards,
Ankit.


All times are GMT -5. The time now is 07:42 AM.