Like Tree1Likes

char function call

This is a discussion on char function call within the C Programming forums, part of the General Programming Boards category; Code: void smssend(char *b1,char *b2) { printf(gsm,"AT+CMGF=1\r"); printf(gsm,"AT+CMGS="); putc(34,gsm); printf(gsm,"%s",b1); putc(34,gsm); printf(gsm,"\r"); printf(gsm,"%s",b2); delay_ms(100); putc(26,gsm); delay_ms(200); } void main() { ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    6

    char function call

    Code:
    void smssend(char *b1,char *b2)
    {
      printf(gsm,"AT+CMGF=1\r");
      printf(gsm,"AT+CMGS=");
      putc(34,gsm);
      printf(gsm,"%s",b1);
      putc(34,gsm);
      printf(gsm,"\r");
      printf(gsm,"%s",b2);
      delay_ms(100);
      putc(26,gsm);
      delay_ms(200);
    }
    void main()
    {
        while(1)
         {
            smssend("12345","JOVIN"); 
         }
    }
    this function shows error.how can i solve this

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Try posting your error messages.

    From the look of things (assuming your undeclared gsm variable is a FILE*), that your printf calls should be fprintf

    Also, main returns int, not void
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Quote Originally Posted by Salem View Post
    Try posting your error messages.

    From the look of things (assuming your undeclared gsm variable is a FILE*), that your printf calls should be fprintf

    Also, main returns int, not void

    Code:
    #include <16F877A.h>
    #device ADC=10 //for 10 bit resolution
    #include "pic.h"
    #use delay(CLOCK=20000000)
    #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,stream=pc)
    #use rs232(baud=9600,XMIT=PIN_D2,RCV=PIN_D3,stream=gsm)
    //void smssend(char *b1,char *b2);
    char num[12]="09995995523";
    void smssend(char *b1,char *b2)
    {
      fprintf(gsm,"AT+CMGF=1\r");
      fprintf(gsm,"AT+CMGS=");
      fputc(34,gsm);
      fprintf(gsm,"%s",b1);
      fputc(34,gsm);
      fprintf(gsm,"\r");
      fprintf(gsm,"%s",b2);
      delay_ms(100);
      fputc(26,gsm);
      delay_ms(200);
    }
    void main()
    {
        while(1)
         {
            smssend("12345","JOVIN"); 
         }
    }
    this is my original code.am wirting a code in mplab for pic programming. I got this error wen i compile

    Line 26(16,17): Bad expression syntax

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    I've no idea.
    Apart from all the PIC stuff, the code itself looks valid enough, and I can compile that part of it without any trouble.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,435
    I don't know which compiler you're using (MPLAB is just the IDE), but I've seen my compiler complain when you try to pass a string literal to a function expecting a char*.

    Try changing this:

    Code:
    void smssend(char *b1,char *b2)
    To this:

    Code:
    void smssend(const char *b1,const char *b2)

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Quote Originally Posted by Matticus View Post
    I don't know which compiler you're using (MPLAB is just the IDE), but I've seen my compiler complain when you try to pass a string literal to a function expecting a char*.

    Try changing this:

    Code:
    void smssend(char *b1,char *b2)
    To this:

    Code:
    void smssend(const char *b1,const char *b2)
    i tried it.but i got these errors

    *** Error 32 "D:\Beginow\Mplab\gsmcheck.c" Line 9(25,29): Expecting a , or )
    *** Error 48 "D:\Beginow\Mplab\gsmcheck.c" Line 9(26,28): Expecting a (
    *** Error 46 "D:\Beginow\Mplab\gsmcheck.c" Line 9(43,44): Expecting an =
    *** Error 43 "D:\Beginow\Mplab\gsmcheck.c" Line 10(1,2): Expecting a declaration
    *** Error 43 "D:\Beginow\Mplab\gsmcheck.c" Line 21(1,2): Expecting a declaration
    *** Error 58 "D:\Beginow\Mplab\gsmcheck.c" Line 26(16,17): Expecting a close paren

    am using ccs compiler

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    What is in pic.h (post the contents) ?

    If there is some incomplete declaration at the end of that file (I assume it's yours, because you use "" to include it, and not <>), then that will spill over to produce error messages in your main code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,433
    It looks like you are using the CCS compiler, right?

    And I'm guessing you have entered all your FUSE's and pin I/O in your .h file.

    You don't need to include stdio.h for the CCS compiler, so that's ok

    It might be a good idea to declare the strings you enter as a char array and enter a pointer to it into your function.

    [edit]
    I see you said CCS now...
    [/edit]
    Last edited by Click_here; 02-21-2013 at 04:37 PM. Reason: missed compiler in previous post
    Fact - Beethoven wrote his first symphony in C

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,433
    I just looked up the help file and using "char *" to pass strings in the way you were originally doing is fine.
    [edit] This doesn't appear to be fine on newer versions, see post #10 [/edit]

    I'm guessing you are calling your program "pic.c" and your .h file is "pic.h"

    Might I suggest that you change the names to something else, as it may be getting confused with another file
    Last edited by Click_here; 02-21-2013 at 05:03 PM.
    Fact - Beethoven wrote his first symphony in C

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,433
    and if you look up "Bad expression syntax" is has this

    Quote Originally Posted by CCS Help File
    Bad expression syntax
    This is a generic error message. It covers all incorrect syntax.
    That could be anything...

    I then put this search string into Google: CCS compiler "Bad expression syntax"
    and found this CCS :: View topic - Seemingly Simple Syntax Question

    It explained that "CCS doesn't permit pointers to constant strings. You have to copy the string to a ram buffer first."

    It then has an example which should help you
    Fact - Beethoven wrote his first symphony in C

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > It explained that "CCS doesn't permit pointers to constant strings. You have to copy the string to a ram buffer first."
    WTF PoS compiler is this!?
    The compiler is very close to being 100% ANSI compatible.
    Yeah, except for the fact that it can't handle strings.

    I really hope the situation has improved in the last decade - that post was "Posted: Wed Feb 26, 2003 5:11 pm"

    And how exactly are you supposed to "copy to a ram buffer first" - oh that's right, use strcpy()
    Code:
    char temp[20];
    strcpy(temp, "Hello World");
    Test1(temp); 
    Test1("Hello World");
    Just how does strcpy() work if Test1() fails?

    Even worse than that, PICs are resource constrained systems to begin with (not too much memory), so the very idea that you have to make copies of things is just brain-damaged to begin with.

    In other words, what extra magic do you need over and above what Matticus proposed in post #5.

    The normal prototype for strcpy is
    char *strcpy(char *dest, const char *src);

    I would suggest that in the PIC string.h header file, that their strcpy prototype has some extra magic in it just to make it work.
    If this is the case, then I would copy the magic syntax to the prototype for smssend() and see what happens.

    I still think there's something wrong with the pic.h (which still hasn't been posted).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,433
    WTF PoS compiler is this!?
    The compiler is very close to being 100% ANSI compatible.
    When I got shown this compiler/IDE the first time, I was told that it wasn't ANSI compatible.

    Yeah, except for the fact that it can't handle strings.
    That is a consequence to Microchip's implementation of its non-linear memory: PIC microcontrollors use separate program memory and data memory address buses. In the C18 compiler, the "far"/"near"/"rom"/"ram" keywords are used for storage qualifiers.

    CCS is really good at pumping out efficient hex files and does a lot of background work with memory (unlike the C18 compiler, where you have to manually nominate memory banks using pragma's). I find it good to use on small projects, but you do need to know it's limitations.
    Fact - Beethoven wrote his first symphony in C

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    So is strcpy() a blessed function that knows about the weird architecture, and can presumably cope with copying a string declared as a constant in one address space to a variable in another address space?




    I notice the OP seems to have left though.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,433
    Hmmm... The OP seems to have left.

    I love it when you get no feedback whether the problem was fixed or not...

    If they come back, I found another sample that may help them
    CCS :: View topic - Strings: strcpy and constant strings
    Salem likes this.
    Fact - Beethoven wrote his first symphony in C

  15. #15
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Sorry guys.i was busy with a project.i read everything you guys posted and the thread posted by TEIAM really worked for me.i am posting my final code here and also pic.h wasn't necessary because i was doing it for a project and in between i tried to send sms using this format and it ddn't work and that's why i posted it here.Thanks to everyone.
    Code:
    #include <16F877A.h>
    #fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP 
    #use delay(CLOCK=20000000)
    #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
    void smssend(char* b1,char* b2)
    {
      printf("AT+CMGF=1\r");
      printf("AT+CMGS=");
      putc(34);
      printf("%s",b1);
      putc(34);
      printf("\r");
      printf("%s",b2);
      delay_ms(100);
      putc(26);
      delay_ms(200);
    }
    void main()
    {
      char temp1[20],temp2[20];
      strcpy(temp1,"JOVIN");
      strcpy(temp2,"12345");
        while(1)
         {
            smssend(temp1,temp2);
         }
    }
    and i would like to know one more thing.is there any difference between char* a and char *a?
    Last edited by Jovin Basil; 03-01-2013 at 01:49 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2011, 09:59 PM
  2. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  3. Second scanf call with char does not work
    By madwizzy in forum C Programming
    Replies: 2
    Last Post: 02-15-2011, 09:37 PM
  4. Replies: 5
    Last Post: 10-17-2006, 08:54 AM
  5. Call by (char) or (string)?
    By Blackroot in forum C++ Programming
    Replies: 11
    Last Post: 01-04-2006, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21