...
Code Block |
---|
|
#include <complex.h>
#include <tgmath.h>
void func(void) {
double complex c = 2.0 + 4.0 * I;
double complex result = log2(c);
} |
Compliant Solution
If the programmer's intent was to only take the logarithm of the real part of the complex number. This compliant solution ensures that the logarithm is applied only to the real part of the complex number:
Code Block |
---|
|
#include <complex.h>
#include <tgmath.h>
void func(void) {
double complex c = 2.0 + 4.0 * I;
double complex result = log2(real(c));
} |
Compliant Solution
If the programmer was intending to take the logarithm of the complex number this compliant solution uses log instead of log2 as log can be used on complex arguments:
Code Block |
---|
|
#include <complex.h>
#include <tgmath.h>
void func(void) {
double complex c = 2.0 + 4.0 * I;
double complex result = log(c)/log(2);
} |
Noncompliant Code Example
...
Code Block |
---|
|
#include <stdio.h>
#include <string.h>
char *(*fp)(const char *, int);
int main(void) {
const char *c;
fp = strchr;
c = fp('e', "Hello");
printf("%s\n", c);
return 0;
}
|
Compliant Solution
In this compliant solution, the function pointer fp
is invoked with the correct number and type of arguments:
...
Code Block |
---|
|
/* In another source file */
long f(long x) {
return x < 0 ? -x : x;
}
/* In this source file, no f prototype in scope */
long f();
long g(int x) {
return f(x);
} |
Compliant Solution
In this compliant solution, the prototype for the function f()
is included in the source file in the scope of where it is called, and the function f()
is correctly called with an argument of type long
:
...