Domanda

I have the following code:

int number;
char temp[32] = "";


number = snprintf(temp, sizeof(char), "%c", 'A');

I made this variable number because snprintf has a return value, number can remove the warning of "ignore of return value of snprintf", then it gets another lint warning: Warning 550: Symbol 'number' not accessed, but I do not need to use the variable number, then how to fix it?

And if the number is assigned by the return value of snprintf, how could it complain that the number is not accessed, how to count it is accessed?

È stato utile?

Soluzione

In this case number receives the bytes printed by snprintf and lint is warning about a non used variable, you can skip this lint warning with

number = snprintf(temp, sizeof(char), "%c", 'A'); /* lint -save -e550 */

or as others says

(void)snprintf(temp, sizeof(char), "%c", 'A');

Altri suggerimenti

Function got return value to be checked, so use number to check it or do simply like this

char temp[32] = "";


if (snprintf(temp, sizeof(char), "%c", 'A') < 0)
  printf("Error occured");

try this

(void)snprintf(temp, sizeof(char), "%c", 'A');

if you really wants to use number and wants to remove the warning then try this..

int number = -1;
char temp[32] = "";


number = snprintf(temp, sizeof(char), "%c", 'A');

if(number <= 0)
{
 /*give some error message */
}

This is a warning that you defined a variable which is not used. In the above sample you assign it a value, but you don't do anything with it. If the variable is really not used, then you can remove it from your code, otherwise write some code that actually uses it, like an if, printfwhatever.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top