CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

pointers problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

pointers problem
PostPosted: Wed Jun 10, 2015 8:07 am     Reply with quote

Hi,
I have problem with pointers. When I type 1234, B0,B1,B2 and B3 will become high, but if after that i send command 0, B0,B1,B2 and B3 still High. What is wrong with my coding? suppose all port B become low when i send command 0.

Code:

#include <18F452.h>
#device PASS_STRINGS=IN_RAM
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(crystal=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <stdio.h>
#include <input.c>
#include <string.h>

void main()
{
char *ptr;
char arrayChars[10];
   
ptr=arrayChars;

while(true)
  {
   printf("Enter number : %s \n",arrayChars);
   get_string(arrayChars,10);

   for(ptr=1;ptr<5;ptr++)
     {
      if (strcmp(arrayChars, "0" )==0)
          output_B(0b00000000);
         
      if (*ptr=="1")
          output_high(PIN_B0);
      if (*ptr=="2")
          output_high(PIN_B1);
      if (*ptr=="3")
          output_high(PIN_B2);
      if (*ptr=="4")
          output_high(PIN_B3);
     }
  }
}
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

PostPosted: Wed Jun 10, 2015 8:55 am     Reply with quote

You compare a char with string *ptr == "1"
"1" is set of two characters '1' and '\0'-string terminator.

Try to enclose 1 in single quotation marks to tell the compiler its type is a single char.

(*ptr=='1')

Another thing that may help is to clear the array arrayChars right above get string function.
Code:
strcpy(arrayChars, "");


I hope this will help
_________________
A person who never made a mistake never tried anything new.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Jun 10, 2015 9:05 am     Reply with quote

There are two separate problems.

First when you declare 'arrayChars', it is not initialised. It could initially contain _anything_, and almost certainly will not contain a terminating '\0'. The first print then could easily result in garbage being displayed.

Then the second problem is that '==' will not compare a _string_. It can compare a character, but your right hand argument is a string. I see Rokotech8, has spotted the same problems, while I was on the phone and typing.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 10, 2015 9:14 am     Reply with quote

One more very bad problem. Below, you declare 'ptr' as a char pointer
and initialize it to point to arrayChars[].
Quote:
char *ptr;
char arrayChars[10];

ptr=arrayChars;


But then, you use 'ptr' in a for() loop, wiping out the address of
arrayChars which was stored in it:
Quote:

for(ptr=1;ptr<5;ptr++)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group