Hi,
When i run the below code in AIX it runs and solaris not ... why ???
#include <stdio.h>
#include <string.h>
#define MAX 1
int main ()
{
char str[] ="1,2,3,4,5";
char * pch,b[MAX];
int a;
printf ("Enter the int to be searched [%s]",str);
scanf("%d",&a);
sprintf(b,"%d",a);
pch = strtok (str,",");
while (pch != NULL)
{
if(!strcmp(pch,b))
/printf ("%s\n",pch);/
printf("found");
pch = strtok (NULL, ",");
}
}
Output :
Solaris : NIL
AIX: found
right now i dont have access to an IBM node,
and in solaris,
herez the code:
#include <stdio.h>
#include <string.h>
#define MAX 1
int main ()
{
char str[] ="1,2,3,4,5";
char * pch,b[MAX + 1];
int a;
printf ("Enter the int to be searched [%s]",str);
scanf("%d",&a);
sprintf(b,"%d",a);
b[MAX]='\0';
pch = strtok (str,",");
while (pch != NULL)
{
if(!strcmp(pch,b))
{
printf("found");
break;
}
pch = strtok (NULL, ",");
}
}
you had made use of strcmp - to compare two strings ( here a string and a char array )
but the char array is not terminated,
hold a value and termination value.