Re: right padding
- From: Pilcrow <Pilcrow6@xxxxxxxxx>
- Date: Tue, 28 Oct 2008 11:14:36 -0700
On Tue, 28 Oct 2008 13:48:05 +0000, Ben Bacarisse <ben.usenet@xxxxxxxxx>
wrote:
Using gcc in mingw on a windows machine.. All these varying 'standards'
have me dizzy.
If you pick two sets of strict options, one for C90 and one for C99
the compiler will tell you if you have used C99 features when you
don't mean to, or (in the second case) when you use implementation
specific features when you don't want to. This is one of the most
helpful things a compiler can do if you are writing library code like
this that should compile "almost everywhere". (For gcc and
derivatives -std=c89 -pedantic -Wall -Wextra is a good starting
point).
Thank you.
char buf[sizeof(int) * CHAR_BIT + 5) / 3];I assume that you meant
char buf[(sizeof(int) * CHAR_BIT +) /3];
but I fail to understand the significance of the value between the
square brackets. It evaluates to 12 on my system.
==============================
Ok, Mr H & Mr B. I have my current code for the function below. And I
can now call it like this:
---------------------------------------------
char *str;
int num = 123;
str = pad(num, 20, '*', PAD_RIGHT);
puts(str);
str = pad(INT_MAX - 5000, 20, '=', PAD_LEFT);
puts(str);
---------------------------------------------
and I think I understand that, if I now say free(str), since the pointer
*str points to the same area as the ptr *s in the subroutine, that the
area of memory freed is the area that was allocated in the subroutine.
No?
But what if I said
--------------------------------------------
printf("%s",pad(INT_MAX - 5000, 20, '=', PAD_LEFT));
--------------------------------------------
which I have found to work, how would I free the memory allocated in the
function?
And, Mr H, your <http://www.cpax.org.uk/prg/writings/fgetdata.php> was
what I had in mind, but I was incapable of using it.. I'll try again.
"We live to learn".
Oh, here's the current code. As you see, I have taken your comments to
heart, both of you, and I thank you both,
-----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#define PAD_RIGHT 0
#define PAD_LEFT 1
int digits(int n) /* how many digits in an integer */
{
if(n==0) return 1;
int sign = n;
n = abs(n);
int ct = 0;
while(n > 0) {
n /= 10;
ct++;
}
if(sign < 0) ct++;
return ct;
}
char *pad( /* func returns ptr to start of string */
int n, /* number to print */
size_t width, /* pad to this width */
int padch, /* character to pad with */
int rl) /* pad on right (0) or left (1) */
{
char *s; /* ptr to output string */
int count = 0; /* count of characters */
int dig = digits(n); /* space required for the number */
if(dig > width) {
fputs("Field not big enough for data");
exit (EXIT_FAILURE); /* maybe I should truncate? */
}
free(s);
s = malloc(width+1); /* get necessary memory */
if(s == NULL){
fputs("Inadequate memory!", stderr);
exit (EXIT_FAILURE);
}
char *t = s; /* save ptr */
if (rl == PAD_RIGHT) {
count = sprintf(t, "%d", n); /* insert num */
t += count; /* bump ptr */
while(count++ < width) /* pad on right */
*t++ = padch;
*t = '\0'; /* finish string */
}else{
while(count++ < width-dig) /* pad on left */
*t++ = padch;
sprintf(t, "%d", n); /* insert num */
}
return s; /* return ptr to start of string */
}
-----------------------------------------------------------------------
.
- Follow-Ups:
- Re: right padding
- From: Ben Bacarisse
- Re: right padding
- References:
- Re: right padding
- From: Ben Bacarisse
- Re: right padding
- From: Pilcrow
- Re: right padding
- From: Richard Heathfield
- Re: right padding
- From: Pilcrow
- Re: right padding
- From: Pilcrow
- Re: right padding
- From: James Kuyper
- Re: right padding
- From: Pilcrow
- Re: right padding
- From: Richard Heathfield
- Re: right padding
- From: Pilcrow
- Re: right padding
- From: Ben Bacarisse
- Re: right padding
- From: Pilcrow
- Re: right padding
- From: Ben Bacarisse
- Re: right padding
- Prev by Date: Re: printf warning: "format %08x expects format unsigned int ..."
- Next by Date: Re: right padding
- Previous by thread: Re: right padding
- Next by thread: Re: right padding
- Index(es):
Relevant Pages
|