LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2006, 12:35 PM   #1
ankit4u1
Member
 
Registered: Apr 2006
Distribution: Red Hat, Fedora
Posts: 97

Rep: Reputation: 15
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.
 
Old 05-24-2006, 01:26 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 05-24-2006, 02:06 PM   #3
ankit4u1
Member
 
Registered: Apr 2006
Distribution: Red Hat, Fedora
Posts: 97

Original Poster
Rep: Reputation: 15
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.
 
Old 05-24-2006, 02:29 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 05-24-2006, 02:31 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Or of course:
Code:
	char *c = "{1,2,3,4}";
	printf("%c\n\n",c[2]);
 
Old 05-25-2006, 03:33 AM   #6
ankit4u1
Member
 
Registered: Apr 2006
Distribution: Red Hat, Fedora
Posts: 97

Original Poster
Rep: Reputation: 15
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
 
Old 05-25-2006, 07:37 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 05-26-2006, 03:31 AM   #8
ankit4u1
Member
 
Registered: Apr 2006
Distribution: Red Hat, Fedora
Posts: 97

Original Poster
Rep: Reputation: 15
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
 
Old 05-26-2006, 09:28 AM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
That's fine, it sounds as if you have properly thought this through. I just felt that I should mention this
 
Old 05-27-2006, 11:43 AM   #10
ankit4u1
Member
 
Registered: Apr 2006
Distribution: Red Hat, Fedora
Posts: 97

Original Poster
Rep: Reputation: 15
Thanks mate...for such a wonderful help !!! take care !!!

Regards,
Ankit.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
dpkg query rmram Debian 5 09-09-2005 05:59 PM
Courier query (=AA=) Linux - Software 4 07-27-2005 03:23 AM
query saurabhparihar Linux - Newbie 2 05-21-2005 02:00 PM
mySQL query help ezra143 Programming 3 04-25-2005 03:17 PM
X -query pacman23 Linux - Software 2 07-22-2004 07:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration