Question:
need help in C programming...!!!?
iamcodylee
2010-09-15 01:40:48 UTC
char *token = NULL;
int i=0,j;
int l,h=0;
char *shuzu[8];

char str1[]="CTU,PEK,319,CTU,PEK,321,CTU,PEK,321";
token=strtok(str1,",");

while(token!=NULL )
{
if(strlen(token)==3)
{
//printf("%s\n ",token);
shuzu[i]=token;
i++;
}
token=strtok(NULL,",");
}
for(j=0;j{
//printf("%s\n",shuzu[j]);
for (l=j;l<=i;l++)
{
if (shuzu[l]==shuzu[j])
{
shuzu[j]="none";
}
}
}

Above is my codes, but I just don't understand why the array eventually turn into NONE, I'm trying to figure out the unique values in the array. who can give me a hand?

any help would be appreciated!!!
Three answers:
Paco
2010-09-15 03:03:48 UTC
For goodness sake, and for your own sake, use meaningful variable names. It's no wonder that neither you nor anyone else can make much sense of this code.



Next, you need to understand how string comparison works in C. You cannot use == to compare strings. Since I don't see strcmp() anywhere in this code, I'm pretty sure you aren't comparing any strings. If you never compare strings, you'll never know if you've seen something before or not.



I'd also look out for the size of the shuzu array. It can only hold 8 strings. If you get 9 different things in your input, you're looking at a buffer overflow. Whee! (Go google buffer overflow some time, it's the scourge of C programming)



You need to store the strings that you find. You might want to do something like asprintf( shuzu[i], "%s", token);



Furthermore, you should only store strings that you find and have never seen before. For example, if you find CTU, you should first check your shuzu array and see if CTU is already stored there. if it is, you should not store it again, the second time you find it. Right now, you store everything that you come across.



And why check the length? Does it matter? strlen() is a dangerous programming function and must never be used in real programs in the real world, even if you use it in academic programming.



I hope that helps.
anonymous
2010-09-15 02:02:06 UTC
Part of the development cycle of a program involves debugging, i suggest you fire up your debugger and trace through the program as it's obviously not doing what you expect it to.



Debugging a program helps to develop skills which will improved you coding in the future.
Jason Anime
2010-09-15 02:45:04 UTC
i took your program and tried to compile it then copy and pasted the results/errors for you to corrrect







2 C:\Documents and Settings\Dell User\My Documents\other.c conflicting types for 'token'

7 C:\Documents and Settings\Dell User\My Documents\other.c missing terminating " character

8 C:\Documents and Settings\Dell User\My Documents\other.c [Warning] assignment makes pointer from integer without a cast

8 C:\Documents and Settings\Dell User\My Documents\other.c invalid initializer

10 C:\Documents and Settings\Dell User\My Documents\other.c syntax error before "while"

18 C:\Documents and Settings\Dell User\My Documents\other.c conflicting types for 'token'

2 C:\Documents and Settings\Dell User\My Documents\other.c previous definition of 'token' was here

18 C:\Documents and Settings\Dell User\My Documents\other.c initializer element is not constant

18 C:\Documents and Settings\Dell User\My Documents\other.c [Warning] data definition has no type or storage class

19 C:\Documents and Settings\Dell User\My Documents\other.c syntax error before '}' token


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...