Compiler errors calling sprintf: "expected 'char *' but argument is of type 'char'"

30,196

You are incorrectly using pointers. A "string" is defined as an array of characters, and so when you write char *mystring, you are declaring a pointer to a character array (or a string).

Now, if you dereference mystring using *mystring in your code, you are getting the first element of that array, which is just a character. As the warnings say, those functions accept char* parameters, not char.

So, just pass in the pointer, without dereferencing:

void myfunction(){
    sprintf(mystring, "%ld", (long)(slope * 10000));
    memcpy(LCDline1, mystring, strlen(mystring)+1);
}
Share:
30,196
Bob
Author by

Bob

Updated on July 09, 2022

Comments

  • Bob
    Bob almost 2 years

    I am trying to program a microchip in C. Right now my I'm working to update a line on an LCD screen but it doesnt work correctly. Can anyone shed some light on this?

    float slope = 0.0626;
    char *mystring;
    int8_t    LCDline1[20];
    
    void myfunction(){
        sprintf(*mystring, "%ld", (long)(slope * 10000));
        memcpy(LCDline1, *mystring, strlen(*mystring)+1);
    }
    

    When I run compile code I get the following three errors.

    calibrate.c:60:5: warning: passing argument 1 of 'sprintf' makes
    pointer from integer without a cast. note: expected 'char *'
    but argument is of type 'char'
    
    calibrate.c:61:5: warning: passing argument 1 of 'strlen' makes
    pointer from integer without a cast.  note: expected 'const char *'
    but argument is of type 'char'
    
    calibrate.c:61:5: warning: passing argument 2 of 'memcpy' makes
    pointer from integer without a cast. note: expected 'const void *' but
    argument is of type 'char'
    

    I am not sure what I am doing wrong, I am using the following definitions for my starting point

    void *memcpy(void *str1, const void *str2, size_t n)
    size_t strlen(const char *str)
    char *p = "String";