LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   format '%s' expects type 'char *', but argument 3 has type 'char (*)[31]' (https://www.linuxquestions.org/questions/programming-9/format-s-expects-type-char-%2A-but-argument-3-has-type-char-%2A-%5B31%5D-4175525220/)

RuijunFan 11-12-2014 03:29 PM

format '%s' expects type 'char *', but argument 3 has type 'char (*)[31]'
 
Hello,

My c code complied OK and run well on UNIX.

But on Linux , it reported a warning while compiling .
Code:


Typed char varname[31];  // in .h file
 
 
 
Static void get_out( p1,p2,..,varname* bind)
 
{
 
Char tempstr[31];
char outidx[31];
.
 
.
 
sprintf(tempstr, ", %s[%s]",bind +i,outidx);  //  compiling warning: format '%s' expects type 'char *', but argument 3 has type 'char (*)[31]'
 
}


Can anybody tell me why ? it is ok in UNIX .

Thanks.

Ray

Keith Hedger 11-12-2014 04:41 PM

To start off with please use code tags!
The warning says what it is the printf statement is expecteing a pointer to a sring of characters ( char* ) where as you are fiving it a pointer to a an array of 31 characters, char* is a zero terminated pointer to a string your array may not be, any way to remove the warning just type cast the pointer ie:
Code:

printf ( "%s\n",(char*)tempstr);
You can also specify the amount of characters to be printed if your array has no 0 terminating character, see the printf man page for full details, passing unterminated strings to printf can cause a segfault

Tinkster 11-12-2014 05:09 PM

In addition to Keith's comment.

This doesn't mean that your code is OK in Unix, it means that the compiler you used on Unix sucks.


Cheers,
Tink

NevemTeve 11-13-2014 03:44 AM

You may have altered your code between compilation and publication:

Code:

good:  char chararray[10]; printf ("%s", chararray);
bad:  char chararray[10]; printf ("%s", &chararray);


Keith Hedger 11-13-2014 04:18 AM

Depending on the default settings for the compiler this would still flag up a warning.

rtmistler 11-13-2014 10:23 AM

Code:

Typed char varname[31];
Huh? Where'd "Typed" come from?
Code:

Char tempstr[31];
Char with a Capital C is not char unless your compiler agrees that it is.
Code:

sprintf(tempstr, ", %s[%s]",bind +i,outidx);
As others have pointed out, you can cast to fix the warning. I recommend you do cast to fix the warning. Personally I enable as many warning options as I can and work towards warning free code. Also said, this doesn't mean the code is correct, but it helps to not be fooling one's self about the results which can occur. At the very least warnings will highlight a situation where the compiler is interpreting something differently than you envisioned and you get a chance to either do a cast or change the code to something else. And I'm assuming that the variable i came from somewhere?

My earlier points are that "Typed" and "Char" are not native (IMHO) C terms, I believe native terms would be "typedef" and "char" (lowercase 'c'). If there are other preprocessor files which give you these abstractions, fine. Just beware of them. And I can't really think of a reason why someone would want to have "Typed" for any real reason. Especially since it only saves you about one keystroke. You have two fewer characters in the name, but you had to shift to type one of those characters. I do get that things like Char are available in the more modern IDEs like Visual Studio or Eclipse. Jus tnot sure yet I agree.

Keith Hedger 11-13-2014 10:30 AM

Quote:

Originally Posted by rtmistler (Post 5269270)
... Personally I enable as many warning options as I can and work towards warning free code. ...

I do that as well, warnings can be annoying but they are there for a reason, it's always best to get rid of as many as possible ( by fixing the code mostly ).

rtmistler 11-13-2014 12:15 PM

Here's my current flags that I typically use.

-Wall doesn't get them all

Funny, you learn alot about your coding style once you start paying more detailed attention.

And these are by no means the best, they just happen to be useful to me:

Code:

CFLAGS += -Wformat=2 -Werror -Wall -Wextra -Wswitch-default -Wswitch-enum -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn


All times are GMT -5. The time now is 01:21 AM.