please help with mysterious error....

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Richard said:
Martin Jørgensen said:


Hi, there... I almost eliminated all errors/warnings. Only got a few left.
1. The line: "sscanf(line, "%s", &argument);" gives:

warning: format %s expects type char * , but argument 3 has type
char (*)[63u]

Definitions: "char line[64], argument[10];"

Is it afraid that sscanf will put more than 10 characters into argument?
What's the solution?


if(sscanf(line, "%9s", argument) == 1)
{

Your spurious & on &argument means you're passing char (*)[10] - a pointer
to an array of 10 char - instead of char *, which is what you get when you
pass the name of an array of char to a function.

Thanks a lot... Actually I forgot... I had #defined something called
maxsize or something. Suppose I had "#define maxsize 10", could I then
write if(sscanf(line, "%(maxsize-1)s", argument) == 1) ? That would be
better...
Drop the const from fp. The fseek function needs to be able to modify the
file position indicator member of the object pointed to by fp.

Much better.
3. I have a file called array_functions.c which has:

void int_fillinnumbers(int fillvalue, int startx,
int stopx, int starty,
int stopy, int **array)
{
int i, j; /* local index variables */

for(j=starty; j<=stopy; j++)
{
for(i=startx; i<=stopx; i++)
array[j] = fillvalue; /* integer-fill */
}
}

This function is used different places in the program and is called from
main.c. The problem is:

warning: no previous prototype for int_fillinnumbers

What's it whining about? It don't need another prototype, IMO...



If the function is called only from within the translation unit in which it
is defined, and is called only /after/ the function definition, then you
can ignore the warning. Otherwise, but this:

void int_fillinnumbers(int fillvalue, int startx,
int stopx, int starty,
int stopy, int **array);


Damn... I also had that............... I thought....

I just accidentally deleted the semi-colon and thought it was still
there, therefore I didn't understand it before...
in a header, and #include that header in all translation units that contain
calls to the function. It is also wise to #include it in the translation
unit where the function is defined, as this will catch any mods that are
done to the definition which have not been reflected in the header.




#pragmas are, by definition, non-portable (with one trivial C99 exception
which I'll ignore as irrelevant on this occasion).

The fix here is: don't use nasty/stupid unsigned/signed-tricks!

But I don't think I can't avoid it, as I'm using some old code which I
don't know how to rewrite... The messages are: "warning: comparison
between signed and unsigned", and I tried to debug this sh*t, but it's
really nasty so I didn't understood it. For instance it decremented an
unsigned variable below 0, so I dare not even cast any of it to the same
type if it makes any problems...

But hey, let me hear - perhaps I can explicitly cast and get rid of the
warnings, without losing functionality: I have this:

for (i=1;i<=left;i++)

"i" is unsigned int, "left" is integer. Would it be absolutely safe to
rewrite the line to:

1. for (i=1;i<= (unsigned) left;i++)

or

2. for ((int) i=1;i<= left;i++)

And then get the same nasty bit-fiddling functionality and no warning?


I also have
if(l < mm) l++;

where "l" is unsigned long, "mm" is int...




And then I have an output-routine that prints a 2D-array of type double **:

void double_printout(int startcol, int stopcol,
int startrow, int stoprow,
double **array, char *indent,
unsigned digits)
{
int i, j; /* local index variables */
char formatstring[] = "-> %8.2f ";

printf("\n");

if(digits >=8 || digits <0)
{
printf("ERROR! Can't print that number of digits.\n");
quit_program(__FILE__, __LINE__);
}

formatstring[6] = (char)('0'+ digits);

for(j=startrow; j<=stoprow; j++)
{
printf("\n%2d: %s", j, indent);
for(i=startcol; i<=stopcol; i++)
printf( ("%s", formatstring), array[j]); /*<= WARNING!*/
}
printf("\n\n");
}


I know that's probably considered ugly coding but I wanted to pass an
unsigned "digits"-value, that determines how many decimal places after
the comma should be included in the output... Therefore I had to modify
the "formatstring", which is passed as an argument to printf().

The message is:

"warning: format not a string literal, argument types not checked"

I guess it's trying to tell me that "formatstring" is not a string
literal. But I don't know how to fix the problem, then and still get
this "number of digits passed into argument"-behaviour.


Last problem (I think / I hope - it looks much better now):

This is probably also a bit nasty, but I need the program to generate a
number of output-files that are consecutive numbered like
"file0000.vtk", "file0001.vtk", "file0002.vtk", "file0003.vtk",
"file0004.vtk", ......., "file0009.vtk", "file0010.vtk", "file0011.vtk",
....... "file0099.vtk", "file0100.vtk"....

Therefore I needed to figure out how many digits the "counter"-variable
took up. I found a good method I think... At least I couldn't find a
better method. I used:

/* calculate number of leading zero's for 3-digit numbers up to 999 */
counter = 2 - (int) log10( (double) *output_filestep );

Let's try it: output_filestep (is an int AFAIR) = 5. log(5) = 0.7. We
make it (cast it to become) an integer, so it becomes (int) 0.7 => 0.
Counter = 2, because then we need to fill in two zero, i.e. "file00" +
"5". My problem is:

"warning: cast from function call of type double to non-matching type
int"

I thought I already casted it to an integer with the (int) part and
therefore I thought I explicitly told the compiler not to complain about
this cast. How do I fix it/make the warning disappear?



And that's it... The compiling process would really look beautiful if I
could get rid of the last warnings, if you know what I mean :) :)


Best regards
Martin Jørgensen
 
R

Richard Heathfield

Martin Jørgensen said:

I had #defined something called
maxsize or something. Suppose I had "#define maxsize 10", could I then
write if(sscanf(line, "%(maxsize-1)s", argument) == 1) ? That would be
better...

Well, you can do this:

#define STR(x) #x
#define XSTR(x) STR(x)

#define maxsize 9

char argument[maxsize + 1];

if(sscanf(line, "%" XSTR(maxsize)"s", argument))
{

but it's a bit ugly. Personally, I prefer to avoid *scanf completely.

But I don't think I can't avoid it, as I'm using some old code which I
don't know how to rewrite... The messages are: "warning: comparison
between signed and unsigned",

Sometimes you can put up with these warnings. It depends how close to the
edge you're going to get. If you're comparing si with ui and both of them
are only going to have values that are inside the range of both (i.e. 0 to
INT_MAX), then it's no big deal.

I don't recommend adding casts in an attempt to hide the problem. The
diagnostics are useful in reminding you that this problem exists and
requires caution. Clean compiles are all very well, and indeed very
laudable, but not (IMHO) at the expense of clarity, correctness, and
caution. The real fix is not to use those tricks - but if you can't avoid
that because you're not confident about editing the code, then you might be
better off leaving the warnings where you can see them, where they will
remind you that you have a few minor dragons in your code which must be
carefully fed and kept on a leash.

The message is:

"warning: format not a string literal, argument types not checked"

I guess it's trying to tell me that "formatstring" is not a string
literal. But I don't know how to fix the problem, then and still get
this "number of digits passed into argument"-behaviour.

Well, you can't. All the compiler is telling you is "I'd love to check these
% thingies for you, but hey, the string can change at runtime, so there
isn't much point". So it's a heads-up that you should be extra careful to
ensure that the % fields match up correctly by type, to the things you want
to print. No big deal.
Last problem (I think / I hope - it looks much better now):

This is probably also a bit nasty, but I need the program to generate a
number of output-files that are consecutive numbered like
"file0000.vtk", "file0001.vtk", "file0002.vtk", "file0003.vtk",
"file0004.vtk", ......., "file0009.vtk", "file0010.vtk", "file0011.vtk",
...... "file0099.vtk", "file0100.vtk"....

Therefore I needed to figure out how many digits the "counter"-variable
took up. I found a good method I think... At least I couldn't find a
better method. I used:

/* calculate number of leading zero's for 3-digit numbers up to 999 */
counter = 2 - (int) log10( (double) *output_filestep );

Why bother? Why not let sprintf do it for you?

Decide how many digits you want altogether (in your above examples, you are
using four digits altogether in each filename), and just do this:

fieldwidth = 4; /* or whatever */
sprintf(filename, "file%0*d.vtk", fieldwidth, *output_filestep);

or, if you are confident that you always want exactly four digits, just do:

sprintf(filename, "file%04d.vtk", *output_filestep);
 
B

Barry Schwarz

snip

Please learn to trim your quotes.
But I don't think I can't avoid it, as I'm using some old code which I
don't know how to rewrite... The messages are: "warning: comparison
between signed and unsigned", and I tried to debug this sh*t, but it's
really nasty so I didn't understood it. For instance it decremented an
unsigned variable below 0, so I dare not even cast any of it to the same
type if it makes any problems...

But hey, let me hear - perhaps I can explicitly cast and get rid of the
warnings, without losing functionality: I have this:

for (i=1;i<=left;i++)

"i" is unsigned int, "left" is integer. Would it be absolutely safe to

Why not create a new int ii and use that instead of i?
rewrite the line to:

1. for (i=1;i<= (unsigned) left;i++)

Can left ever be negative? Can left ever be UINT_MAX?
or

2. for ((int) i=1;i<= left;i++)

Can left ever be between INT_MAX and UINT_MAX?
And then get the same nasty bit-fiddling functionality and no warning?


I also have
if(l < mm) l++;

where "l" is unsigned long, "mm" is int...




And then I have an output-routine that prints a 2D-array of type double **:

Do you really have a 2D array of pointer to pointer to double? Or do
you have a pointer to pointer to double which you are treating as a 2D
array.
void double_printout(int startcol, int stopcol,
int startrow, int stoprow,
double **array, char *indent,
unsigned digits)
{
int i, j; /* local index variables */
char formatstring[] = "-> %8.2f ";

printf("\n");

if(digits >=8 || digits <0)

digits is unsigned. Can it ever be less than 0?
{
printf("ERROR! Can't print that number of digits.\n");
quit_program(__FILE__, __LINE__);
}

formatstring[6] = (char)('0'+ digits);

What do you think the cast does? Is digits always less than 10?
for(j=startrow; j<=stoprow; j++)
{
printf("\n%2d: %s", j, indent);
for(i=startcol; i<=stopcol; i++)
printf( ("%s", formatstring), array[j]); /*<= WARNING!*/


What do you think the
"%s",
does?
}
printf("\n\n");
}


I know that's probably considered ugly coding but I wanted to pass an
unsigned "digits"-value, that determines how many decimal places after
the comma should be included in the output... Therefore I had to modify
the "formatstring", which is passed as an argument to printf().

The message is:

"warning: format not a string literal, argument types not checked"

I guess it's trying to tell me that "formatstring" is not a string
literal. But I don't know how to fix the problem, then and still get
this "number of digits passed into argument"-behaviour.

Check your documentation to see if you can turn this warning off.
Last problem (I think / I hope - it looks much better now):

This is probably also a bit nasty, but I need the program to generate a
number of output-files that are consecutive numbered like
"file0000.vtk", "file0001.vtk", "file0002.vtk", "file0003.vtk",
"file0004.vtk", ......., "file0009.vtk", "file0010.vtk", "file0011.vtk",
...... "file0099.vtk", "file0100.vtk"....

Therefore I needed to figure out how many digits the "counter"-variable
took up. I found a good method I think... At least I couldn't find a
better method. I used:

/* calculate number of leading zero's for 3-digit numbers up to 999 */
counter = 2 - (int) log10( (double) *output_filestep );

What do you think the cast to double does?
Let's try it: output_filestep (is an int AFAIR) = 5. log(5) = 0.7. We
make it (cast it to become) an integer, so it becomes (int) 0.7 => 0.
Counter = 2, because then we need to fill in two zero, i.e. "file00" +
"5". My problem is:

"warning: cast from function call of type double to non-matching type
int"

I thought I already casted it to an integer with the (int) part and
therefore I thought I explicitly told the compiler not to complain about
this cast. How do I fix it/make the warning disappear?

This warning makes no sense to me. You are deliberately casting the
return from log10 to force the arithmetic to be performed in int mode
as opposed to the normal double mode. Check the docs again for a way
to suppress it.



Remove del for email
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Richard said:
Martin Jørgensen said:

I had #defined something called
maxsize or something. Suppose I had "#define maxsize 10", could I then
write if(sscanf(line, "%(maxsize-1)s", argument) == 1) ? That would be
better...


Well, you can do this:

#define STR(x) #x
#define XSTR(x) STR(x)

#define maxsize 9

char argument[maxsize + 1];

if(sscanf(line, "%" XSTR(maxsize)"s", argument))
{

but it's a bit ugly. Personally, I prefer to avoid *scanf completely.

Yes, that's ugly... I think I should just search/replace maxsize and
replace it with 10, so the %9s part is okay. Would you use:

#define MAXLINE 10
fgets(line, MAXLINE, fp)

Instead of scanf?
Sometimes you can put up with these warnings. It depends how close to the
edge you're going to get. If you're comparing si with ui and both of them
are only going to have values that are inside the range of both (i.e. 0 to
INT_MAX), then it's no big deal.

Ok. AFAIR: It did some crazy/stupid crap with decrementing an *unsigned*
counter to below 0 somewhere (that can't fit in a signed int, I guess).
But perhaps I could make it a long integer? hmm... Or I just stick with
those 6-7 warnings...
I don't recommend adding casts in an attempt to hide the problem. The
diagnostics are useful in reminding you that this problem exists and
requires caution. Clean compiles are all very well, and indeed very
laudable, but not (IMHO) at the expense of clarity, correctness, and
caution. The real fix is not to use those tricks - but if you can't avoid
that because you're not confident about editing the code, then you might be
better off leaving the warnings where you can see them, where they will
remind you that you have a few minor dragons in your code which must be
carefully fed and kept on a leash.

I think you're right... I just do that for now.
Well, you can't. All the compiler is telling you is "I'd love to check these
% thingies for you, but hey, the string can change at runtime, so there
isn't much point". So it's a heads-up that you should be extra careful to
ensure that the % fields match up correctly by type, to the things you want
to print. No big deal.

Ok, but I can't make a clean compilation then (unless I change that part)...
Why bother? Why not let sprintf do it for you?

Decide how many digits you want altogether (in your above examples, you are
using four digits altogether in each filename), and just do this:

fieldwidth = 4; /* or whatever */
sprintf(filename, "file%0*d.vtk", fieldwidth, *output_filestep);

or, if you are confident that you always want exactly four digits, just do:

sprintf(filename, "file%04d.vtk", *output_filestep);

Dough! Thanks. Look good - I'll try that :)


Best regards
Martin Jørgensen
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Barry said:
On Sat, 03 Jun 2006 02:04:01 +0200, Martin Jørgensen



Can left ever be negative? Can left ever be UINT_MAX?

AFAIR: If you say:

unsigned left = 0;
left--;

Then we get a very big unsigned number that can't fit in a integer. So I
think left is bigger than UINT_MAX, but I'm not sure as I didn't look at
that code in about 1 month and remember that it did some stupid
unsigned/signed tricks like left-- to below 0, even though it's unsigned.

-snip-
Do you really have a 2D array of pointer to pointer to double? Or do
you have a pointer to pointer to double which you are treating as a 2D
array.

Erhm. I think I have a pointer to pointer to double (dynamically
allocated - not static), which I treat as a 2D array. So perhaps writing
a 2D-array of type double ** is incorrect?

Anyway, the function argument is double **, right? I don't get any
compiler warnings/problems, so I think the code is okay.
void double_printout(int startcol, int stopcol,
int startrow, int stoprow,
double **array, char *indent,
unsigned digits)
{
int i, j; /* local index variables */
char formatstring[] = "-> %8.2f ";

printf("\n");

if(digits >=8 || digits <0)


digits is unsigned. Can it ever be less than 0?

No, correct. It was integer before. I just thought it was nice to keep
it if I suddenly changed it back to an integer (suppose I should pass
digits to another function that took "digits" as an integer), but I just
found out that the compiler with this high warning level complains.

I just removed || digits <0, to get rid of the warning.
{
printf("ERROR! Can't print that number of digits.\n");
quit_program(__FILE__, __LINE__);
}

formatstring[6] = (char)('0'+ digits);


What do you think the cast does? Is digits always less than 10?

It should be as I just did an "if(digits >=8)" quit. Actually I can't
remember if I inserted the cast to get rid of a compiler warning or not.
But formatstring consists of chars and the righthandside gives what...?
An integer? Should I remove the cast?
for(j=startrow; j<=stoprow; j++)
{
printf("\n%2d: %s", j, indent);
for(i=startcol; i<=stopcol; i++)
printf( ("%s", formatstring), array[j]); /*<= WARNING!*/



What do you think the
"%s",
does?


Hmm... Yes, that's not too good... Perhaps nothing? AFAIR I deleted it
late yesterday, due to a compiler warning.
-snip-
What do you think the cast to double does?

Makes sure that log10 gets a double-parameter as *output_filestep isn't
a double, but either unsigned int/int.
This warning makes no sense to me. You are deliberately casting the
return from log10 to force the arithmetic to be performed in int mode
as opposed to the normal double mode. Check the docs again for a way
to suppress it.

Perhaps I needed more parenthesis... But Richard came with a much better
solution than this code, so it doesn't matter now.


Best regards
Martin Jørgensen
 
B

Barry Schwarz

AFAIR: If you say:

unsigned left = 0;
left--;

Then we get a very big unsigned number that can't fit in a integer. So I
think left is bigger than UINT_MAX, but I'm not sure as I didn't look at
that code in about 1 month and remember that it did some stupid
unsigned/signed tricks like left-- to below 0, even though it's unsigned.

The point is that if left is negative, without the cast your for loop
executes 0 times. If you follow your suggestion and insert the cast,
you have changed the performance of the program for the sake of
eliminating a warning.
-snip-

Erhm. I think I have a pointer to pointer to double (dynamically
allocated - not static), which I treat as a 2D array. So perhaps writing
a 2D-array of type double ** is incorrect?

That was the point.
Anyway, the function argument is double **, right? I don't get any
compiler warnings/problems, so I think the code is okay.
void double_printout(int startcol, int stopcol,
int startrow, int stoprow,
double **array, char *indent,
unsigned digits)
{
int i, j; /* local index variables */
char formatstring[] = "-> %8.2f ";

printf("\n");

if(digits >=8 || digits <0)


digits is unsigned. Can it ever be less than 0?

No, correct. It was integer before. I just thought it was nice to keep
it if I suddenly changed it back to an integer (suppose I should pass
digits to another function that took "digits" as an integer), but I just
found out that the compiler with this high warning level complains.

I just removed || digits <0, to get rid of the warning.
{
printf("ERROR! Can't print that number of digits.\n");
quit_program(__FILE__, __LINE__);
}

formatstring[6] = (char)('0'+ digits);


What do you think the cast does? Is digits always less than 10?

It should be as I just did an "if(digits >=8)" quit. Actually I can't
remember if I inserted the cast to get rid of a compiler warning or not.
But formatstring consists of chars and the righthandside gives what...?
An integer? Should I remove the cast?

If your code had been
formatstring[6] = '0';
would you have inserted the cast? The RHS of both statements have the
same type.
for(j=startrow; j<=stoprow; j++)
{
printf("\n%2d: %s", j, indent);
for(i=startcol; i<=stopcol; i++)
printf( ("%s", formatstring), array[j]); /*<= WARNING!*/



What do you think the
"%s",
does?


Hmm... Yes, that's not too good... Perhaps nothing? AFAIR I deleted it
late yesterday, due to a compiler warning.
-snip-
What do you think the cast to double does?

Makes sure that log10 gets a double-parameter as *output_filestep isn't
a double, but either unsigned int/int.


If there is a prototype in scope for log10 and if the parameter has
type double, then the argument will be converted to double implicitly
if possible. If not possible, you have a syntax error which you
really want to know about. All the cast does is prohibit the compiler
from helping you. (It is the same as casting the return from malloc.)
Perhaps I needed more parenthesis... But Richard came with a much better
solution than this code, so it doesn't matter now.


Best regards
Martin Jørgensen


Remove del for email
 
R

Richard Heathfield

Martin Jørgensen said:
Richard said:
[...] but it's a bit ugly. Personally, I prefer to avoid *scanf
completely.

Yes, that's ugly... I think I should just search/replace maxsize and
replace it with 10, so the %9s part is okay. Would you use:

#define MAXLINE 10
fgets(line, MAXLINE, fp)

Instead of scanf?

Well, yes and no. No, in that I'd want to check the return value of the
function. No, in that I wouldn't actually use fgets either, probably. But
yes, some kind of function that grabs the text a line at a time - which
even fgets can do if no line is longer than the buffer you supply. So yes,
you're on the right track.

Having read it, then I'd pick it apart myself, using the various str*
parsing routines.

<snip>
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Flash said:
Martin Jørgensen wrote: -snip-



With both the while loop and the getchar before it the user can end up
having to press enter twice! Also, since you are only checking for \n
and not for EOF this can turn in to an infinite loop!

Would this be bullet-proof?


/* quit_program pauses before exiting the program with an error */


void quit_program(char *filename, int line)
{
int ch;
printf("\n\nSomething went wrong in line %d of file:\n%s!\n\n",
line, filename);
printf("\nPress <ENTER> to finish the program...\n\n");
fflush(stdout);
while( ( ch=getchar() ) != '\n' && ch != EOF);
exit( EXIT_FAILURE );
}

I always hate those keyboard-functions... Is there any chance there
would still be something left in the keyboard buffer and is it something
I should do anything to avoid?


Best regards
Martin Jørgensen
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Richard said:
Martin Jørgensen said:

Richard said:
[...] but it's a bit ugly. Personally, I prefer to avoid *scanf
completely.

Yes, that's ugly... I think I should just search/replace maxsize and
replace it with 10, so the %9s part is okay. Would you use:

#define MAXLINE 10
fgets(line, MAXLINE, fp)

Instead of scanf?


Well, yes and no. No, in that I'd want to check the return value of the
function. No, in that I wouldn't actually use fgets either, probably. But
yes, some kind of function that grabs the text a line at a time - which
even fgets can do if no line is longer than the buffer you supply. So yes,
you're on the right track.

Having read it, then I'd pick it apart myself, using the various str*
parsing routines.

Ok... I have something I can go and think about...

The good news is that the only warnings I got left is the:

- unsigned/signed warning +
- the "format not a string literal, argument types not checked"-warnings

(that came because of this "formatstring[6] = (char)('0'+ digits);" and
then formatstring was passed to printf).

I don't think I can do anything about those right now...


There were also some "uninitialized variable" warnings which I fixed,
even though it wasn't strictly necessary (the program would have gone
into quit_program so those variables would never have been used in that
case)...

It makes me sleep much better in the night with all those warnings
fixed.... I must have fixed a couple of hundred warning messages lately :)

So thanks again :)


Best regards
Martin Jørgensen
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Barry said:
On Sat, 03 Jun 2006 17:21:46 +0200, Martin Jørgensen



That was the point.

Hmm. What exactly is wrong with writing that?

-snip-
{
printf("ERROR! Can't print that number of digits.\n");
quit_program(__FILE__, __LINE__);
}

formatstring[6] = (char)('0'+ digits);


What do you think the cast does? Is digits always less than 10?

It should be as I just did an "if(digits >=8)" quit. Actually I can't
remember if I inserted the cast to get rid of a compiler warning or not.
But formatstring consists of chars and the righthandside gives what...?
An integer? Should I remove the cast?


If your code had been
formatstring[6] = '0';
would you have inserted the cast? The RHS of both statements have the
same type.

Then you're saying that I should just remove the cast, as both sides are
the same type... Hmm. Ok.

=snip-
If there is a prototype in scope for log10 and if the parameter has
type double, then the argument will be converted to double implicitly
if possible. If not possible, you have a syntax error which you
really want to know about. All the cast does is prohibit the compiler
from helping you. (It is the same as casting the return from malloc.)

I learned not to cast the return of malloc from this group... And it
looks like I now learned that it would automatically perform an implicit
cast from int to double, which I might not have known before... Actually
I thought I inserted it because I got a warning, but if you say so I
must have been using it without even trying to compile the code without
the cast.

BTW: If you or anyone else find out why this gave an error, it could be
interesting to know (I hope I didn't gave a wrong problem description
here or misread the warning-linenumber?):

counter = 2 - (int) log10( (double) *output_filestep );

"warning: cast from function call of type double to non-matching type
int"... It is actually strange...???

*output_filestep = pointer to int.


Best regards
Martin Jørgensen
 
K

Keith Thompson

Martin Jørgensen said:
I learned not to cast the return of malloc from this group... And it
looks like I now learned that it would automatically perform an
implicit cast from int to double, which I might not have known
before... Actually I thought I inserted it because I got a warning,
but if you say so I must have been using it without even trying to
compile the code without the cast.

There is no such thing as an "implicit cast", though it's a common
(and incorrect) phrase.

A cast is an operator, consisting of a type name in parentheses,
applied to some expression. A cast operator specifies a conversion.
A conversion can be either explicit (i.e., a cast) or implicit (e.g.,
"x = y", where x and y are of different numeric types).
 
K

Kevin Bagust

Martin said:
Ok... I have something I can go and think about...

The good news is that the only warnings I got left is the:

- unsigned/signed warning +
- the "format not a string literal, argument types not checked"-warnings

(that came because of this "formatstring[6] = (char)('0'+ digits);" and
then formatstring was passed to printf).

You should be able to fix the format string by replacing the number in
the format string with a * and then supplying the number as an int
argument so you would have something like this:

printf( "-> %8.*f", (int)digits, array[j]);

Kevin Bagust.
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Kevin said:
Martin said:
Ok... I have something I can go and think about...

The good news is that the only warnings I got left is the:

- unsigned/signed warning +
- the "format not a string literal, argument types not checked"-warnings

(that came because of this "formatstring[6] = (char)('0'+ digits);"
and then formatstring was passed to printf).


You should be able to fix the format string by replacing the number in
the format string with a * and then supplying the number as an int
argument so you would have something like this:

printf( "-> %8.*f", (int)digits, array[j]);


Oh, thanks a lot - that's great. I just looked it up in my book and it
seems like this should work without any difficulties at all. So I'll
implement it later.


Best regards
Martin Jørgensen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top