Jump to content
Sign in to follow this  
HOEK

Arrays and the IF statement

Recommended Posts

Is there a way to use a character array in a if statement, something like:

 

if ( array[] == {'C','O','M','M','A','N','D'} )
{
 do stuff...
}

Share this post


Link to post
Share on other sites

You cannot use the example you have exactly.

 

I don't know if the standard string library is included in C2C(++), but if it is you can use something like this:

 

   char pch1[5] = "hi";

   char pch2[5]; //presumably, something in this array

   if (strcmp (pch1, pch2))

   {

       //do something useful

   }

 

If the string library is not included you should be able to find source code for it on the net somewhere.  Not like it's all that original or secretive code anyway.

 

Hope this helps.

-Ryan

Share this post


Link to post
Share on other sites

There is no such a function or library in the Sourceboost, but once you'll do your own, might want to add a starting location to the function. Like this:

 

instr(Start, Original, SearchString)

 

And it returns byte position (starting from 1) where the found match is starting, or 0 if it doesn't find anything.

Share this post


Link to post
Share on other sites

Thanks guys.

 

I've tried to port the GNU strncmp function to SourceBoost, in essence it just compares strings character by character and returns 0 for a match.

 

The problem is that the C2C compiler can only handle one const char pointer in a function call, which means I can't send both strings or arrays to a single function.

 

I've seen the guys in HTPIC using the strncmp function, so there must be a workaround??

Share this post


Link to post
Share on other sites
The problem is that the C2C compiler can only handle one const char pointer in a function call, which means I can't send both strings or arrays to a single function.

But it can handle more than one regular char pointers.

However, it works only with the C++ compiler, not with pure C-compiler.

Share this post


Link to post
Share on other sites

Thanks for the help - I'm a South African student, so the C2C++ compiler will have to wait till a decent pay check arrives.  The only difference between C and C++ is that the one is object-orientated and the one is procedural? The normal compiler should be able to cope with this problem in my opinion.  In any case, here is the function for you ++ guys, have a look and let me know if it ticks...

 

/*------------------------------------------------------------*
 Filename: strncmp.c
 Purpose:
  Compares first n characters of two strings
  for lexical order.
  
 Inputs:
  char first:  First string to compare
  char second: Second string to compare	
  int size:    Max. number of chars to compare

 Outputs:
  returns <0 if first<last
  returns 0  if first=last
  returns >0 if first>last
*------------------------------------------------------------*/

int strcmp( char *first, char *second, int size)
{
if( !count )
 return(0);
 
while(--count&& *first&& *first== *last)
{
 first++;
 last++;
}

return(first-last);	
}

Share this post


Link to post
Share on other sites

Well, the source is full of wrongly named arguments :) Did you perhaps modify it already?

 

Anyway, this is what the compiler says when parameters correspond to names that are in the body:

 

Compiling...

C:\PROGRA~1\SOURCE~1\c2cpp.exe -PPIC16F877 -SRC -ols -md -O2 -W1 -Ip16F877.inc -osdfsdf.asm  sdfsdf.c

C2C++ 5.3e C/C++ compiler  Copyright(C) 1998-1999,2000-2004 by Pavel Baranov
[email=support@picant.com]support@picant.com[/email]
(Extended version)

Licensed to: Antti Toivanen


sdfsdf.c
C:\PROGRA~1\SOURCE~1\Lib\cppPIC2.lib
Message: Built-in variables are obsolete and mapped to PIC16F84 address space. Use variables defined inside the system header file
Adding banks...
Optimized 26 instruction(s)


Finished.
Done

Share this post


Link to post
Share on other sites

/*------------------------------------------------------------*
 Filename: strncmp.c
*------------------------------------------------------------*/

int strcmp(char *first, char *second, int count)
{
if( !count )
 return(0);
 
while(--count&& *first&& *first== *last)
{
 first++;
 last++;
}

return(first-last);	
}

 

"Size" should be "Count"

Function should also be declared in .h file or at top of source.

And const char should be normal chars as you explained above.

But it can handle more than one regular char pointers.

Hope that works.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...